@@ -2,6 +2,10 @@ 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 {
|
||||
connectedDirtyForms,
|
||||
markPreparationDirty,
|
||||
} from '@/lib/preparation-forms';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
@@ -18,7 +22,7 @@ export function WorkContextChange() {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [message, setMessage] = useState('');
|
||||
useEffect(() => {
|
||||
dirty.current.clear();
|
||||
dirty.current = new Set(connectedDirtyForms(dirty.current));
|
||||
}, [page.url]);
|
||||
useEffect(() => {
|
||||
const formChanged = (event: Event) => {
|
||||
@@ -27,6 +31,7 @@ export function WorkContextChange() {
|
||||
!(event.detail.form instanceof HTMLFormElement)
|
||||
)
|
||||
return;
|
||||
markPreparationDirty(event.detail.form, !!event.detail.dirty);
|
||||
if (event.detail.dirty) dirty.current.add(event.detail.form);
|
||||
else dirty.current.delete(event.detail.form);
|
||||
};
|
||||
@@ -37,13 +42,16 @@ export function WorkContextChange() {
|
||||
input instanceof HTMLTextAreaElement ||
|
||||
input instanceof HTMLSelectElement
|
||||
) {
|
||||
if (input.form) dirty.current.add(input.form);
|
||||
if (input.form) {
|
||||
markPreparationDirty(input.form, true);
|
||||
dirty.current.add(input.form);
|
||||
}
|
||||
}
|
||||
};
|
||||
const offDepartures = protectDepartures(
|
||||
(submittedForm) =>
|
||||
[...dirty.current].some(
|
||||
(form) => form.isConnected && form !== submittedForm,
|
||||
connectedDirtyForms(dirty.current).some(
|
||||
(form) => form !== submittedForm,
|
||||
),
|
||||
(proceed) => {
|
||||
destination.current = proceed;
|
||||
@@ -53,7 +61,11 @@ export function WorkContextChange() {
|
||||
);
|
||||
const saved = (event: Event) => {
|
||||
if (!(event instanceof CustomEvent)) return;
|
||||
if (event.detail.success) dirty.current.delete(event.detail.form);
|
||||
if (event.detail.success) {
|
||||
dirty.current.delete(event.detail.form);
|
||||
if (event.detail.form instanceof HTMLFormElement)
|
||||
markPreparationDirty(event.detail.form, false);
|
||||
}
|
||||
if (!pending.current || event.detail.form !== savingForm.current)
|
||||
return;
|
||||
if (event.detail.success) {
|
||||
@@ -108,9 +120,7 @@ export function WorkContextChange() {
|
||||
<div className="flex flex-wrap gap-3">
|
||||
<Button
|
||||
onClick={() => {
|
||||
const forms = [...dirty.current].filter(
|
||||
(form) => form.isConnected,
|
||||
);
|
||||
const forms = connectedDirtyForms(dirty.current);
|
||||
if (forms.length !== 1) {
|
||||
setMessage(
|
||||
'Existem alterações em vários formulários. Cancele a mudança e guarde cada formulário antes de continuar.',
|
||||
@@ -141,6 +151,10 @@ export function WorkContextChange() {
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
for (const form of connectedDirtyForms(
|
||||
dirty.current,
|
||||
))
|
||||
markPreparationDirty(form, false);
|
||||
dirty.current.clear();
|
||||
pending.current = false;
|
||||
setOpen(false);
|
||||
|
||||
Reference in New Issue
Block a user