Files
sign/apps/web/src/components/forms/profile.tsx

156 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-06-09 18:21:18 +10:00
'use client';
2023-08-17 19:56:18 +10:00
import { useRouter } from 'next/navigation';
2023-06-09 18:21:18 +10:00
import { zodResolver } from '@hookform/resolvers/zod';
2024-08-27 20:34:39 +09:00
import { Trans, msg } from '@lingui/macro';
import { useLingui } from '@lingui/react';
2023-11-30 15:55:29 +05:30
import { useForm } from 'react-hook-form';
2023-06-09 18:21:18 +10:00
import { z } from 'zod';
2023-12-22 01:46:41 +00:00
import type { User } from '@documenso/prisma/client';
2023-06-09 18:21:18 +10:00
import { TRPCClientError } from '@documenso/trpc/client';
import { trpc } from '@documenso/trpc/react';
import { cn } from '@documenso/ui/lib/utils';
import { Button } from '@documenso/ui/primitives/button';
2023-11-30 15:55:29 +05:30
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@documenso/ui/primitives/form/form';
2023-06-09 18:21:18 +10:00
import { Input } from '@documenso/ui/primitives/input';
import { Label } from '@documenso/ui/primitives/label';
import { SignaturePad } from '@documenso/ui/primitives/signature-pad';
2023-06-09 18:21:18 +10:00
import { useToast } from '@documenso/ui/primitives/use-toast';
export const ZProfileFormSchema = z.object({
name: z.string().trim().min(1, { message: 'Please enter a valid name.' }),
signature: z.string().min(1, 'Signature Pad cannot be empty'),
2023-06-09 18:21:18 +10:00
});
export const ZTwoFactorAuthTokenSchema = z.object({
token: z.string(),
});
export type TTwoFactorAuthTokenSchema = z.infer<typeof ZTwoFactorAuthTokenSchema>;
2023-06-09 18:21:18 +10:00
export type TProfileFormSchema = z.infer<typeof ZProfileFormSchema>;
export type ProfileFormProps = {
className?: string;
user: User;
};
export const ProfileForm = ({ className, user }: ProfileFormProps) => {
2023-08-17 19:56:18 +10:00
const router = useRouter();
2024-08-27 20:34:39 +09:00
const { _ } = useLingui();
2023-06-09 18:21:18 +10:00
const { toast } = useToast();
2023-11-30 15:55:29 +05:30
const form = useForm<TProfileFormSchema>({
2023-06-09 18:21:18 +10:00
values: {
name: user.name ?? '',
2023-09-04 22:24:42 +05:30
signature: user.signature || '',
2023-06-09 18:21:18 +10:00
},
resolver: zodResolver(ZProfileFormSchema),
});
2023-11-30 15:55:29 +05:30
const isSubmitting = form.formState.isSubmitting;
2023-06-09 18:21:18 +10:00
const { mutateAsync: updateProfile } = trpc.profile.updateProfile.useMutation();
const onFormSubmit = async ({ name, signature }: TProfileFormSchema) => {
try {
await updateProfile({
name,
signature,
});
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`Profile updated`),
description: _(msg`Your profile has been updated successfully.`),
2023-06-09 18:21:18 +10:00
duration: 5000,
});
2023-08-17 19:56:18 +10:00
router.refresh();
2023-06-09 18:21:18 +10:00
} catch (err) {
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`An error occurred`),
2023-06-09 18:21:18 +10:00
description: err.message,
variant: 'destructive',
});
} else {
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`An unknown error occurred`),
description: _(
msg`We encountered an unknown error while attempting to sign you In. Please try again later.`,
),
2023-06-09 18:21:18 +10:00
variant: 'destructive',
});
}
}
};
return (
2023-11-30 15:55:29 +05:30
<Form {...form}>
<form
className={cn('flex w-full flex-col gap-y-4', className)}
onSubmit={form.handleSubmit(onFormSubmit)}
>
<fieldset className="flex w-full flex-col gap-y-4" disabled={isSubmitting}>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
2024-08-27 20:34:39 +09:00
<FormLabel>
<Trans>Full Name</Trans>
</FormLabel>
2023-11-30 15:55:29 +05:30
<FormControl>
<Input type="text" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div>
<Label htmlFor="email" className="text-muted-foreground">
2024-08-27 20:34:39 +09:00
<Trans>Email</Trans>
2023-11-30 15:55:29 +05:30
</Label>
<Input id="email" type="email" className="bg-muted mt-2" value={user.email} disabled />
</div>
<FormField
control={form.control}
2023-06-09 18:21:18 +10:00
name="signature"
render={({ field: { onChange } }) => (
2023-11-30 15:55:29 +05:30
<FormItem>
2024-08-27 20:34:39 +09:00
<FormLabel>
<Trans>Signature</Trans>
</FormLabel>
2023-11-30 15:55:29 +05:30
<FormControl>
<SignaturePad
className="h-44 w-full"
disabled={isSubmitting}
containerClassName={cn('rounded-lg border bg-background')}
2023-11-30 15:55:29 +05:30
defaultValue={user.signature ?? undefined}
onChange={(v) => onChange(v ?? '')}
/>
</FormControl>
<FormMessage />
</FormItem>
2023-06-09 18:21:18 +10:00
)}
/>
2023-11-30 15:55:29 +05:30
</fieldset>
2023-06-09 18:21:18 +10:00
<Button type="submit" loading={isSubmitting} className="self-end">
2024-08-27 20:34:39 +09:00
{isSubmitting ? <Trans>Updating profile...</Trans> : <Trans>Update profile</Trans>}
2023-06-09 18:21:18 +10:00
</Button>
2023-11-30 15:55:29 +05:30
</form>
</Form>
2023-06-09 18:21:18 +10:00
);
};