From 5ce7f4adcccb2e327580ab0229c10e8c14f10615 Mon Sep 17 00:00:00 2001 From: Catalin Pit <25515812+catalinpit@users.noreply.github.com> Date: Wed, 21 Aug 2024 08:50:24 +0300 Subject: [PATCH] feat: ux improvements --- .../primitives/document-flow/add-subject.tsx | 58 +++++++++++++------ 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/packages/ui/primitives/document-flow/add-subject.tsx b/packages/ui/primitives/document-flow/add-subject.tsx index 946e3f794..770566b18 100644 --- a/packages/ui/primitives/document-flow/add-subject.tsx +++ b/packages/ui/primitives/document-flow/add-subject.tsx @@ -1,6 +1,6 @@ 'use client'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; @@ -73,6 +73,7 @@ export const AddSubjectFormPartial = ({ } : null, ); + const [showUndoButton, setShowUndoButton] = useState(false); const { toast } = useToast(); const router = useRouter(); @@ -94,25 +95,29 @@ export const AddSubjectFormPartial = ({ }); const handleOnBlur = async (field: 'subject' | 'message', value: string) => { - try { - await setEmailSettingsForDocument({ - documentId: document.id, - [field]: value, - }); + if (value !== oldEmailData?.[field]) { + try { + await setEmailSettingsForDocument({ + documentId: document.id, + [field]: value, + }); - router.refresh(); + router.refresh(); - toast({ - title: 'Email settings updated', - description: `The email settings for the document have been updated.`, - }); - } catch (e) { - console.error(e); + toast({ + title: 'Email settings updated', + description: 'The email settings for the document have been updated.', + duration: 5000, + }); + } catch (e) { + console.error(e); - toast({ - title: 'Error', - description: 'An error occurred while updating the email settings.', - }); + toast({ + title: 'Error', + description: 'An error occurred while updating the email settings.', + duration: 5000, + }); + } } }; @@ -134,6 +139,7 @@ export const AddSubjectFormPartial = ({ toast({ title: 'Changes reverted', description: 'The latest change has been reverted to the original value.', + duration: 5000, }); } } catch (e) { @@ -141,10 +147,25 @@ export const AddSubjectFormPartial = ({ toast({ title: 'Error', description: 'An error occurred while undoing the latest change.', + duration: 5000, }); } }; + useEffect(() => { + if (oldEmailData) { + const timeout = setTimeout(() => { + setShowUndoButton(false); + }, 5000); + + setShowUndoButton(true); + + return () => { + clearTimeout(timeout); + }; + } + }, [oldEmailData]); + const onFormSubmit = handleSubmit(onSubmit); const { currentStep, totalSteps, previousStep } = useStep(); @@ -204,8 +225,7 @@ export const AddSubjectFormPartial = ({ /> - {/* Hide the button after 5 seconds */} - {oldEmailData && ( + {showUndoButton && oldEmailData && ( )}