|
| 1 | +<script setup lang="ts"> |
| 2 | +import { signUpSchema } from "#shared/validation/sign-up"; |
| 3 | +import { toTypedSchema } from "@vee-validate/zod"; |
| 4 | +import { LoaderIcon } from "lucide-vue-next"; |
| 5 | +import { useForm } from "vee-validate"; |
| 6 | +import { toast } from "vue-sonner"; |
| 7 | +import { isFetchError } from "~/lib/is-ofetch-error"; |
| 8 | +
|
| 9 | +const validationSchema = toTypedSchema(signUpSchema); |
| 10 | +
|
| 11 | +const form = useForm({ |
| 12 | + validationSchema, |
| 13 | + initialValues: { |
| 14 | + name: "", |
| 15 | + tos: false, |
| 16 | + }, |
| 17 | +}); |
| 18 | +
|
| 19 | +const isSubmitting = computed(() => form.isSubmitting.value); |
| 20 | +
|
| 21 | +const onSubmit = form.handleSubmit(async (values, { resetForm }) => { |
| 22 | + try { |
| 23 | + await new Promise((resolve) => setTimeout(resolve, 1000)); // Simulate network delay |
| 24 | + const response = await $fetch("/sign-up", { method: "POST", body: values }); |
| 25 | + toast.success(response.message); |
| 26 | + resetForm(); |
| 27 | + } catch (error) { |
| 28 | + if (isFetchError(error)) { |
| 29 | + toast.error(error.statusMessage ?? error.message); |
| 30 | + } else { |
| 31 | + toast.error("An unexpected error occurred."); |
| 32 | + console.error(error); |
| 33 | + } |
| 34 | + } |
| 35 | +}); |
| 36 | +</script> |
| 37 | + |
| 38 | +<template> |
| 39 | + <form @submit="onSubmit" @reset="form.handleReset"> |
| 40 | + <div> |
| 41 | + <p class="text-xl font-bold">Sign Up</p> |
| 42 | + <p class="text-sm text-muted-foreground"> |
| 43 | + Create an account to get started. |
| 44 | + </p> |
| 45 | + </div> |
| 46 | + <FormField v-slot="{ componentField }" name="name"> |
| 47 | + <FormItem> |
| 48 | + <FormLabel>Name</FormLabel> |
| 49 | + <FormControl> |
| 50 | + <Input type="text" v-bind="componentField" placeholder="Jane Doe" /> |
| 51 | + </FormControl> |
| 52 | + <FormMessage /> |
| 53 | + </FormItem> |
| 54 | + </FormField> |
| 55 | + <FormField v-slot="{ value, handleChange }" type="checkbox" name="tos"> |
| 56 | + <FormItem> |
| 57 | + <div class="space-y-4 leading-none"> |
| 58 | + <div class="flex items-center space-x-2"> |
| 59 | + <FormControl> |
| 60 | + <Checkbox |
| 61 | + :model-value="value" |
| 62 | + @update:model-value="handleChange" |
| 63 | + /> |
| 64 | + </FormControl> |
| 65 | + <FormLabel |
| 66 | + class="text-sm font-normal leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70" |
| 67 | + >I agree to the terms of service.</FormLabel |
| 68 | + > |
| 69 | + </div> |
| 70 | + <FormMessage /> |
| 71 | + </div> |
| 72 | + </FormItem> |
| 73 | + </FormField> |
| 74 | + <div class="grid lt-sm:grid-cols-1 sm:grid-cols-2 gap-2"> |
| 75 | + <Button type="reset" variant="secondary" :disabled="isSubmitting" |
| 76 | + >Reset</Button |
| 77 | + > |
| 78 | + <Button |
| 79 | + type="submit" |
| 80 | + class="flex items-center gap-2" |
| 81 | + :disabled="isSubmitting" |
| 82 | + > |
| 83 | + <span>Submit</span> |
| 84 | + <LoaderIcon v-if="isSubmitting" class="animate-spin w-[1em]" /> |
| 85 | + </Button> |
| 86 | + </div> |
| 87 | + |
| 88 | + <span |
| 89 | + aria-hidden="true" |
| 90 | + class="absolute w-3 h-3 rounded-full bg-primary/30 -top-1.5 -right-1.5 animate-ping" |
| 91 | + ></span> |
| 92 | + |
| 93 | + <span |
| 94 | + aria-hidden="true" |
| 95 | + class="absolute w-2 h-2 rounded-full bg-primary -top-1 -right-1" |
| 96 | + ></span> |
| 97 | + </form> |
| 98 | +</template> |
0 commit comments