diff --git a/apps/web/components/dashboard/admin/AddUserDialog.tsx b/apps/web/components/dashboard/admin/AddUserDialog.tsx index 0581511d..a13c6b88 100644 --- a/apps/web/components/dashboard/admin/AddUserDialog.tsx +++ b/apps/web/components/dashboard/admin/AddUserDialog.tsx @@ -8,6 +8,7 @@ import { DialogFooter, DialogHeader, DialogTitle, + DialogTrigger, } from "@/components/ui/dialog"; import { Form, @@ -18,28 +19,31 @@ import { FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; import { toast } from "@/components/ui/use-toast"; -import { api } from "@/lib/trpc"; // Adjust the import path as needed +import { api } from "@/lib/trpc"; import { zodResolver } from "@hookform/resolvers/zod"; import { TRPCClientError } from "@trpc/client"; -import { SubmitHandler, useForm } from "react-hook-form"; +import { useForm } from "react-hook-form"; import { z } from "zod"; -import { zAdminCreateUserSchema } from "@hoarder/shared/types/users"; +import { zAdminCreateUserSchema } from "@hoarder/shared/types/admin"; type AdminCreateUserSchema = z.infer; -interface AddUserDialogProps { - isOpen: boolean; - onOpenChange: (isOpen: boolean) => void; - onUserAdded: () => void; -} - export default function AddUserDialog({ - isOpen, - onOpenChange, - onUserAdded, -}: AddUserDialogProps) { + children, +}: { + children?: React.ReactNode; +}) { + const apiUtils = api.useUtils(); + const [isOpen, onOpenChange] = useState(false); const form = useForm({ resolver: zodResolver(zAdminCreateUserSchema), defaultValues: { @@ -48,24 +52,18 @@ export default function AddUserDialog({ password: "", confirmPassword: "", role: "user", - adminPassword: "", }, }); - const [isLoading, setIsLoading] = useState(false); - const createUserMutation = api.admin.createUser.useMutation(); - - const handleCreateUser: SubmitHandler = async ( - data, - ) => { - setIsLoading(true); - try { - await createUserMutation.mutateAsync(data); + const { mutate, isPending } = api.admin.createUser.useMutation({ + onSuccess: () => { toast({ description: "User created successfully", }); onOpenChange(false); - onUserAdded(); - } catch (error) { + apiUtils.users.list.invalidate(); + apiUtils.admin.userStats.invalidate(); + }, + onError: (error) => { if (error instanceof TRPCClientError) { toast({ variant: "destructive", @@ -77,10 +75,8 @@ export default function AddUserDialog({ description: "Failed to create user", }); } - } finally { - setIsLoading(false); - } - }; + }, + }); useEffect(() => { if (!isOpen) { @@ -90,12 +86,13 @@ export default function AddUserDialog({ return ( + {children} Add User
- + mutate(val))}>
Role - + )} /> -
-
- ( - - Admin Password - - - - - - )} - /> -