@@ -1,6 +1,7 @@
|
||||
import { router, usePage } from '@inertiajs/react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { protectDepartures } from '@/lib/unsaved-navigation';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
@@ -13,6 +14,7 @@ export function WorkContextChange() {
|
||||
const dirty = useRef(new Set<HTMLFormElement>());
|
||||
const pending = useRef(false);
|
||||
const savingForm = useRef<HTMLFormElement | null>(null);
|
||||
const destination = useRef<(() => void) | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [message, setMessage] = useState('');
|
||||
useEffect(() => {
|
||||
@@ -38,26 +40,17 @@ export function WorkContextChange() {
|
||||
if (input.form) dirty.current.add(input.form);
|
||||
}
|
||||
};
|
||||
const click = (event: MouseEvent) => {
|
||||
if (!(event.target instanceof Element)) return;
|
||||
const link = event.target.closest('a');
|
||||
if (
|
||||
!link ||
|
||||
new URL(link.href).pathname !== '/dashboard' ||
|
||||
!dirty.current.size
|
||||
)
|
||||
return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
setMessage('');
|
||||
setOpen(true);
|
||||
};
|
||||
const unload = (event: BeforeUnloadEvent) => {
|
||||
if (dirty.current.size) {
|
||||
event.preventDefault();
|
||||
event.returnValue = '';
|
||||
}
|
||||
};
|
||||
const offDepartures = protectDepartures(
|
||||
(submittedForm) =>
|
||||
[...dirty.current].some(
|
||||
(form) => form.isConnected && form !== submittedForm,
|
||||
),
|
||||
(proceed) => {
|
||||
destination.current = proceed;
|
||||
setMessage('');
|
||||
setOpen(true);
|
||||
},
|
||||
);
|
||||
const saved = (event: Event) => {
|
||||
if (!(event instanceof CustomEvent)) return;
|
||||
if (event.detail.success) dirty.current.delete(event.detail.form);
|
||||
@@ -67,7 +60,9 @@ export function WorkContextChange() {
|
||||
pending.current = false;
|
||||
dirty.current.clear();
|
||||
setOpen(false);
|
||||
router.visit('/dashboard');
|
||||
const proceed = destination.current;
|
||||
destination.current = null;
|
||||
proceed?.();
|
||||
} else {
|
||||
pending.current = false;
|
||||
setOpen(true);
|
||||
@@ -89,19 +84,16 @@ export function WorkContextChange() {
|
||||
const offNetwork = router.on('networkError', failure);
|
||||
document.addEventListener('input', changed);
|
||||
document.addEventListener('change', changed);
|
||||
document.addEventListener('click', click, true);
|
||||
window.addEventListener('beforeunload', unload);
|
||||
window.addEventListener('sgc:context-save-result', saved);
|
||||
window.addEventListener('sgc:context-dirty', formChanged);
|
||||
return () => {
|
||||
document.removeEventListener('input', changed);
|
||||
document.removeEventListener('change', changed);
|
||||
document.removeEventListener('click', click, true);
|
||||
window.removeEventListener('beforeunload', unload);
|
||||
window.removeEventListener('sgc:context-save-result', saved);
|
||||
window.removeEventListener('sgc:context-dirty', formChanged);
|
||||
offException();
|
||||
offNetwork();
|
||||
offDepartures();
|
||||
};
|
||||
}, []);
|
||||
return (
|
||||
@@ -109,8 +101,8 @@ export function WorkContextChange() {
|
||||
<DialogContent>
|
||||
<DialogTitle>Alterações por guardar</DialogTitle>
|
||||
<DialogDescription>
|
||||
Guarde as alterações antes de escolher outro contexto, ou
|
||||
descarte-as.
|
||||
Existem alterações por guardar. Pode guardar antes de sair,
|
||||
descartar as alterações ou continuar a editar.
|
||||
</DialogDescription>
|
||||
{message && <p role="alert">{message}</p>}
|
||||
<div className="flex flex-wrap gap-3">
|
||||
@@ -131,7 +123,10 @@ export function WorkContextChange() {
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (!forms[0].reportValidity()) {
|
||||
if (
|
||||
!forms[0].noValidate &&
|
||||
!forms[0].reportValidity()
|
||||
) {
|
||||
setOpen(false);
|
||||
return;
|
||||
}
|
||||
@@ -149,7 +144,9 @@ export function WorkContextChange() {
|
||||
dirty.current.clear();
|
||||
pending.current = false;
|
||||
setOpen(false);
|
||||
router.visit('/dashboard');
|
||||
const proceed = destination.current;
|
||||
destination.current = null;
|
||||
proceed?.();
|
||||
}}
|
||||
>
|
||||
Descartar
|
||||
@@ -158,10 +155,11 @@ export function WorkContextChange() {
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
pending.current = false;
|
||||
destination.current = null;
|
||||
setOpen(false);
|
||||
}}
|
||||
>
|
||||
Cancelar
|
||||
Continuar a editar
|
||||
</Button>
|
||||
</div>
|
||||
</DialogContent>
|
||||
|
||||
Reference in New Issue
Block a user