@@ -1,5 +1,6 @@
|
||||
import { Head, Link, useForm, usePage } from '@inertiajs/react';
|
||||
import { useEffect, useRef, type RefObject } from 'react';
|
||||
import { useEffect } from 'react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { FormErrors, PageTitle, Panel } from '@/components/procurement';
|
||||
@@ -79,22 +80,6 @@ type Props = {
|
||||
preview: Preview | null;
|
||||
};
|
||||
|
||||
function useGuard(ref: RefObject<HTMLFormElement | null>, dirty: boolean) {
|
||||
useEffect(() => {
|
||||
const form = ref.current;
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', { detail: { form, dirty } }),
|
||||
);
|
||||
return () => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form, dirty: false },
|
||||
}),
|
||||
);
|
||||
};
|
||||
}, [dirty, ref]);
|
||||
}
|
||||
|
||||
function Adoption({
|
||||
source,
|
||||
currency,
|
||||
@@ -226,7 +211,6 @@ function Prepared({
|
||||
})),
|
||||
};
|
||||
const form = useForm(historyKey, initial);
|
||||
const ref = useRef<HTMLFormElement>(null);
|
||||
const changed = form.data.budget_sources.some((source, i) =>
|
||||
source.allocations.some(
|
||||
(row, j) =>
|
||||
@@ -235,7 +219,7 @@ function Prepared({
|
||||
row.total !== initial.budget_sources[i]?.allocations[j]?.total,
|
||||
),
|
||||
);
|
||||
useGuard(ref, changed);
|
||||
const ref = usePreparationDirty(changed);
|
||||
return (
|
||||
<div id="order-parts-review" tabIndex={-1} className="scroll-mt-24">
|
||||
<Panel title="Conferir e emitir a ordem">
|
||||
@@ -247,6 +231,7 @@ function Prepared({
|
||||
</p>
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={changed ? 'true' : 'false'}
|
||||
className="grid min-w-0 gap-5"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
@@ -489,14 +474,14 @@ function Prepared({
|
||||
);
|
||||
}
|
||||
|
||||
export default function Parts({ candidates, preview }: Props) {
|
||||
function PartsContent({ candidates, preview }: Props) {
|
||||
const actor = usePage().props.auth?.user;
|
||||
const historyKey = `order-parts:${actor?.tenant_id}:${actor?.id}`;
|
||||
const form = useForm<{ sources: Selection[] }>(`${historyKey}:selection`, {
|
||||
sources: [],
|
||||
});
|
||||
const ref = useRef<HTMLFormElement>(null);
|
||||
useGuard(ref, form.data.sources.length > 0);
|
||||
const dirty = form.data.sources.length > 0;
|
||||
const ref = usePreparationDirty(dirty);
|
||||
useEffect(() => {
|
||||
if (preview) {
|
||||
const review = document.getElementById('order-parts-review');
|
||||
@@ -520,6 +505,7 @@ export default function Parts({ candidates, preview }: Props) {
|
||||
<Panel title="Escolher origens e quantidades">
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="grid min-w-0 gap-4"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
@@ -739,3 +725,13 @@ export default function Parts({ candidates, preview }: Props) {
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Parts(props: Props) {
|
||||
const actor = usePage().props.auth?.user;
|
||||
return (
|
||||
<PartsContent
|
||||
key={`order-parts:${actor?.tenant_id}:${actor?.id}`}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user