refactor: reorder imports and enhance event handling in product and raw material draft hooks

This commit is contained in:
Yoga Pangestu 2026-08-02 12:47:18 +07:00
parent 3039396192
commit 75f821957d
2 changed files with 30 additions and 11 deletions

View File

@ -1,10 +1,10 @@
import { useEffect, useRef } from 'react';
import { router } from '@inertiajs/react';
import {
saveProductDraft,
clearProductDraft,
saveProductDraft,
type ProductDraftData,
} from '@/lib/product-draft';
import { router } from '@inertiajs/react';
import { useEffect, useRef } from 'react';
type DraftType = 'create' | 'edit';
@ -21,9 +21,19 @@ export function useProductDraftSave(
const submittedRef = useRef(false);
useEffect(() => {
return router.on('before', () => {
submittedRef.current = true;
const offBefore = router.on('before', (event) => {
if (event.detail.visit.method !== 'get') {
submittedRef.current = true;
}
});
const offError = router.on('error', () => {
submittedRef.current = false;
});
return () => {
offBefore();
offError();
};
}, []);
useEffect(() => {

View File

@ -1,10 +1,10 @@
import { useEffect, useRef } from 'react';
import { router } from '@inertiajs/react';
import {
saveRawMaterialDraft,
clearRawMaterialDraft,
saveRawMaterialDraft,
type RawMaterialDraftData,
} from '@/lib/raw-material-draft';
import { router } from '@inertiajs/react';
import { useEffect, useRef } from 'react';
type DraftType = 'create' | 'edit';
@ -21,11 +21,20 @@ export function useRawMaterialDraftSave(
const submittedRef = useRef(false);
useEffect(() => {
return router.on('before', () => {
submittedRef.current = true;
const offBefore = router.on('before', (event) => {
if (event.detail.visit.method !== 'get') {
submittedRef.current = true;
}
});
const offError = router.on('error', () => {
submittedRef.current = false;
});
}, []);
return () => {
offBefore();
offError();
};
}, []);
useEffect(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);