Skip to content

Commit 117a38f

Browse files
committed
feat: allow changing occupation
1 parent 36e5c1f commit 117a38f

File tree

9 files changed

+52
-45
lines changed

9 files changed

+52
-45
lines changed

backend/src/modules/resource/occupation.controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
2+
Body,
23
Controller,
34
Get,
45
HttpException,
56
HttpStatus,
6-
Param,
77
Put,
88
} from '@nestjs/common';
99
import { ResourceService } from '@/modules/resource/resource.service';
@@ -21,12 +21,12 @@ export class OccupationController {
2121
return occupation;
2222
}
2323

24-
@Put(':type')
25-
async upgradeResource(@Param() params: OccupationTypeDto): Promise<void> {
24+
@Put()
25+
async changeOccupation(@Body() body: OccupationTypeDto): Promise<void> {
2626
const { id } = await getUser();
2727

2828
try {
29-
await this.resourceService.changeOccupation(id, params.occupation);
29+
await this.resourceService.changeOccupation(id, body.occupation);
3030
} catch (e) {
3131
throw new HttpException(
3232
'Failed to change occupation: ' + e,

frontend/components/ProposeTrade.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div>
2+
<div class="flex flex-col space-y-2 max-w-56">
33
<h1>Requested Resources:</h1>
44
<UInputMenu v-model="requestedResources.type" :options="resourceOptions" />
55
<UInput v-model="requestedResources.amount" type="number" />
@@ -32,7 +32,7 @@ const offeredResources = ref<{
3232
amount?: number,
3333
}>({});
3434
35-
async function submitTrade () {
35+
async function submitTrade() {
3636
const trade = {
3737
requestedResources: [requestedResources.value],
3838
offeredResources: [offeredResources.value],

frontend/components/ShowOccupation.vue

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
<template>
22
<div v-if="occupation">
33
<p>Occupation: {{ occupation }} {{ getOccupationIcon(occupation) }}</p>
4+
<br>
5+
<div class="flex flex-col space-y-2 max-w-56">
6+
<p>Change Occupation:</p>
7+
<UInputMenu v-model="chosenOccupation" :options="availableOccupations" />
8+
<UButton :disabled="cantChangeOccupation" @click="changeOccupation">
9+
<template v-if="chosenOccupation === null">
10+
Apply
11+
</template>
12+
<template v-else>
13+
Apply change to {{ chosenOccupation }} {{
14+
getOccupationIcon(chosenOccupation) }}
15+
</template>
16+
</UButton>
17+
</div>
18+
419
</div>
520
</template>
621

@@ -19,7 +34,8 @@ const occupationIcon = {
1934
[Occupation.HUNTER]: '🏹',
2035
};
2136
22-
function getOccupationIcon (occupation: Occupation) {
37+
38+
function getOccupationIcon(occupation: Occupation) {
2339
return occupationIcon[occupation];
2440
}
2541
@@ -31,14 +47,30 @@ const { data: occupation, refresh } = await useFetch<Occupation>(
3147
},
3248
);
3349
34-
let stopInterval: NodeJS.Timeout;
50+
const availableOccupations =
51+
computed(() => Object.values(Occupation)
52+
.filter((availableOccupation) => availableOccupation !== occupation.value));
53+
54+
const cantChangeOccupation =
55+
computed(() => chosenOccupation.value === null || chosenOccupation.value === occupation.value)
3556
36-
onMounted(() => {
37-
// Refresh data every 2.5 seconds
38-
stopInterval = setInterval(refresh, 2500);
39-
});
57+
const chosenOccupation = ref<null | Occupation>(null)
58+
59+
async function changeOccupation() {
60+
await useFetch(
61+
basePath + 'occupations',
62+
{
63+
method: 'PUT',
64+
body: {
65+
occupation: chosenOccupation.value
66+
},
67+
lazy: true,
68+
server: false,
69+
}
70+
);
71+
72+
chosenOccupation.value = null;
73+
await refresh();
74+
}
4075
41-
onUnmounted(() => {
42-
clearInterval(stopInterval);
43-
});
4476
</script>

frontend/components/navbar.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
const links = [{
3-
label: 'Home',
4-
icon: 'i-heroicons-home',
3+
label: 'User',
4+
icon: 'i-heroicons-user-circle',
55
to: '/',
66
}, {
77
label: 'Resources',

frontend/composables/useHeader.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

frontend/pages/index.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<template>
22
<div>
3-
<h1>Home</h1>
3+
<h1>User Overview</h1>
4+
<br>
5+
<ShowOccupation />
46
</div>
57
</template>
68

79
<script setup lang="ts">
8-
import { useHeader } from '~/composables/useHeader';
9-
10-
const { title } = useHeader();
11-
title.value = 'Home';
1210
</script>

frontend/pages/resources.vue

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
<template>
22
<div>
3-
<ShowOccupation />
4-
<br>
53
<h1>Available Resources:</h1>
64
<ShowAllResources />
75
</div>
86
</template>
97

108
<script setup lang="ts">
11-
import { useHeader } from '~/composables/useHeader';
12-
13-
const { title } = useHeader();
14-
title.value = 'Resources';
159
</script>

frontend/pages/trades.vue

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,4 @@
1111
</template>
1212

1313
<script setup lang="ts">
14-
import { useHeader } from '~/composables/useHeader';
15-
16-
const { title } = useHeader();
17-
title.value = 'Trades';
1814
</script>

frontend/pages/treaties.vue

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,4 @@
1111
</template>
1212

1313
<script setup lang="ts">
14-
import { useHeader } from '~/composables/useHeader';
15-
16-
const { title } = useHeader();
17-
title.value = 'Treaties';
1814
</script>

0 commit comments

Comments
 (0)