-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ama.vue
74 lines (70 loc) · 1.85 KB
/
ama.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<template>
<div class="flex-grow px-4 py-2 md:px-12 md:py-4 w-full">
<main class="text-muted text-lg">
<form
class="flex flex-col items-start py-8 gap-2"
:class="{ 'opacity-50 pointer-events-none': status === 'pending' }"
@submit.prevent="askQuestion"
>
<h1>
<label
for="question"
class="text-2xl leading-none mt-[5vw] mb-[1vw] max-w-[37.50rem]"
>
ask me anything
</label>
</h1>
<textarea
id="question"
name="question"
class="rounded w-full max-w-400px min-h-[10ch] text-black px-3 py-1"
/>
<button
type="submit"
:disabled="status === 'pending'"
class="underlined-link"
>
ask anonymously
</button>
</form>
<template v-if="status === 'success'">
<p>
<i class="i-ri-checkbox-fill" /> question submitted successfully!
</p>
<p>
<NuxtLink
to="/"
class="underlined-link"
>
go home
</NuxtLink>
</p>
</template>
<p v-else-if="status === 'error'">
<i class="i-ri-error-warning-fill" /> an error occurred
</p>
</main>
</div>
</template>
<script lang="ts" setup>
definePageMeta({ title: 'Ask me anything' })
const status = ref<'idle' | 'pending' | 'error' | 'success'>('idle')
async function askQuestion (event: Event) {
if (status.value === 'pending') return
try {
status.value = 'pending'
const formData = new FormData(event.target as HTMLFormElement)
await $fetch('/api/question', {
method: 'POST',
body: {
question: formData.get('question'),
},
})
status.value = 'success'
}
catch (error) {
console.error(error)
status.value = 'error'
}
}
</script>