fix: refetch order groups on window focus and GroupDetailPage mount to prevent stale status
This commit is contained in:
parent
37ea7d3a19
commit
f1e2b47396
|
|
@ -22,38 +22,49 @@ export const useOrderGroups = () => {
|
||||||
const [isSavingDriverAssignment, setIsSavingDriverAssignment] = React.useState(false);
|
const [isSavingDriverAssignment, setIsSavingDriverAssignment] = React.useState(false);
|
||||||
const [isSavingStatusChange, setIsSavingStatusChange] = 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(() => {
|
React.useEffect(() => {
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
|
const doLoad = async () => {
|
||||||
const loadLiveData = async () => {
|
|
||||||
/* Demo mode removed — always use Supabase */
|
|
||||||
|
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setLoadError("");
|
await loadLiveData(true);
|
||||||
|
if (cancelled) return;
|
||||||
const groupsResult = await fetchOrderGroups();
|
|
||||||
|
|
||||||
if (cancelled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (groupsResult.error) {
|
|
||||||
setLoadError(groupsResult.error?.message || "Не удалось загрузить группы доставки");
|
|
||||||
setOrderGroups([]);
|
|
||||||
setIsLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setOrderGroups(groupsResult.data || []);
|
|
||||||
setIsLoading(false);
|
|
||||||
};
|
};
|
||||||
|
doLoad();
|
||||||
|
return () => { cancelled = true; };
|
||||||
|
}, [loadLiveData]);
|
||||||
|
|
||||||
loadLiveData();
|
// Refetch on window focus to keep data fresh across tabs/pages
|
||||||
|
React.useEffect(() => {
|
||||||
return () => {
|
const handleFocus = () => {
|
||||||
cancelled = true;
|
loadLiveData(false); // silent refetch, no loading spinner
|
||||||
};
|
};
|
||||||
}, []);
|
window.addEventListener("focus", handleFocus);
|
||||||
|
return () => window.removeEventListener("focus", handleFocus);
|
||||||
|
}, [loadLiveData]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (!orderGroups.length) {
|
if (!orderGroups.length) {
|
||||||
|
|
@ -238,5 +249,6 @@ export const useOrderGroups = () => {
|
||||||
isSavingStatusChange,
|
isSavingStatusChange,
|
||||||
isLoading,
|
isLoading,
|
||||||
loadError,
|
loadError,
|
||||||
|
refetch: loadLiveData,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ export const GroupDetailPage = () => {
|
||||||
changeDeliveryStatus,
|
changeDeliveryStatus,
|
||||||
saveShipmentData,
|
saveShipmentData,
|
||||||
isLoading,
|
isLoading,
|
||||||
|
refetch,
|
||||||
} = useOrderGroups();
|
} = useOrderGroups();
|
||||||
|
|
||||||
// ── Drivers ────────────────────────────────────────────────────────────────
|
// ── Drivers ────────────────────────────────────────────────────────────────
|
||||||
|
|
@ -48,6 +49,11 @@ export const GroupDetailPage = () => {
|
||||||
}
|
}
|
||||||
}, [groupId, setSelectedOrderGroupId]);
|
}, [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(() => {
|
React.useEffect(() => {
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
const load = async () => {
|
const load = async () => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue