Files
sign/apps/remix/app/components/forms/profile.tsx

145 lines
4.4 KiB
TypeScript
Raw Normal View History

2023-06-09 18:21:18 +10:00
import { zodResolver } from '@hookform/resolvers/zod';
2025-01-02 15:33:37 +11:00
import { msg } from '@lingui/core/macro';
2024-08-27 20:34:39 +09:00
import { useLingui } from '@lingui/react';
2025-01-02 15:33:37 +11:00
import { Trans } from '@lingui/react/macro';
2023-11-30 15:55:29 +05:30
import { useForm } from 'react-hook-form';
2025-01-02 15:33:37 +11:00
import { useRevalidator } from 'react-router';
2023-06-09 18:21:18 +10:00
import { z } from 'zod';
2025-01-02 15:33:37 +11:00
import { useSession } from '@documenso/lib/client-only/providers/session';
2023-06-09 18:21:18 +10:00
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;
};
2025-01-02 15:33:37 +11:00
export const ProfileForm = ({ className }: ProfileFormProps) => {
2024-08-27 20:34:39 +09:00
const { _ } = useLingui();
2023-06-09 18:21:18 +10:00
const { toast } = useToast();
2025-01-02 15:33:37 +11:00
const { user } = useSession();
const { revalidate } = useRevalidator();
2023-06-09 18:21:18 +10:00
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
2025-01-02 15:33:37 +11:00
await revalidate();
2023-06-09 18:21:18 +10:00
} catch (err) {
toast({
title: _(msg`An unknown error occurred`),
description: _(
msg`We encountered an unknown error while attempting update your profile. Please try again later.`,
),
variant: 'destructive',
});
2023-06-09 18:21:18 +10:00
}
};
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 ?? '')}
allowTypedSignature={true}
2023-11-30 15:55:29 +05:30
/>
</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
);
};