From f1e2b47396cb242e81774c984a24e67d2547b2ec Mon Sep 17 00:00:00 2001 From: root Date: Tue, 16 Jun 2026 13:25:41 +0000 Subject: [PATCH] fix: refetch order groups on window focus and GroupDetailPage mount to prevent stale status --- src/hooks/useOrderGroups.js | 64 +++++++++++++++++++++-------------- src/pages/GroupDetailPage.jsx | 6 ++++ 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/hooks/useOrderGroups.js b/src/hooks/useOrderGroups.js index ec4c73d..3ce9998 100644 --- a/src/hooks/useOrderGroups.js +++ b/src/hooks/useOrderGroups.js @@ -22,38 +22,49 @@ export const useOrderGroups = () => { const [isSavingDriverAssignment, setIsSavingDriverAssignment] = React.useState(false); const [isSavingStatusChange, setIsSavingStatusChange] = React.useState(false); + const loadLiveData = React.useCallback(async (showLoading = true) => { + if (showLoading) { + setIsLoading(true); + } + setLoadError(""); + + const groupsResult = await fetchOrderGroups(); + + if (!groupsResult) { + return; + } + + if (groupsResult.error) { + setLoadError(groupsResult.error?.message || "Не удалось загрузить группы доставки"); + setOrderGroups([]); + setIsLoading(false); + return; + } + + setOrderGroups(groupsResult.data || []); + setIsLoading(false); + }, []); + + // Initial load React.useEffect(() => { let cancelled = false; - - const loadLiveData = async () => { - /* Demo mode removed — always use Supabase */ - + const doLoad = async () => { setIsLoading(true); - setLoadError(""); - - const groupsResult = await fetchOrderGroups(); - - if (cancelled) { - return; - } - - if (groupsResult.error) { - setLoadError(groupsResult.error?.message || "Не удалось загрузить группы доставки"); - setOrderGroups([]); - setIsLoading(false); - return; - } - - setOrderGroups(groupsResult.data || []); - setIsLoading(false); + await loadLiveData(true); + if (cancelled) return; }; + doLoad(); + return () => { cancelled = true; }; + }, [loadLiveData]); - loadLiveData(); - - return () => { - cancelled = true; + // Refetch on window focus to keep data fresh across tabs/pages + React.useEffect(() => { + const handleFocus = () => { + loadLiveData(false); // silent refetch, no loading spinner }; - }, []); + window.addEventListener("focus", handleFocus); + return () => window.removeEventListener("focus", handleFocus); + }, [loadLiveData]); React.useEffect(() => { if (!orderGroups.length) { @@ -238,5 +249,6 @@ export const useOrderGroups = () => { isSavingStatusChange, isLoading, loadError, + refetch: loadLiveData, }; }; diff --git a/src/pages/GroupDetailPage.jsx b/src/pages/GroupDetailPage.jsx index 43803fb..62d166e 100644 --- a/src/pages/GroupDetailPage.jsx +++ b/src/pages/GroupDetailPage.jsx @@ -37,6 +37,7 @@ export const GroupDetailPage = () => { changeDeliveryStatus, saveShipmentData, isLoading, + refetch, } = useOrderGroups(); // ── Drivers ──────────────────────────────────────────────────────────────── @@ -48,6 +49,11 @@ export const GroupDetailPage = () => { } }, [groupId, setSelectedOrderGroupId]); + // Refetch data when navigating to this page to ensure fresh status + React.useEffect(() => { + refetch(false); // silent refetch, no loading spinner + }, [refetch]); + React.useEffect(() => { let cancelled = false; const load = async () => {