feat: ux improvements

This commit is contained in:
Catalin Pit
2024-08-21 08:50:24 +03:00
parent d74aca2aa6
commit 5ce7f4adcc

View File

@ -1,6 +1,6 @@
'use client'; 'use client';
import { useState } from 'react'; import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation'; import { useRouter } from 'next/navigation';
@ -73,6 +73,7 @@ export const AddSubjectFormPartial = ({
} }
: null, : null,
); );
const [showUndoButton, setShowUndoButton] = useState(false);
const { toast } = useToast(); const { toast } = useToast();
const router = useRouter(); const router = useRouter();
@ -94,6 +95,7 @@ export const AddSubjectFormPartial = ({
}); });
const handleOnBlur = async (field: 'subject' | 'message', value: string) => { const handleOnBlur = async (field: 'subject' | 'message', value: string) => {
if (value !== oldEmailData?.[field]) {
try { try {
await setEmailSettingsForDocument({ await setEmailSettingsForDocument({
documentId: document.id, documentId: document.id,
@ -104,7 +106,8 @@ export const AddSubjectFormPartial = ({
toast({ toast({
title: 'Email settings updated', title: 'Email settings updated',
description: `The email settings for the document have been updated.`, description: 'The email settings for the document have been updated.',
duration: 5000,
}); });
} catch (e) { } catch (e) {
console.error(e); console.error(e);
@ -112,8 +115,10 @@ export const AddSubjectFormPartial = ({
toast({ toast({
title: 'Error', title: 'Error',
description: 'An error occurred while updating the email settings.', description: 'An error occurred while updating the email settings.',
duration: 5000,
}); });
} }
}
}; };
const handleUndoButton = async () => { const handleUndoButton = async () => {
@ -134,6 +139,7 @@ export const AddSubjectFormPartial = ({
toast({ toast({
title: 'Changes reverted', title: 'Changes reverted',
description: 'The latest change has been reverted to the original value.', description: 'The latest change has been reverted to the original value.',
duration: 5000,
}); });
} }
} catch (e) { } catch (e) {
@ -141,10 +147,25 @@ export const AddSubjectFormPartial = ({
toast({ toast({
title: 'Error', title: 'Error',
description: 'An error occurred while undoing the latest change.', 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 onFormSubmit = handleSubmit(onSubmit);
const { currentStep, totalSteps, previousStep } = useStep(); const { currentStep, totalSteps, previousStep } = useStep();
@ -204,8 +225,7 @@ export const AddSubjectFormPartial = ({
/> />
</div> </div>
{/* Hide the button after 5 seconds */} {showUndoButton && oldEmailData && (
{oldEmailData && (
<Button onClick={() => void handleUndoButton()}>Undo latest change</Button> <Button onClick={() => void handleUndoButton()}>Undo latest change</Button>
)} )}