feat: show confirm dialog when editing case or steps

This commit is contained in:
Takeshi Kimata
2024-06-22 22:54:03 +09:00
parent f86b2a38cb
commit c652502277
6 changed files with 42 additions and 2 deletions

View File

@@ -0,0 +1,30 @@
import { useEffect } from 'react';
export const useFormGuard = (isDirty: boolean, confirmText: string) => {
useEffect(() => {
console.log(isDirty);
const handleClick = (event: MouseEvent) => {
if (isDirty && event.target instanceof Element && event.target.closest('a:not([target="_blank"]')) {
if (!window.confirm(confirmText)) {
event.preventDefault();
event.stopPropagation();
}
}
};
const handleBeforeUnload = (event: BeforeUnloadEvent) => {
if (isDirty) {
event.preventDefault();
return (event.returnValue = '');
}
};
window.addEventListener('beforeunload', handleBeforeUnload);
window.addEventListener('click', handleClick, true);
return () => {
window.removeEventListener('beforeunload', handleBeforeUnload);
window.removeEventListener('click', handleClick, true);
};
}, [isDirty]);
};