game-village-politics-frontend/app/pages/account.vue

127 lines
5.0 KiB
Vue
Raw Permalink Normal View History

<script setup lang="ts">
import type { AccountState } from '~/types/game'
const errorText = useErrorText()
const notices = useNotices()
const { t } = useI18n()
const { logout } = useAuth()
const account = ref<AccountState | null>(null)
const error = ref<string | null>(null)
const busy = ref(false)
async function call<T>(event: string, data: object = {}) {
busy.value = true
error.value = null
try {
return await ws.send<T>(event, data)
} catch (e) {
error.value = errorText(e)
return null
} finally {
busy.value = false
}
}
call<AccountState>('account.show').then((a) => (account.value = a))
const pw = reactive({ current: '', next: '', again: '' })
async function changePassword() {
if (pw.next !== pw.again) {
error.value = t('auth.password_mismatch')
return
}
const a = await call<AccountState>('account.password', { current: pw.current, password: pw.next })
if (a) {
account.value = a
Object.assign(pw, { current: '', next: '', again: '' })
notices.push(t('account.password_changed'), 'success')
}
}
const leave = reactive({ password: '', sure: false })
async function deleteAccount() {
if (!leave.sure) return
if (await call('account.delete', { password: leave.password })) {
logout()
await navigateTo('/')
}
}
</script>
<!-- The account: who you are on which worlds, a new password, and leaving for good. -->
<template>
<div>
<div class="mx-auto max-w-3xl space-y-4 px-4 py-8 text-sm">
<h1 class="font-display text-3xl font-semibold text-wood-900" style="font-variant: small-caps">{{ $t('account.title') }}</h1>
<p v-if="error" class="alert" role="alert">{{ error }}</p>
<p v-if="!account" class="text-ink-500">{{ $t('common.loading') }}</p>
<template v-else>
<section class="panel">
<div class="panel-head"><h2 class="panel-title">{{ account.name }}</h2></div>
<dl class="grid grid-cols-[auto_1fr] gap-x-6 gap-y-1 p-3">
<dt class="text-ink-500">{{ $t('auth.email') }}</dt>
<dd>{{ account.email }}</dd>
<dt class="text-ink-500">{{ $t('account.since') }}</dt>
<dd>{{ new Date(account.since).toLocaleDateString() }}</dd>
</dl>
<table v-if="account.worlds.length" class="grid-table">
<thead>
<tr>
<th>{{ $t('account.world') }}</th>
<th class="text-right">{{ $t('account.villages') }}</th>
<th class="text-right">{{ $t('account.points') }}</th>
<th>{{ $t('account.tribe') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="w in account.worlds" :key="w.world.id">
<td class="font-semibold">{{ w.world.name }}</td>
<td class="text-right tabular-nums">{{ w.villages }}</td>
<td class="text-right tabular-nums">{{ w.points }}</td>
<td>{{ w.tribe ? `${w.tribe.name} [${w.tribe.tag}]` : '' }}</td>
</tr>
</tbody>
</table>
</section>
<section class="panel">
<div class="panel-head"><h2 class="panel-title">{{ $t('account.password') }}</h2></div>
<form class="grid gap-3 p-3 sm:grid-cols-3" @submit.prevent="changePassword">
<label class="block">
<span class="label">{{ $t('account.current') }}</span>
<input v-model="pw.current" class="input" type="password" autocomplete="current-password" required />
</label>
<label class="block">
<span class="label">{{ $t('account.new') }}</span>
<input v-model="pw.next" class="input" type="password" autocomplete="new-password" required />
</label>
<label class="block">
<span class="label">{{ $t('auth.password_again') }}</span>
<input v-model="pw.again" class="input" type="password" autocomplete="new-password" required />
</label>
<div class="sm:col-span-3">
<button type="submit" class="btn btn-primary" :disabled="busy">{{ $t('account.change') }}</button>
</div>
</form>
</section>
<section class="panel border-blood-700">
<div class="panel-head"><h2 class="panel-title text-blood-700">{{ $t('account.delete') }}</h2></div>
<form class="space-y-3 p-3" @submit.prevent="deleteAccount">
<p>{{ $t('account.delete_text') }}</p>
<label class="block max-w-xs">
<span class="label">{{ $t('auth.password') }}</span>
<input v-model="leave.password" class="input" type="password" autocomplete="current-password" required />
</label>
<label class="flex items-center gap-2">
<input v-model="leave.sure" type="checkbox" />
{{ $t('account.delete_sure') }}
</label>
<button type="submit" class="btn btn-attack" :disabled="busy || !leave.sure">{{ $t('account.delete_button') }}</button>
</form>
</section>
</template>
</div>
<GameFooter />
</div>
</template>