mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
152 lines
4.9 KiB
TypeScript
152 lines
4.9 KiB
TypeScript
'use client';
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
import type { Field, Recipient } from '@documenso/prisma/client';
|
|
import { DocumentStatus } from '@documenso/prisma/client';
|
|
import type { DocumentWithData } from '@documenso/prisma/types/document-with-data';
|
|
|
|
import { FormErrorMessage } from '../form/form-error-message';
|
|
import { Input } from '../input';
|
|
import { Label } from '../label';
|
|
import { useStep } from '../stepper';
|
|
import { Textarea } from '../textarea';
|
|
import { type TAddSubjectFormSchema, ZAddSubjectFormSchema } from './add-subject.types';
|
|
import {
|
|
DocumentFlowFormContainerActions,
|
|
DocumentFlowFormContainerContent,
|
|
DocumentFlowFormContainerFooter,
|
|
DocumentFlowFormContainerHeader,
|
|
DocumentFlowFormContainerStep,
|
|
} from './document-flow-root';
|
|
import { ShowFieldItem } from './show-field-item';
|
|
import type { DocumentFlowStep } from './types';
|
|
|
|
export type AddSubjectFormProps = {
|
|
documentFlow: DocumentFlowStep;
|
|
recipients: Recipient[];
|
|
fields: Field[];
|
|
document: DocumentWithData;
|
|
onSubmit: (_data: TAddSubjectFormSchema) => void;
|
|
};
|
|
|
|
export const AddSubjectFormPartial = ({
|
|
documentFlow,
|
|
recipients: recipients,
|
|
fields: fields,
|
|
document,
|
|
onSubmit,
|
|
}: AddSubjectFormProps) => {
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
formState: { errors, isSubmitting },
|
|
} = useForm<TAddSubjectFormSchema>({
|
|
defaultValues: {
|
|
meta: {
|
|
subject: document.documentMeta?.subject ?? '',
|
|
message: document.documentMeta?.message ?? '',
|
|
},
|
|
},
|
|
resolver: zodResolver(ZAddSubjectFormSchema),
|
|
});
|
|
|
|
const onFormSubmit = handleSubmit(onSubmit);
|
|
const { currentStep, totalSteps, previousStep } = useStep();
|
|
|
|
return (
|
|
<>
|
|
<DocumentFlowFormContainerHeader
|
|
title={documentFlow.title}
|
|
description={documentFlow.description}
|
|
/>
|
|
<DocumentFlowFormContainerContent>
|
|
<div className="flex flex-col">
|
|
{fields.map((field, index) => (
|
|
<ShowFieldItem key={index} field={field} recipients={recipients} />
|
|
))}
|
|
|
|
<div className="flex flex-col gap-y-4">
|
|
<div>
|
|
<Label htmlFor="subject">
|
|
Subject <span className="text-muted-foreground">(Optional)</span>
|
|
</Label>
|
|
|
|
<Input
|
|
id="subject"
|
|
className="bg-background mt-2"
|
|
disabled={isSubmitting}
|
|
{...register('meta.subject')}
|
|
/>
|
|
|
|
<FormErrorMessage className="mt-2" error={errors.meta?.subject} />
|
|
</div>
|
|
|
|
<div>
|
|
<Label htmlFor="message">
|
|
Message <span className="text-muted-foreground">(Optional)</span>
|
|
</Label>
|
|
|
|
<Textarea
|
|
id="message"
|
|
className="bg-background mt-2 h-32 resize-none"
|
|
disabled={isSubmitting}
|
|
{...register('meta.message')}
|
|
/>
|
|
|
|
<FormErrorMessage
|
|
className="mt-2"
|
|
error={typeof errors.meta?.message !== 'string' ? errors.meta?.message : undefined}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<p className="text-muted-foreground text-sm">
|
|
You can use the following variables in your message:
|
|
</p>
|
|
|
|
<ul className="mt-2 flex list-inside list-disc flex-col gap-y-2 text-sm">
|
|
<li className="text-muted-foreground">
|
|
<code className="text-muted-foreground bg-muted-foreground/20 rounded p-1 text-sm">
|
|
{'{signer.name}'}
|
|
</code>{' '}
|
|
- The signer's name
|
|
</li>
|
|
<li className="text-muted-foreground">
|
|
<code className="text-muted-foreground bg-muted-foreground/20 rounded p-1 text-sm">
|
|
{'{signer.email}'}
|
|
</code>{' '}
|
|
- The signer's email
|
|
</li>
|
|
<li className="text-muted-foreground">
|
|
<code className="text-muted-foreground bg-muted-foreground/20 rounded p-1 text-sm">
|
|
{'{document.name}'}
|
|
</code>{' '}
|
|
- The document's name
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</DocumentFlowFormContainerContent>
|
|
|
|
<DocumentFlowFormContainerFooter>
|
|
<DocumentFlowFormContainerStep
|
|
title={documentFlow.title}
|
|
step={currentStep}
|
|
maxStep={totalSteps}
|
|
/>
|
|
|
|
<DocumentFlowFormContainerActions
|
|
loading={isSubmitting}
|
|
disabled={isSubmitting}
|
|
goNextLabel={document.status === DocumentStatus.DRAFT ? 'Send' : 'Update'}
|
|
onGoBackClick={previousStep}
|
|
onGoNextClick={() => void onFormSubmit()}
|
|
/>
|
|
</DocumentFlowFormContainerFooter>
|
|
</>
|
|
);
|
|
};
|