From 333d1610fad10e70759545f223959503288a02c6 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Thu, 10 Jul 2025 19:34:31 +0000 Subject: feat: Add invite user support --- apps/web/components/admin/CreateInviteDialog.tsx | 134 +++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 apps/web/components/admin/CreateInviteDialog.tsx (limited to 'apps/web/components/admin/CreateInviteDialog.tsx') diff --git a/apps/web/components/admin/CreateInviteDialog.tsx b/apps/web/components/admin/CreateInviteDialog.tsx new file mode 100644 index 00000000..84f5c60f --- /dev/null +++ b/apps/web/components/admin/CreateInviteDialog.tsx @@ -0,0 +1,134 @@ +"use client"; + +import { useState } from "react"; +import { ActionButton } from "@/components/ui/action-button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { toast } from "@/components/ui/use-toast"; +import { api } from "@/lib/trpc"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { TRPCClientError } from "@trpc/client"; +import { useForm } from "react-hook-form"; +import { z } from "zod"; + +const createInviteSchema = z.object({ + email: z.string().email("Please enter a valid email address"), +}); + +interface CreateInviteDialogProps { + children: React.ReactNode; +} + +export default function CreateInviteDialog({ + children, +}: CreateInviteDialogProps) { + const [open, setOpen] = useState(false); + const [errorMessage, setErrorMessage] = useState(""); + + const form = useForm>({ + resolver: zodResolver(createInviteSchema), + defaultValues: { + email: "", + }, + }); + + const invalidateInvitesList = api.useUtils().invites.list.invalidate; + const createInviteMutation = api.invites.create.useMutation({ + onSuccess: () => { + toast({ + description: "Invite sent successfully", + }); + invalidateInvitesList(); + setOpen(false); + form.reset(); + setErrorMessage(""); + }, + onError: (e) => { + if (e instanceof TRPCClientError) { + setErrorMessage(e.message); + } else { + setErrorMessage("Failed to send invite"); + } + }, + }); + + return ( + + {children} + + + Send User Invitation + + Send an invitation to a new user to join Karakeep. They'll + receive an email with instructions to create their account and will + be assigned the "user" role. + + + +
+ { + setErrorMessage(""); + await createInviteMutation.mutateAsync(value); + })} + className="space-y-4" + > + {errorMessage && ( +

{errorMessage}

+ )} + + ( + + Email Address + + + + + + )} + /> + +
+ setOpen(false)} + > + Cancel + + + Send Invitation + +
+ + +
+
+ ); +} -- cgit v1.2.3-70-g09d2