The Open Source DocuSign Alternative.
TypeScript digital-signaturedocument-signingdocusign-alternativee-signatureesignesignaturenext-authnextjsopen-sourcepades-standardpdfpdf-signpdf-signaturepostgresqlprismaself-hostedsigningtypescript
feat: implement Drag-n-Drop for templates (#1791)
Catalin Pit803edf5b1665
diff --git a/apps/remix/app/components/general/template/template-drop-zone-wrapper.tsx b/apps/remix/app/components/general/template/template-drop-zone-wrapper.tsxnew file mode 100644index 000000000..0d9c3fc9e--- /dev/null+++ b/apps/remix/app/components/general/template/template-drop-zone-wrapper.tsx@@ -0,0 +1,129 @@+import { type ReactNode, useState } from 'react';++import { msg } from '@lingui/core/macro';+import { useLingui } from '@lingui/react';+import { Trans } from '@lingui/react/macro';+import { Loader } from 'lucide-react';+import { useDropzone } from 'react-dropzone';+import { useNavigate, useParams } from 'react-router';++import { APP_DOCUMENT_UPLOAD_SIZE_LIMIT } from '@documenso/lib/constants/app';+import { megabytesToBytes } from '@documenso/lib/universal/unit-convertions';+import { putPdfFile } from '@documenso/lib/universal/upload/put-file';+import { formatTemplatesPath } from '@documenso/lib/utils/teams';+import { trpc } from '@documenso/trpc/react';+import { cn } from '@documenso/ui/lib/utils';+import { useToast } from '@documenso/ui/primitives/use-toast';++import { useCurrentTeam } from '~/providers/team';++export interface TemplateDropZoneWrapperProps {+ children: ReactNode;+ className?: string;+}++export const TemplateDropZoneWrapper = ({ children, className }: TemplateDropZoneWrapperProps) => {+ const { _ } = useLingui();+ const { toast } = useToast();+ const { folderId } = useParams();++ const team = useCurrentTeam();++ const navigate = useNavigate();++ const [isLoading, setIsLoading] = useState(false);++ const { mutateAsync: createTemplate } = trpc.template.createTemplate.useMutation();++ const onFileDrop = async (file: File) => {+ try {+ setIsLoading(true);++ const documentData = await putPdfFile(file);++ const { id } = await createTemplate({+ title: file.name,+ templateDocumentDataId: documentData.id,+ folderId: folderId ?? undefined,+ });++ toast({+ title: _(msg`Template uploaded`),+ description: _(+ msg`Your template has been uploaded successfully. You will be redirected to the template page.`,+ ),+ duration: 5000,+ });++ await navigate(`${formatTemplatesPath(team.url)}/${id}/edit`);+ } catch {+ toast({+ title: _(msg`Something went wrong`),+ description: _(msg`Please try again later.`),+ variant: 'destructive',+ });+ } finally {+ setIsLoading(false);+ }+ };++ const onFileDropRejected = () => {+ toast({+ title: _(msg`Your template failed to upload.`),+ description: _(msg`File cannot be larger than ${APP_DOCUMENT_UPLOAD_SIZE_LIMIT}MB`),+ duration: 5000,+ variant: 'destructive',+ });+ };++ const { getRootProps, getInputProps, isDragActive } = useDropzone({+ accept: {+ 'application/pdf': ['.pdf'],+ },+ //disabled: isUploadDisabled,+ multiple: false,+ maxSize: megabytesToBytes(APP_DOCUMENT_UPLOAD_SIZE_LIMIT),+ onDrop: ([acceptedFile]) => {+ if (acceptedFile) {+ void onFileDrop(acceptedFile);+ }+ },+ onDropRejected: () => {+ void onFileDropRejected();+ },+ noClick: true,+ noDragEventsBubbling: true,+ });++ return (+ <div {...getRootProps()} className={cn('relative min-h-screen', className)}>+ <input {...getInputProps()} />+ {children}++ {isDragActive && (+ <div className="bg-muted/60 fixed left-0 top-0 z-[9999] h-full w-full backdrop-blur-[4px]">+ <div className="pointer-events-none flex h-full w-full flex-col items-center justify-center">+ <h2 className="text-foreground text-2xl font-semibold">+ <Trans>Upload Template</Trans>+ </h2>++ <p className="text-muted-foreground text-md mt-4">+ <Trans>Drag and drop your PDF file here</Trans>+ </p>+ </div>+ </div>+ )}++ {isLoading && (+ <div className="bg-muted/30 absolute inset-0 z-50 backdrop-blur-[2px]">+ <div className="pointer-events-none flex h-1/2 w-full flex-col items-center justify-center">+ <Loader className="text-primary h-12 w-12 animate-spin" />+ <p className="text-foreground mt-8 font-medium">+ <Trans>Uploading template...</Trans>+ </p>+ </div>+ </div>+ )}+ </div>+ );+};diff --git a/apps/remix/app/routes/_authenticated+/t.$teamUrl+/templates._index.tsx b/apps/remix/app/routes/_authenticated+/t.$teamUrl+/templates._index.tsxindex d5751d80c..bd9f0de99 100644--- a/apps/remix/app/routes/_authenticated+/t.$teamUrl+/templates._index.tsx+++ b/apps/remix/app/routes/_authenticated+/t.$teamUrl+/templates._index.tsx@@ -12,6 +12,7 @@ import { FolderGrid } from '~/components/general/folder/folder-grid';import { TemplatesTable } from '~/components/tables/templates-table';import { useCurrentTeam } from '~/providers/team';import { appMetaTags } from '~/utils/meta';+import { TemplateDropZoneWrapper } from '~/components/general/template/template-drop-zone-wrapper';export function meta() {return appMetaTags('Templates');@@ -36,51 +37,54 @@ export default function TemplatesPage() {});return (- <div className="mx-auto max-w-screen-xl px-4 md:px-8">- <FolderGrid type={FolderType.TEMPLATE} parentId={folderId ?? null} />+ <TemplateDropZoneWrapper>+ <div className="mx-auto max-w-screen-xl px-4 md:px-8">+ <FolderGrid type={FolderType.TEMPLATE} parentId={folderId ?? null} />- <div className="mt-8">- <div className="flex flex-row items-center">- <Avatar className="dark:border-border mr-3 h-12 w-12 border-2 border-solid border-white">- {team.avatarImageId && <AvatarImage src={formatAvatarUrl(team.avatarImageId)} />}- <AvatarFallback className="text-muted-foreground text-xs">- {team.name.slice(0, 1)}- </AvatarFallback>- </Avatar>+ <div className="mt-8">+ <div className="flex flex-row items-center">+ <Avatar className="dark:border-border mr-3 h-12 w-12 border-2 border-solid border-white">+ {team.avatarImageId && <AvatarImage src={formatAvatarUrl(team.avatarImageId)} />}+ <AvatarFallback className="text-muted-foreground text-xs">+ {team.name.slice(0, 1)}+ </AvatarFallback>+ </Avatar>- <h1 className="truncate text-2xl font-semibold md:text-3xl">- <Trans>Templates</Trans>- </h1>- </div>+ <h1 className="truncate text-2xl font-semibold md:text-3xl">+ <Trans>Templates</Trans>+ </h1>+ </div>- <div className="mt-8">- {data && data.count === 0 ? (- <div className="text-muted-foreground/60 flex h-96 flex-col items-center justify-center gap-y-4">- <Bird className="h-12 w-12" strokeWidth={1.5} />+ <div className="mt-8">+ {data && data.count === 0 ? (+ <div className="text-muted-foreground/60 flex h-96 flex-col items-center justify-center gap-y-4">+ <Bird className="h-12 w-12" strokeWidth={1.5} />- <div className="text-center">- <h3 className="text-lg font-semibold">- <Trans>We're all empty</Trans>- </h3>+ <div className="text-center">+ <h3 className="text-lg font-semibold">+ <Trans>We're all empty</Trans>+ </h3>- <p className="mt-2 max-w-[50ch]">- <Trans>- You have not yet created any templates. To create a template please upload one.- </Trans>- </p>+ <p className="mt-2 max-w-[50ch]">+ <Trans>+ You have not yet created any templates. To create a template please upload+ one.+ </Trans>+ </p>+ </div></div>- </div>- ) : (- <TemplatesTable- data={data}- isLoading={isLoading}- isLoadingError={isLoadingError}- documentRootPath={documentRootPath}- templateRootPath={templateRootPath}- />- )}+ ) : (+ <TemplatesTable+ data={data}+ isLoading={isLoading}+ isLoadingError={isLoadingError}+ documentRootPath={documentRootPath}+ templateRootPath={templateRootPath}+ />+ )}+ </div></div></div>- </div>+ </TemplateDropZoneWrapper>);}diff --git a/packages/app-tests/e2e/folders/team-account-folders.spec.ts b/packages/app-tests/e2e/folders/team-account-folders.spec.tsindex 09272ee96..71649be8d 100644--- a/packages/app-tests/e2e/folders/team-account-folders.spec.ts+++ b/packages/app-tests/e2e/folders/team-account-folders.spec.ts@@ -379,10 +379,11 @@ test('[TEAMS]: can create a template inside a template folder', async ({ page }).filter({ hasText: /^Upload Template DocumentDrag & drop your PDF here\.$/ }).nth(2).click();- await page.locator('input[type="file"]').waitFor({ state: 'attached' });+ await page.locator('input[type="file"]').nth(0).waitFor({ state: 'attached' });await page.locator('input[type="file"]')+ .nth(0).setInputFiles(path.join(__dirname, '../../../assets/documenso-supporter-pledge.pdf'));await page.waitForTimeout(3000);