Files
pichunt/app/pages/hunt/[huntSlug]/[questSlug]/admin.vue
T
Pascal 370ba3406f refactor(hunt): replace admin middleware with auth and implement membership state
- Change middleware from 'admin' to 'auth' on hunt and quest admin pages
- Add global hunt store to track current hunt ID and membership status
- Use hunt store membership state to conditionally display admin UI elements
- Handle fetch errors gracefully with alerts and back navigation buttons
- Update admin UI logic to depend on membership state instead of user role
- Add membership state update after API calls in hunt, quest, teams, and admin pages
- Refactor UI templates to show error messages when fetch fails
- Centralize membership logic for better authorization control in frontend
2026-07-30 22:33:47 +02:00

107 lines
3.1 KiB
Vue

<script setup lang="ts">
import { parseError, sluggy } from '#imports';
import { useI18nKey } from '~/composables/useI18nKey';
import { useHuntStore } from '~/stores/hunt';
import { useTitleStore } from '~/stores/title';
definePageMeta({
middleware: ['auth'],
keepalive: false
});
const app = useNuxtApp();
const { loggedIn } = useUserSession();
const localePath = useLocalePath();
const { t } = useI18n();
const { tKey, hasKey, tSluggy } = useI18nKey();
const router = useRouter();
const { huntSlug } = app.$slugData;
const { name, id } = app.$slugData.questSlug!;
const titleStore = useTitleStore();
const huntStore = useHuntStore();
const { data, refresh, error } = await useFetch(`/api/admin/quest/${id}`);
huntStore.setMembership(huntSlug!.id, !error.value);
useHead({
title: t('layouts.title', {
title: hasKey(data.value, 'title') ? tKey(data.value!, 'title').value : name
})
});
watch(loggedIn, (isLoggedIn) => {
if (!isLoggedIn) {
router.push(localePath('/login'));
}
});
titleStore.title = data.value ? tKey(data.value, 'title').value : undefined;
</script>
<template>
<div>
<div v-if="error">
<VaAlert color="danger" class="my-4">
{{ t(parseError(error)) }}
</VaAlert>
<VaButton
icon="arrow_back"
:to="localePath(`/hunt/${sluggy(huntSlug!)}`)"
>{{ t('page.common.back_to_hunt') }}</VaButton
>
</div>
<template v-else>
<h1 class="my-4 text-3xl">Admin{{ data?.title_de }}</h1>
<VaButtonGroup>
<VaButton
icon="arrow_back"
:to="
localePath(
`/hunt/${data ? tSluggy(data.hunt).value : sluggy(huntSlug!)}`
)
"
>{{ t('page.common.back_to_hunt') }}</VaButton
>
<VaButton icon="replay" color="success" @click="refresh"
>Refresh</VaButton
>
</VaButtonGroup>
<div v-if="data" class="my-2 flex flex-col gap-2 p-4">
<p>{{ tKey(data, 'description') }}</p>
<p v-if="hasKey(data, 'question')">{{ tKey(data, 'question') }}</p>
</div>
<div class="mb-4 grid grid-cols-2 gap-2 p-2 md:flex md:flex-row">
<VaChip outline>In progress</VaChip>
<VaChip color="primary">Reviewable</VaChip>
<VaChip color="danger">Review missing</VaChip>
<VaChip color="success">Reviewed</VaChip>
</div>
<div v-if="data">
<p class="text-xl">{{ data.points }} Points</p>
<p v-if="data.extraPoints" class="text-xl">
{{ data.extraPoints }} Extra points
</p>
<p v-if="hasKey(data, 'extraText')">
Extra: {{ tKey(data, 'extraText') }}
</p>
</div>
<div v-if="data && data.answers.length > 0">
<VaAccordion multiple stateful>
<AdminAnswer
v-for="answer in data.answers"
:key="answer.id"
:answer="answer"
:quest="data"
@update="refresh"
/>
</VaAccordion>
</div>
<p v-else class="mt-4 text-lg">No answers yet</p>
</template>
</div>
</template>
<style scoped></style>