HomePage.vue 1.28 KB
<script setup>
import { onMounted, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'
import { Button, Card, Input } from '@drama/ui'

import { api, getToken } from '../api.js'

const { t } = useI18n()
const router = useRouter()
const me = ref(null)
const note = ref('')

async function load() {
  me.value = await api('GET', '/api/voko/me')
}

async function apply() {
  await api('POST', '/api/voko/access-requests', { note: note.value })
  await load()
}

onMounted(async () => {
  if (getToken() === null) {
    await router.push('/login')
    return
  }
  await load()
})
</script>

<template>
  <Card v-if="me">
    <h1>{{ t('home.title') }}</h1>
    <p>{{ me.user.email }}</p>
    <template v-if="me.access_request === null">
      <p>{{ t('home.notApplied') }}</p>
      <label class="voko-field">{{ t('home.note') }}<Input v-model="note" /></label>
      <Button @click="apply">{{ t('home.apply') }}</Button>
    </template>
    <p v-else>{{ t(`home.status.${me.access_request.status}`) }}</p>
    <p v-if="me.access_request !== null && me.access_request.status === 'approved'"><router-link to="/agents">{{ t('home.agents') }}</router-link></p>
    <router-link v-if="me.user.role === 'admin'" to="/admin">{{ t('home.admin') }}</router-link>
  </Card>
</template>