feat: fix code

This commit is contained in:
Catalin Pit
2024-08-21 12:15:22 +03:00
parent 5ce7f4adcc
commit bbf2ed7154

View File

@ -5,6 +5,7 @@ import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation'; import { useRouter } from 'next/navigation';
import { zodResolver } from '@hookform/resolvers/zod'; import { zodResolver } from '@hookform/resolvers/zod';
import { Loader } from 'lucide-react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc'; import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
@ -73,7 +74,11 @@ export const AddSubjectFormPartial = ({
} }
: null, : null,
); );
const [showUndoButton, setShowUndoButton] = useState(false);
const [changedFields, setChangedFields] = useState<{ subject: boolean; message: boolean }>({
subject: false,
message: false,
});
const { toast } = useToast(); const { toast } = useToast();
const router = useRouter(); const router = useRouter();
@ -95,75 +100,75 @@ export const AddSubjectFormPartial = ({
}); });
const handleOnBlur = async (field: 'subject' | 'message', value: string) => { const handleOnBlur = async (field: 'subject' | 'message', value: string) => {
if (value !== oldEmailData?.[field]) { if (value == oldEmailData?.[field]) return;
try {
await setEmailSettingsForDocument({
documentId: document.id,
[field]: value,
});
router.refresh(); try {
await setEmailSettingsForDocument({
documentId: document.id,
[field]: value,
});
toast({ setChangedFields((prev) => ({ ...prev, [field]: true }));
title: 'Email settings updated', router.refresh();
description: 'The email settings for the document have been updated.',
duration: 5000,
});
} catch (e) {
console.error(e);
toast({ toast({
title: 'Error', title: 'Email settings updated',
description: 'An error occurred while updating the email settings.', description: 'The email settings for the document have been updated.',
duration: 5000, duration: 5000,
}); });
} } catch (e) {
console.error(e);
toast({
title: 'Error',
description: 'An error occurred while updating the email settings.',
duration: 5000,
});
} }
}; };
const handleUndoButton = async () => { const handleUndoButton = async (field: 'subject' | 'message') => {
if (!oldEmailData) return;
try { try {
if (oldEmailData) { await setEmailSettingsForDocument({
await setEmailSettingsForDocument({ documentId: document.id,
documentId: document.id, [field]: oldEmailData[field],
subject: oldEmailData.subject, });
message: oldEmailData.message,
});
setValue('meta.subject', oldEmailData.subject); setValue(`meta.${field}`, oldEmailData[field]);
setValue('meta.message', oldEmailData.message);
setOldEmailData((prev) => (prev ? { ...prev, [field]: undefined } : null));
setChangedFields((prev) => ({ ...prev, [field]: false }));
router.refresh();
toast({
title: 'Change reverted',
description: `The latest change to the ${field} has been reverted.`,
duration: 5000,
});
if (oldEmailData.subject === undefined && oldEmailData.message === undefined) {
setOldEmailData(null); setOldEmailData(null);
router.refresh();
toast({
title: 'Changes reverted',
description: 'The latest change has been reverted to the original value.',
duration: 5000,
});
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);
toast({ toast({
title: 'Error', title: 'Error',
description: 'An error occurred while undoing the latest change.', description: `An error occurred while undoing the latest change to the ${field}.`,
duration: 5000, duration: 5000,
}); });
} }
}; };
useEffect(() => { useEffect(() => {
if (oldEmailData) { const timeout = setTimeout(() => {
const timeout = setTimeout(() => { setChangedFields({ subject: false, message: false });
setShowUndoButton(false); }, 5000);
}, 5000);
setShowUndoButton(true); return () => {
clearTimeout(timeout);
return () => { };
clearTimeout(timeout);
};
}
}, [oldEmailData]); }, [oldEmailData]);
const onFormSubmit = handleSubmit(onSubmit); const onFormSubmit = handleSubmit(onSubmit);
@ -200,6 +205,20 @@ export const AddSubjectFormPartial = ({
})} })}
/> />
{changedFields.subject && oldEmailData && (
<div className="mt-2 flex items-center">
<Button
className="-ml-2"
size="sm"
variant="link"
onClick={() => void handleUndoButton('subject')}
>
<span className="text-xs">Undo change</span>
<Loader className="ml-1 h-4 w-4 animate-spin text-gray-500" />
</Button>
</div>
)}
<FormErrorMessage className="mt-2" error={errors.meta?.subject} /> <FormErrorMessage className="mt-2" error={errors.meta?.subject} />
</div> </div>
@ -219,16 +238,25 @@ export const AddSubjectFormPartial = ({
})} })}
/> />
{changedFields.message && oldEmailData && (
<div className="mt-2 flex items-center">
<Button
className="-ml-2"
size="sm"
variant="link"
onClick={() => void handleUndoButton('message')}
>
<span className="text-xs">Undo change</span>
<Loader className="ml-1 h-4 w-4 animate-spin text-gray-500" />
</Button>
</div>
)}
<FormErrorMessage <FormErrorMessage
className="mt-2" className="mt-2"
error={typeof errors.meta?.message !== 'string' ? errors.meta?.message : undefined} error={typeof errors.meta?.message !== 'string' ? errors.meta?.message : undefined}
/> />
</div> </div>
{showUndoButton && oldEmailData && (
<Button onClick={() => void handleUndoButton()}>Undo latest change</Button>
)}
<DocumentSendEmailMessageHelper /> <DocumentSendEmailMessageHelper />
</div> </div>
</div> </div>