Skip to content

Commit

Permalink
feat: allow changing occupation
Browse files Browse the repository at this point in the history
  • Loading branch information
maybeanerd committed Jul 24, 2024
1 parent 36e5c1f commit 117a38f
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 45 deletions.
8 changes: 4 additions & 4 deletions backend/src/modules/resource/occupation.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Put,
} from '@nestjs/common';
import { ResourceService } from '@/modules/resource/resource.service';
Expand All @@ -21,12 +21,12 @@ export class OccupationController {
return occupation;
}

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

try {
await this.resourceService.changeOccupation(id, params.occupation);
await this.resourceService.changeOccupation(id, body.occupation);
} catch (e) {
throw new HttpException(
'Failed to change occupation: ' + e,
Expand Down
4 changes: 2 additions & 2 deletions frontend/components/ProposeTrade.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div>
<div class="flex flex-col space-y-2 max-w-56">
<h1>Requested Resources:</h1>
<UInputMenu v-model="requestedResources.type" :options="resourceOptions" />
<UInput v-model="requestedResources.amount" type="number" />
Expand Down Expand Up @@ -32,7 +32,7 @@ const offeredResources = ref<{
amount?: number,
}>({});
async function submitTrade () {
async function submitTrade() {
const trade = {
requestedResources: [requestedResources.value],
offeredResources: [offeredResources.value],
Expand Down
50 changes: 41 additions & 9 deletions frontend/components/ShowOccupation.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<template>
<div v-if="occupation">
<p>Occupation: {{ occupation }} {{ getOccupationIcon(occupation) }}</p>
<br>
<div class="flex flex-col space-y-2 max-w-56">
<p>Change Occupation:</p>
<UInputMenu v-model="chosenOccupation" :options="availableOccupations" />
<UButton :disabled="cantChangeOccupation" @click="changeOccupation">
<template v-if="chosenOccupation === null">
Apply
</template>
<template v-else>
Apply change to {{ chosenOccupation }} {{
getOccupationIcon(chosenOccupation) }}
</template>
</UButton>
</div>

</div>
</template>

Expand All @@ -19,7 +34,8 @@ const occupationIcon = {
[Occupation.HUNTER]: '🏹',
};
function getOccupationIcon (occupation: Occupation) {
function getOccupationIcon(occupation: Occupation) {
return occupationIcon[occupation];
}
Expand All @@ -31,14 +47,30 @@ const { data: occupation, refresh } = await useFetch<Occupation>(
},
);
let stopInterval: NodeJS.Timeout;
const availableOccupations =
computed(() => Object.values(Occupation)
.filter((availableOccupation) => availableOccupation !== occupation.value));
const cantChangeOccupation =
computed(() => chosenOccupation.value === null || chosenOccupation.value === occupation.value)
onMounted(() => {
// Refresh data every 2.5 seconds
stopInterval = setInterval(refresh, 2500);
});
const chosenOccupation = ref<null | Occupation>(null)
async function changeOccupation() {
await useFetch(
basePath + 'occupations',
{
method: 'PUT',
body: {
occupation: chosenOccupation.value
},
lazy: true,
server: false,
}
);
chosenOccupation.value = null;
await refresh();
}
onUnmounted(() => {
clearInterval(stopInterval);
});
</script>
4 changes: 2 additions & 2 deletions frontend/components/navbar.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
const links = [{
label: 'Home',
icon: 'i-heroicons-home',
label: 'User',
icon: 'i-heroicons-user-circle',
to: '/',
}, {
label: 'Resources',
Expand Down
9 changes: 0 additions & 9 deletions frontend/composables/useHeader.ts

This file was deleted.

8 changes: 3 additions & 5 deletions frontend/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<template>
<div>
<h1>Home</h1>
<h1>User Overview</h1>
<br>
<ShowOccupation />
</div>
</template>

<script setup lang="ts">
import { useHeader } from '~/composables/useHeader';
const { title } = useHeader();
title.value = 'Home';
</script>
6 changes: 0 additions & 6 deletions frontend/pages/resources.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
<template>
<div>
<ShowOccupation />
<br>
<h1>Available Resources:</h1>
<ShowAllResources />
</div>
</template>

<script setup lang="ts">
import { useHeader } from '~/composables/useHeader';
const { title } = useHeader();
title.value = 'Resources';
</script>
4 changes: 0 additions & 4 deletions frontend/pages/trades.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@
</template>

<script setup lang="ts">
import { useHeader } from '~/composables/useHeader';
const { title } = useHeader();
title.value = 'Trades';
</script>
4 changes: 0 additions & 4 deletions frontend/pages/treaties.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@
</template>

<script setup lang="ts">
import { useHeader } from '~/composables/useHeader';
const { title } = useHeader();
title.value = 'Treaties';
</script>

0 comments on commit 117a38f

Please sign in to comment.