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 [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,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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 () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue