fix: refetch order groups on window focus and GroupDetailPage mount to prevent stale status

This commit is contained in:
root 2026-06-16 13:25:41 +00:00
parent 37ea7d3a19
commit f1e2b47396
2 changed files with 44 additions and 26 deletions

View File

@ -22,18 +22,15 @@ export const useOrderGroups = () => {
const [isSavingDriverAssignment, setIsSavingDriverAssignment] = React.useState(false);
const [isSavingStatusChange, setIsSavingStatusChange] = React.useState(false);
React.useEffect(() => {
let cancelled = false;
const loadLiveData = async () => {
/* Demo mode removed — always use Supabase */
const loadLiveData = React.useCallback(async (showLoading = true) => {
if (showLoading) {
setIsLoading(true);
}
setLoadError("");
const groupsResult = await fetchOrderGroups();
if (cancelled) {
if (!groupsResult) {
return;
}
@ -46,15 +43,29 @@ export const useOrderGroups = () => {
setOrderGroups(groupsResult.data || []);
setIsLoading(false);
};
loadLiveData();
return () => {
cancelled = true;
};
}, []);
// Initial load
React.useEffect(() => {
let cancelled = false;
const doLoad = async () => {
setIsLoading(true);
await loadLiveData(true);
if (cancelled) return;
};
doLoad();
return () => { cancelled = true; };
}, [loadLiveData]);
// 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) {
setSelectedOrderGroupId(null);
@ -238,5 +249,6 @@ export const useOrderGroups = () => {
isSavingStatusChange,
isLoading,
loadError,
refetch: loadLiveData,
};
};

View File

@ -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 () => {