From 75f821957d9174d522e11a33ca54ca6badfcd282 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Sun, 2 Aug 2026 12:47:18 +0700 Subject: [PATCH] refactor: reorder imports and enhance event handling in product and raw material draft hooks --- resources/js/hooks/use-product-draft.ts | 20 ++++++++++++++----- resources/js/hooks/use-raw-material-draft.ts | 21 ++++++++++++++------ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/resources/js/hooks/use-product-draft.ts b/resources/js/hooks/use-product-draft.ts index 9194827..dde26e2 100644 --- a/resources/js/hooks/use-product-draft.ts +++ b/resources/js/hooks/use-product-draft.ts @@ -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(() => { diff --git a/resources/js/hooks/use-raw-material-draft.ts b/resources/js/hooks/use-raw-material-draft.ts index 5d74d87..d74cc06 100644 --- a/resources/js/hooks/use-raw-material-draft.ts +++ b/resources/js/hooks/use-raw-material-draft.ts @@ -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);