101 lines
3.1 KiB
Vue
101 lines
3.1 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, ref } from 'vue'
|
|
import type { InvitePreview, JoinResponse } from '~/types'
|
|
import { apiErrorMessage } from '~/utils/errors'
|
|
import { formatDate } from '~/utils/format'
|
|
|
|
definePageMeta({ layout: 'auth' })
|
|
useHead({ title: 'Join a room · Rhema Together' })
|
|
|
|
const route = useRoute()
|
|
const api = useApi()
|
|
const { session, load } = useSession()
|
|
|
|
const token = computed(() => String(route.params.token ?? ''))
|
|
const invite = ref<InvitePreview | null>(null)
|
|
const loading = ref(true)
|
|
const loadError = ref('')
|
|
const displayName = ref('')
|
|
const submitting = ref(false)
|
|
const error = ref('')
|
|
|
|
onMounted(async () => {
|
|
displayName.value = session.value.user?.display_name ?? ''
|
|
try {
|
|
invite.value = await api.get<InvitePreview>(`/invites/${encodeURIComponent(token.value)}`)
|
|
} catch (err) {
|
|
loadError.value = apiErrorMessage(err)
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
})
|
|
|
|
async function submit() {
|
|
if (submitting.value || !invite.value) return
|
|
submitting.value = true
|
|
error.value = ''
|
|
try {
|
|
const name = session.value.user?.display_name || displayName.value.trim()
|
|
const response = await api.post<JoinResponse>(`/invites/${encodeURIComponent(token.value)}`, {
|
|
display_name: name,
|
|
})
|
|
await load(true)
|
|
await navigateTo(`/room/${response.room_id}`, { replace: true })
|
|
} catch (err) {
|
|
error.value = apiErrorMessage(err)
|
|
} finally {
|
|
submitting.value = false
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div>
|
|
<template v-if="loading">
|
|
<p class="meta">Checking the invite.</p>
|
|
</template>
|
|
|
|
<template v-else-if="loadError">
|
|
<h1 class="auth-title">Invite</h1>
|
|
<p class="form-error" role="alert">{{ loadError }}</p>
|
|
<div class="auth-footer">
|
|
<NuxtLink to="/login">Sign in</NuxtLink> if you already have an account.
|
|
</div>
|
|
</template>
|
|
|
|
<template v-else-if="invite">
|
|
<h1 class="auth-title">Join {{ invite.room_name }}</h1>
|
|
<p class="auth-note">
|
|
Owned by {{ invite.owner }}. The link works once and expires {{ formatDate(invite.expires_at) }}.
|
|
</p>
|
|
|
|
<form class="form" @submit.prevent="submit">
|
|
<p v-if="session.user" class="form-note">Joining as {{ session.user.display_name }}.</p>
|
|
<div v-else class="field">
|
|
<label class="field-label" for="join-name">Display name</label>
|
|
<input
|
|
id="join-name"
|
|
v-model="displayName"
|
|
class="input"
|
|
type="text"
|
|
autocomplete="name"
|
|
autofocus
|
|
required
|
|
>
|
|
<span class="hint">This is how the room sees you. You can add an account later.</span>
|
|
</div>
|
|
|
|
<p v-if="error" class="form-error" role="alert">{{ error }}</p>
|
|
|
|
<button type="submit" class="btn btn-primary" :disabled="submitting || (!session.user && !displayName.trim())">
|
|
{{ submitting ? 'Joining' : 'Join room' }}
|
|
</button>
|
|
</form>
|
|
|
|
<div v-if="session.user" class="auth-footer">
|
|
<NuxtLink to="/rooms">Back to rooms</NuxtLink>
|
|
</div>
|
|
</template>
|
|
</div>
|
|
</template>
|