|
| 1 | +import * as React from "react" |
| 2 | +import { zodResolver } from "@hookform/resolvers/zod" |
| 3 | +import { useForm } from "react-hook-form" |
| 4 | +import { toast } from "sonner" |
| 5 | + |
| 6 | +import { registerUserWithAccessCode } from "@/server/actions/registration" |
| 7 | +import { |
| 8 | + joinWorkshopWithCodeSchema, |
| 9 | + type JoinWorkshopWithCodeSchema, |
| 10 | +} from "@/lib/zod/schemas/workshops" |
| 11 | +import { showErrorToast } from "@/utils/handle-error" |
| 12 | + |
| 13 | +import { Icons } from "../icons" |
| 14 | +import { Button } from "../ui/button" |
| 15 | +import { Dialog, DialogContent, DialogHeader, DialogTitle } from "../ui/dialog" |
| 16 | +import { JoinWorkshopWithCodeForm } from "./join-workshop-with-code-form" |
| 17 | + |
| 18 | +interface JoinWorkshopWithCodeModalProps { |
| 19 | + userId: string |
| 20 | +} |
| 21 | + |
| 22 | +export function JoinWorkshopWithCodeModal({ |
| 23 | + showJoinWorkshopWithCodeModal, |
| 24 | + setShowJoinWorkshopWithCodeModal, |
| 25 | + props, |
| 26 | +}: { |
| 27 | + showJoinWorkshopWithCodeModal: boolean |
| 28 | + setShowJoinWorkshopWithCodeModal: React.Dispatch< |
| 29 | + React.SetStateAction<boolean> |
| 30 | + > |
| 31 | + props: JoinWorkshopWithCodeModalProps |
| 32 | +}) { |
| 33 | + const [isPending, startTransition] = React.useTransition() |
| 34 | + |
| 35 | + const form = useForm<JoinWorkshopWithCodeSchema>({ |
| 36 | + resolver: zodResolver(joinWorkshopWithCodeSchema), |
| 37 | + defaultValues: { |
| 38 | + accessCode: "", |
| 39 | + }, |
| 40 | + }) |
| 41 | + |
| 42 | + const onSubmit = (values: JoinWorkshopWithCodeSchema) => { |
| 43 | + startTransition(async () => { |
| 44 | + const { error } = await registerUserWithAccessCode({ |
| 45 | + userId: props.userId, |
| 46 | + accessCode: values.accessCode, |
| 47 | + }) |
| 48 | + |
| 49 | + if (error) { |
| 50 | + showErrorToast(error) |
| 51 | + } |
| 52 | + |
| 53 | + setShowJoinWorkshopWithCodeModal(false) |
| 54 | + toast.success("Registration Successful") |
| 55 | + form.reset() |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + return ( |
| 60 | + <Dialog |
| 61 | + open={showJoinWorkshopWithCodeModal} |
| 62 | + onOpenChange={setShowJoinWorkshopWithCodeModal} |
| 63 | + > |
| 64 | + <DialogContent className="max-w-sm"> |
| 65 | + <DialogHeader className="items-center"> |
| 66 | + <DialogTitle className="text-base">Join Workshop</DialogTitle> |
| 67 | + </DialogHeader> |
| 68 | + <JoinWorkshopWithCodeForm onSubmit={onSubmit} form={form}> |
| 69 | + <Button disabled={isPending}> |
| 70 | + {isPending && ( |
| 71 | + <Icons.spinner |
| 72 | + className="mr-2 size-4 animate-spin" |
| 73 | + aria-hidden="true" |
| 74 | + /> |
| 75 | + )} |
| 76 | + Join |
| 77 | + </Button> |
| 78 | + </JoinWorkshopWithCodeForm> |
| 79 | + </DialogContent> |
| 80 | + </Dialog> |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +export function useJoinWorkshopWithCodeModal( |
| 85 | + props: JoinWorkshopWithCodeModalProps |
| 86 | +) { |
| 87 | + const [showJoinWorkshopWithCodeModal, setShowJoinWorkshopWithCodeModal] = |
| 88 | + React.useState(false) |
| 89 | + |
| 90 | + const JoinWorkshopWithCodeModalCallback = React.useCallback( |
| 91 | + () => ( |
| 92 | + <JoinWorkshopWithCodeModal |
| 93 | + showJoinWorkshopWithCodeModal={showJoinWorkshopWithCodeModal} |
| 94 | + setShowJoinWorkshopWithCodeModal={setShowJoinWorkshopWithCodeModal} |
| 95 | + props={props} |
| 96 | + /> |
| 97 | + ), |
| 98 | + [showJoinWorkshopWithCodeModal, props] |
| 99 | + ) |
| 100 | + |
| 101 | + return React.useMemo( |
| 102 | + () => ({ |
| 103 | + showJoinWorkshopWithCodeModal, |
| 104 | + setShowJoinWorkshopWithCodeModal, |
| 105 | + JoinWorkshopWithCodeModal: JoinWorkshopWithCodeModalCallback, |
| 106 | + }), |
| 107 | + [JoinWorkshopWithCodeModalCallback, showJoinWorkshopWithCodeModal] |
| 108 | + ) |
| 109 | +} |
0 commit comments