fix: enhancements

This commit is contained in:
David Nguyen
2024-04-25 22:28:41 +07:00
parent ef666b0e70
commit 39bd3e5880
3 changed files with 57 additions and 48 deletions

View File

@ -98,8 +98,8 @@ export function UseTemplateDialog({
sendDocument: false, sendDocument: false,
recipients: recipients.map((recipient) => ({ recipients: recipients.map((recipient) => ({
id: recipient.id, id: recipient.id,
name: recipient.name, name: '',
email: recipient.email, email: '',
})), })),
}, },
}); });
@ -156,15 +156,16 @@ export function UseTemplateDialog({
<DialogContent className="sm:max-w-lg"> <DialogContent className="sm:max-w-lg">
<DialogHeader> <DialogHeader>
<DialogTitle>Create document from template</DialogTitle> <DialogTitle>Create document from template</DialogTitle>
<DialogDescription>Add the recipients to create the document with</DialogDescription> <DialogDescription>
{recipients.length === 0
? 'A draft document will be created'
: 'Add the recipients to create the document with'}
</DialogDescription>
</DialogHeader> </DialogHeader>
<Form {...form}> <Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}> <form onSubmit={form.handleSubmit(onSubmit)}>
<fieldset <fieldset className="flex h-full flex-col" disabled={form.formState.isSubmitting}>
className="flex h-full flex-col space-y-4"
disabled={form.formState.isSubmitting}
>
<div className="custom-scrollbar -m-1 max-h-[60vh] space-y-4 overflow-y-auto p-1"> <div className="custom-scrollbar -m-1 max-h-[60vh] space-y-4 overflow-y-auto p-1">
{formRecipients.map((recipient, index) => ( {formRecipients.map((recipient, index) => (
<div className="flex w-full flex-row space-x-4" key={recipient.id}> <div className="flex w-full flex-row space-x-4" key={recipient.id}>
@ -201,6 +202,7 @@ export function UseTemplateDialog({
))} ))}
</div> </div>
{recipients.length > 0 && (
<div className="mt-4 flex flex-row items-center"> <div className="mt-4 flex flex-row items-center">
<FormField <FormField
control={form.control} control={form.control}
@ -241,6 +243,7 @@ export function UseTemplateDialog({
)} )}
/> />
</div> </div>
)}
<DialogFooter> <DialogFooter>
<DialogClose asChild> <DialogClose asChild>
@ -250,7 +253,11 @@ export function UseTemplateDialog({
</DialogClose> </DialogClose>
<Button type="submit" loading={form.formState.isSubmitting}> <Button type="submit" loading={form.formState.isSubmitting}>
{form.getValues('sendDocument') ? 'Send' : 'Review'} {recipients.length === 0
? 'Create'
: form.getValues('sendDocument')
? 'Send'
: 'Review'}
</Button> </Button>
</DialogFooter> </DialogFooter>
</fieldset> </fieldset>

View File

@ -56,13 +56,13 @@ export const createDocumentFromTemplate = async ({
throw new Error('Template not found.'); throw new Error('Template not found.');
} }
if (recipients.length === 0 || recipients.length !== template.Recipient.length) { if (recipients.length !== template.Recipient.length) {
throw new Error('Invalid number of recipients.'); throw new Error('Invalid number of recipients.');
} }
let finalRecipients: Pick<Recipient, 'name' | 'email' | 'role'>[] = []; let finalRecipients: Pick<Recipient, 'name' | 'email' | 'role'>[] = [];
if (Object.prototype.hasOwnProperty.call(recipients[0], 'id')) { if (recipients.length > 0 && Object.prototype.hasOwnProperty.call(recipients[0], 'id')) {
finalRecipients = template.Recipient.map((templateRecipient) => { finalRecipients = template.Recipient.map((templateRecipient) => {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const foundRecipient = (recipients as RecipientWithId[]).find( const foundRecipient = (recipients as RecipientWithId[]).find(

View File

@ -66,7 +66,9 @@ export const templateRouter = router({
userId: ctx.user.id, userId: ctx.user.id,
teamId, teamId,
requestMetadata: extractNextApiRequestMetadata(ctx.req), requestMetadata: extractNextApiRequestMetadata(ctx.req),
}).catch(() => { }).catch((err) => {
console.error(err);
throw new AppError('DOCUMENT_SEND_FAILED'); throw new AppError('DOCUMENT_SEND_FAILED');
}); });
} }