|
1 | 1 | "use client"; |
2 | | -import { useState } from "react"; |
3 | | -import { inviteMemberToClassroom } from "../../../actions"; |
| 2 | +import { ReactNode, useState } from "react"; |
| 3 | +import { InviteActionResults } from "../../../actions"; |
4 | 4 | import { toast } from "sonner"; |
| 5 | +import { Loader2, UserPlus } from "lucide-react"; |
| 6 | +import { Button } from "@/shared/components/ui/button"; |
| 7 | +// import { TooltipUtil } from "@/app/classrooms/clientUtils"; |
| 8 | +import { Input } from "@/shared/components/ui/input"; |
| 9 | +import { |
| 10 | + Dialog, |
| 11 | + DialogContent, |
| 12 | + DialogDescription, |
| 13 | + DialogFooter, |
| 14 | + DialogHeader, |
| 15 | + DialogTitle, |
| 16 | + DialogTrigger, |
| 17 | +} from "@/shared/components/ui/dialog"; |
| 18 | +import { Label } from "@/shared/components/ui/label"; |
| 19 | +// import { |
| 20 | +// Dialog, |
| 21 | +// DialogContent, |
| 22 | +// DialogDescription, |
| 23 | +// DialogHeader, |
| 24 | +// DialogTitle, |
| 25 | +// DialogTrigger, |
| 26 | +// } from "@/components/ui/dialog"; |
5 | 27 |
|
6 | | -export default function InviteMember({ classroomId }: { classroomId: number }) { |
| 28 | +export default function InviteMember({ |
| 29 | + optimisticUpdateCallback, |
| 30 | + dialogTrigger, |
| 31 | +}: { |
| 32 | + optimisticUpdateCallback: (email: string) => Promise<unknown>; |
| 33 | + dialogTrigger?: ReactNode; |
| 34 | +}) { |
7 | 35 | const [email, setEmail] = useState(""); |
8 | | - const handleInvite = async () => { |
9 | | - try { |
10 | | - await inviteMemberToClassroom(email, classroomId); |
11 | | - setEmail(""); |
12 | | - toast.success("Added Member Successfully", { |
13 | | - description: `${email} was added to the class.`, |
14 | | - }); |
15 | | - } catch (error: unknown) { |
16 | | - //type unknown for typescript lint |
17 | | - if (error instanceof Error) { |
18 | | - toast.error("The user is already part of the classroom."); |
19 | | - // console.error(error.message); |
| 36 | + const [isDialogOpen, setIsDialogOpen] = useState(false); |
| 37 | + const [isPending, setIsPending] = useState(false); |
| 38 | + |
| 39 | + const inviteCallback = async () => { |
| 40 | + setIsDialogOpen(false); |
| 41 | + setIsPending(true); |
| 42 | + // startTransition(async () => { |
| 43 | + const result: InviteActionResults = (await optimisticUpdateCallback( |
| 44 | + email |
| 45 | + )) as InviteActionResults; |
| 46 | + if (!result.supabaseInsertSuccess) { |
| 47 | + if (result.userDoesNotExist) { |
| 48 | + toast.error(`User does not exist!`); |
| 49 | + } else if (result.userAlreadyInClass) { |
| 50 | + toast.error(`User already in class!`); |
20 | 51 | } else { |
21 | | - console.error("Error Occured"); |
| 52 | + toast.error( |
| 53 | + `Uh oh! Something went wrong when inviting member to classroom.`, |
| 54 | + { |
| 55 | + description: `Please refresh and try again`, |
| 56 | + } |
| 57 | + ); |
22 | 58 | } |
| 59 | + } else { |
| 60 | + toast.success(`Invited user with email ${email} successfully!`); |
23 | 61 | } |
| 62 | + setIsDialogOpen(false); |
| 63 | + setIsPending(false); |
| 64 | + setEmail(""); |
| 65 | + return; |
24 | 66 | }; |
25 | 67 |
|
26 | 68 | return ( |
27 | 69 | <div className="my-3 flex gap-5"> |
28 | | - <input |
29 | | - type="email" |
30 | | - placeholder="Enter Email" |
31 | | - value={email} |
32 | | - onChange={(e) => setEmail(e.target.value)} |
33 | | - className={ |
34 | | - "block w-5/12 rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-blue-500 dark:focus:ring-blue-500" |
35 | | - } |
36 | | - /> |
37 | | - |
38 | | - <button |
39 | | - onClick={handleInvite} |
40 | | - className="w-full rounded-lg bg-blue-700 px-5 py-2.5 text-center text-sm font-medium text-white hover:bg-blue-800 focus:outline-none focus:ring-4 focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800 sm:w-auto" |
41 | | - > |
42 | | - Invite |
43 | | - </button> |
| 70 | + <Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}> |
| 71 | + {dialogTrigger ?? ( |
| 72 | + <DialogTrigger asChild> |
| 73 | + <Button variant="outline" effect={"hoverUnderline"}> |
| 74 | + Invite Member <UserPlus /> |
| 75 | + </Button> |
| 76 | + </DialogTrigger> |
| 77 | + )} |
| 78 | + <DialogContent className="sm:max-w-[425px]"> |
| 79 | + <DialogHeader> |
| 80 | + <DialogTitle>Invite Member</DialogTitle> |
| 81 | + <DialogDescription> |
| 82 | + Enter the email of the member you would like to invite to your |
| 83 | + classroom here. |
| 84 | + </DialogDescription> |
| 85 | + </DialogHeader> |
| 86 | + <div className="grid gap-4 py-4"> |
| 87 | + <div className="grid grid-cols-4 items-center gap-4"> |
| 88 | + <Label htmlFor="name" className="text-right"> |
| 89 | + Email |
| 90 | + </Label> |
| 91 | + <Input |
| 92 | + id="email" |
| 93 | + value={email} |
| 94 | + className="col-span-3" |
| 95 | + onChange={(e) => setEmail(e.target.value)} |
| 96 | + /> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + <DialogFooter> |
| 100 | + <Button onClick={inviteCallback} type="submit"> |
| 101 | + {isPending && <Loader2 className="animate-spin" />} |
| 102 | + Invite |
| 103 | + </Button> |
| 104 | + </DialogFooter> |
| 105 | + </DialogContent> |
| 106 | + </Dialog> |
44 | 107 | </div> |
45 | 108 | ); |
46 | 109 | } |
0 commit comments