mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
feat: profile page done
This commit is contained in:
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
|
|
||||||
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { Loader } from 'lucide-react';
|
import { Loader } from 'lucide-react';
|
||||||
import { Controller, useForm } from 'react-hook-form';
|
import { Controller, useForm } from 'react-hook-form';
|
||||||
|
|
||||||
@ -10,10 +11,13 @@ import { Button } from '@documenso/ui/primitives/button';
|
|||||||
import { Combobox } from '@documenso/ui/primitives/combobox';
|
import { Combobox } from '@documenso/ui/primitives/combobox';
|
||||||
import { Input } from '@documenso/ui/primitives/input';
|
import { Input } from '@documenso/ui/primitives/input';
|
||||||
import { Label } from '@documenso/ui/primitives/label';
|
import { Label } from '@documenso/ui/primitives/label';
|
||||||
import { SignaturePad } from '@documenso/ui/primitives/signature-pad';
|
|
||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
import { FormErrorMessage } from '../../../../../components/form/form-error-message';
|
import { FormErrorMessage } from '../../../../../components/form/form-error-message';
|
||||||
|
import {
|
||||||
|
TUserFormSchema,
|
||||||
|
ZUserFormSchema,
|
||||||
|
} from '../../../../../providers/admin-user-profile-update.types';
|
||||||
|
|
||||||
export default function UserPage({ params }: { params: { id: number } }) {
|
export default function UserPage({ params }: { params: { id: number } }) {
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
@ -28,7 +32,9 @@ export default function UserPage({ params }: { params: { id: number } }) {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const roles = result.data?.roles;
|
const user = result.data;
|
||||||
|
|
||||||
|
const roles = user?.roles;
|
||||||
let rolesArr: string[] = [];
|
let rolesArr: string[] = [];
|
||||||
|
|
||||||
if (roles) {
|
if (roles) {
|
||||||
@ -42,29 +48,22 @@ export default function UserPage({ params }: { params: { id: number } }) {
|
|||||||
control,
|
control,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
formState: { errors, isSubmitting },
|
formState: { errors, isSubmitting },
|
||||||
} = useForm();
|
} = useForm<TUserFormSchema>({
|
||||||
|
resolver: zodResolver(ZUserFormSchema),
|
||||||
const onSubmit = async (data) => {
|
values: {
|
||||||
console.log(data);
|
name: user?.name ?? '',
|
||||||
|
email: user?.email ?? '',
|
||||||
// const submittedRoles = data.roles
|
roles: user?.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);
|
|
||||||
|
|
||||||
|
const onSubmit = async ({ name, email, roles }: TUserFormSchema) => {
|
||||||
try {
|
try {
|
||||||
await updateUserMutation({
|
await updateUserMutation({
|
||||||
id: Number(result.data?.id),
|
id: Number(user?.id),
|
||||||
name: data.name,
|
name,
|
||||||
email: data.email,
|
email,
|
||||||
roles: data.roles,
|
roles,
|
||||||
});
|
});
|
||||||
|
|
||||||
router.refresh();
|
router.refresh();
|
||||||
@ -86,20 +85,20 @@ export default function UserPage({ params }: { params: { id: number } }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h2 className="text-4xl font-semibold">Manage {result.data?.name}'s profile</h2>
|
<h2 className="text-4xl font-semibold">Manage {user?.name}'s profile</h2>
|
||||||
<form className="mt-6 flex w-full flex-col gap-y-4" onSubmit={handleSubmit(onSubmit)}>
|
<form className="mt-6 flex w-full flex-col gap-y-4" onSubmit={handleSubmit(onSubmit)}>
|
||||||
<div>
|
<div>
|
||||||
<Label htmlFor="name" className="text-muted-foreground">
|
<Label htmlFor="name" className="text-muted-foreground">
|
||||||
Name
|
Name
|
||||||
</Label>
|
</Label>
|
||||||
<Input placeholder={result.data?.name} type="text" {...register('name')} />
|
<Input defaultValue={user?.name ?? ''} type="text" {...register('name')} />
|
||||||
<FormErrorMessage className="mt-1.5" error={errors.name} />
|
<FormErrorMessage className="mt-1.5" error={errors.name} />
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<Label htmlFor="email" className="text-muted-foreground">
|
<Label htmlFor="email" className="text-muted-foreground">
|
||||||
Email
|
Email
|
||||||
</Label>
|
</Label>
|
||||||
<Input placeholder={result.data?.email} {...register('email')} />
|
<Input defaultValue={user?.email} type="email" {...register('email')} />
|
||||||
<FormErrorMessage className="mt-1.5" error={errors.email} />
|
<FormErrorMessage className="mt-1.5" error={errors.email} />
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
@ -115,26 +114,6 @@ export default function UserPage({ params }: { params: { id: number } }) {
|
|||||||
/>
|
/>
|
||||||
<FormErrorMessage className="mt-1.5" error={errors.roles} />
|
<FormErrorMessage className="mt-1.5" error={errors.roles} />
|
||||||
</div>
|
</div>
|
||||||
{/* <div>
|
|
||||||
<Label htmlFor="signature" className="text-muted-foreground">
|
|
||||||
Signature
|
|
||||||
</Label>
|
|
||||||
|
|
||||||
<div className="mt-2">
|
|
||||||
<Controller
|
|
||||||
control={control}
|
|
||||||
name="signature"
|
|
||||||
render={({ field: { onChange } }) => (
|
|
||||||
<SignaturePad
|
|
||||||
className="h-44 w-full rounded-lg border bg-white backdrop-blur-sm dark:border-[#e2d7c5] dark:bg-[#fcf8ee]"
|
|
||||||
defaultValue={result.data?.signature ?? undefined}
|
|
||||||
onChange={(v) => onChange(v ?? '')}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormErrorMessage className="mt-1.5" error={errors.signature} />
|
|
||||||
</div>
|
|
||||||
</div> */}
|
|
||||||
|
|
||||||
<div className="mt-4">
|
<div className="mt-4">
|
||||||
<Button type="submit" disabled={isSubmitting}>
|
<Button type="submit" disabled={isSubmitting}>
|
||||||
|
|||||||
@ -0,0 +1,6 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { ZUpdateProfileMutationByAdminSchema } from '@documenso/trpc/server/admin-router/schema';
|
||||||
|
|
||||||
|
export const ZUserFormSchema = ZUpdateProfileMutationByAdminSchema.omit({ id: true });
|
||||||
|
export type TUserFormSchema = z.infer<typeof ZUserFormSchema>;
|
||||||
@ -3,13 +3,12 @@ import { Role } from '@documenso/prisma/client';
|
|||||||
|
|
||||||
export type UpdateUserOptions = {
|
export type UpdateUserOptions = {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string | null | undefined;
|
||||||
email: string;
|
email: string | undefined;
|
||||||
roles: Role[];
|
roles: Role[] | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateUser = async ({ id, name, email, roles }: UpdateUserOptions) => {
|
export const updateUser = async ({ id, name, email, roles }: UpdateUserOptions) => {
|
||||||
console.log('wtf');
|
|
||||||
await prisma.user.findFirstOrThrow({
|
await prisma.user.findFirstOrThrow({
|
||||||
where: {
|
where: {
|
||||||
id,
|
id,
|
||||||
|
|||||||
@ -3,9 +3,9 @@ import z from 'zod';
|
|||||||
|
|
||||||
export const ZUpdateProfileMutationByAdminSchema = z.object({
|
export const ZUpdateProfileMutationByAdminSchema = z.object({
|
||||||
id: z.number().min(1),
|
id: z.number().min(1),
|
||||||
name: z.string(),
|
name: z.string().nullish(),
|
||||||
email: z.string().email(),
|
email: z.string().email().optional(),
|
||||||
roles: z.array(z.nativeEnum(Role)),
|
roles: z.array(z.nativeEnum(Role)).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type TUpdateProfileMutationByAdminSchema = z.infer<
|
export type TUpdateProfileMutationByAdminSchema = z.infer<
|
||||||
|
|||||||
Reference in New Issue
Block a user