|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { signUpSchema, SignUpDto } from "@repo/dtos/auth"; |
| 4 | +import { Button } from "@/components/ui/button"; |
| 5 | +import { |
| 6 | + Form, |
| 7 | + FormControl, |
| 8 | + FormField, |
| 9 | + FormItem, |
| 10 | + FormLabel, |
| 11 | + FormMessage, |
| 12 | +} from "@/components/ui/form"; |
| 13 | +import { Input } from "@/components/ui/input"; |
| 14 | +import { useMutation, useQueryClient } from "@tanstack/react-query"; |
| 15 | +import { signUp } from "@/lib/api/auth"; |
| 16 | +import { useZodForm } from "@/lib/form"; |
| 17 | +import { useLoginState } from "@/contexts/LoginStateContext"; |
| 18 | +import { useToast } from "@/hooks/use-toast"; |
| 19 | + |
| 20 | +export function SignUpForm() { |
| 21 | + const form = useZodForm({ schema: signUpSchema }); |
| 22 | + const { setHasLoginStateFlag } = useLoginState(); |
| 23 | + const queryClient = useQueryClient(); |
| 24 | + const { toast } = useToast(); |
| 25 | + const mutation = useMutation({ |
| 26 | + mutationFn: signUp, |
| 27 | + onSuccess: async () => { |
| 28 | + setHasLoginStateFlag(); |
| 29 | + await queryClient.invalidateQueries({ queryKey: ["me"] }); |
| 30 | + }, |
| 31 | + onError: (error) => { |
| 32 | + toast({ |
| 33 | + description: error.message, |
| 34 | + variant: "destructive", |
| 35 | + }); |
| 36 | + }, |
| 37 | + }); |
| 38 | + function onSubmit(values: SignUpDto) { |
| 39 | + mutation.mutate(values); |
| 40 | + } |
| 41 | + return ( |
| 42 | + <Form {...form}> |
| 43 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8"> |
| 44 | + <FormField |
| 45 | + control={form.control} |
| 46 | + name="username" |
| 47 | + render={({ field }) => ( |
| 48 | + <FormItem> |
| 49 | + <FormLabel>Username</FormLabel> |
| 50 | + <FormControl> |
| 51 | + <Input {...field} /> |
| 52 | + </FormControl> |
| 53 | + <FormMessage /> |
| 54 | + </FormItem> |
| 55 | + )} |
| 56 | + /> |
| 57 | + <FormField |
| 58 | + control={form.control} |
| 59 | + name="email" |
| 60 | + render={({ field }) => ( |
| 61 | + <FormItem> |
| 62 | + <FormLabel>Email</FormLabel> |
| 63 | + <FormControl> |
| 64 | + <Input {...field} /> |
| 65 | + </FormControl> |
| 66 | + <FormMessage /> |
| 67 | + </FormItem> |
| 68 | + )} |
| 69 | + /> |
| 70 | + <FormField |
| 71 | + control={form.control} |
| 72 | + name="password" |
| 73 | + render={({ field }) => ( |
| 74 | + <FormItem> |
| 75 | + <FormLabel>Password</FormLabel> |
| 76 | + <FormControl> |
| 77 | + <Input type="password" {...field} /> |
| 78 | + </FormControl> |
| 79 | + <FormMessage /> |
| 80 | + </FormItem> |
| 81 | + )} |
| 82 | + /> |
| 83 | + <FormField |
| 84 | + control={form.control} |
| 85 | + name="confirmPassword" |
| 86 | + render={({ field }) => ( |
| 87 | + <FormItem> |
| 88 | + <FormLabel>Confirm Password</FormLabel> |
| 89 | + <FormControl> |
| 90 | + <Input type="password" {...field} /> |
| 91 | + </FormControl> |
| 92 | + <FormMessage /> |
| 93 | + </FormItem> |
| 94 | + )} |
| 95 | + /> |
| 96 | + <Button className="w-full" type="submit"> |
| 97 | + Submit |
| 98 | + </Button> |
| 99 | + </form> |
| 100 | + </Form> |
| 101 | + ); |
| 102 | +} |
0 commit comments