From c2cda0f06e907232d98e37f570dafd3194b09180 Mon Sep 17 00:00:00 2001 From: pit Date: Fri, 29 Sep 2023 17:26:37 +0100 Subject: [PATCH] feat: update user functionality --- .../app/(dashboard)/admin/users/[id]/page.tsx | 75 +++++++++++++++++-- packages/lib/server-only/admin/update-user.ts | 8 +- packages/trpc/server/admin-router/router.ts | 33 ++++++++ packages/trpc/server/admin-router/schema.ts | 13 ++++ packages/trpc/server/router.ts | 2 + 5 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 packages/trpc/server/admin-router/router.ts create mode 100644 packages/trpc/server/admin-router/schema.ts diff --git a/apps/web/src/app/(dashboard)/admin/users/[id]/page.tsx b/apps/web/src/app/(dashboard)/admin/users/[id]/page.tsx index ddeb52058..d6b0d9d25 100644 --- a/apps/web/src/app/(dashboard)/admin/users/[id]/page.tsx +++ b/apps/web/src/app/(dashboard)/admin/users/[id]/page.tsx @@ -1,10 +1,13 @@ 'use client'; +import { useRouter } from 'next/navigation'; + import { Loader } from 'lucide-react'; import { Controller, useForm } from 'react-hook-form'; import { trpc } from '@documenso/trpc/react'; import { Button } from '@documenso/ui/primitives/button'; +import { Combobox } from '@documenso/ui/primitives/combobox'; import { Input } from '@documenso/ui/primitives/input'; import { Label } from '@documenso/ui/primitives/label'; import { SignaturePad } from '@documenso/ui/primitives/signature-pad'; @@ -13,7 +16,8 @@ import { useToast } from '@documenso/ui/primitives/use-toast'; import { FormErrorMessage } from '../../../../../components/form/form-error-message'; export default function UserPage({ params }: { params: { id: number } }) { - const toast = useToast(); + const { toast } = useToast(); + const router = useRouter(); const result = trpc.profile.getUser.useQuery( { @@ -24,6 +28,15 @@ export default function UserPage({ params }: { params: { id: number } }) { }, ); + const roles = result.data?.roles; + let rolesArr: string[] = []; + + if (roles) { + rolesArr = Object.values(roles); + } + + const { mutateAsync: updateUserMutation } = trpc.admin.updateUser.useMutation(); + const { register, control, @@ -31,10 +44,44 @@ export default function UserPage({ params }: { params: { id: number } }) { formState: { errors, isSubmitting }, } = useForm(); - console.log(result.data); - const onSubmit = async (data) => { console.log(data); + + // const submittedRoles = data.roles + // .split(',') + // .map((role: string) => role.trim()) + // .map((role: string) => role.toUpperCase()); + + // const dbRoles = JSON.stringify(Role); + + // const roleExists = submittedRoles.some((role: string) => dbRoles.includes(role)); + // console.log('roleExists', roleExists); + + // console.log('db roles', dbRoles); + + try { + await updateUserMutation({ + id: Number(result.data?.id), + name: data.name, + email: data.email, + roles: data.roles, + }); + + router.refresh(); + + toast({ + title: 'Profile updated', + description: 'Your profile has been updated.', + duration: 5000, + }); + } catch (e) { + console.log(e); + toast({ + title: 'Error', + description: 'An error occurred while updating your profile.', + variant: 'destructive', + }); + } }; return ( @@ -45,17 +92,30 @@ export default function UserPage({ params }: { params: { id: number } }) { - +
- +
-
+
+ + ( + onChange(values)} /> + )} + /> + +
+ {/*
@@ -74,7 +134,8 @@ export default function UserPage({ params }: { params: { id: number } }) { />
-
+ */} +