diff --git a/src/pages/nearle/riders/riders.js b/src/pages/nearle/riders/riders.js index 57afa5a..9272a45 100644 --- a/src/pages/nearle/riders/riders.js +++ b/src/pages/nearle/riders/riders.js @@ -235,6 +235,7 @@ const Riders = () => { OpenToast(err.response?.data?.message || err.message || 'Failed to delete substitution.', 'error', 2000); } finally { queryClient.invalidateQueries({ queryKey: ['substitutionsHistory'] }); + queryClient.invalidateQueries({ queryKey: ['activeSubstitutionsToday'] }); } } }; @@ -265,6 +266,7 @@ const Riders = () => { setEditingSub(null); setNewSubRider(null); queryClient.invalidateQueries({ queryKey: ['substitutionsHistory'] }); + queryClient.invalidateQueries({ queryKey: ['activeSubstitutionsToday'] }); } }; @@ -296,6 +298,7 @@ const Riders = () => { localStorage.removeItem('rider_substitutions'); queryClient.invalidateQueries({ queryKey: ['substitutionsHistory'] }); queryClient.invalidateQueries({ queryKey: ['subRidersLogs'] }); + queryClient.invalidateQueries({ queryKey: ['activeSubstitutionsToday'] }); }; const { data: substituteRidersList } = useQuery({ @@ -395,6 +398,51 @@ const Riders = () => { } }); + const { data: activeSubsToday, isLoading: activeSubsTodayLoading } = useQuery({ + queryKey: ['activeSubstitutionsToday', appId, selectedDate.format('YYYY-MM-DD')], + queryFn: async () => { + try { + let partnerId = ''; + try { + const rawLocs = localStorage.getItem('applocations'); + if (rawLocs) { + const locs = JSON.parse(rawLocs); + const currentLoc = locs.find((l) => l.applocationid === appId); + if (currentLoc && currentLoc.partnerid) { + partnerId = currentLoc.partnerid; + } else { + const firstValidLoc = locs.find((l) => l.partnerid); + if (firstValidLoc && firstValidLoc.partnerid) { + partnerId = firstValidLoc.partnerid; + } + } + } + } catch (e) { + console.error('Error parsing applocations', e); + } + + if (!partnerId) { + const savedPartnerId = localStorage.getItem('partnerid'); + if (savedPartnerId && savedPartnerId !== 'undefined' && savedPartnerId !== 'null') { + partnerId = savedPartnerId; + } + } + + if (!partnerId || partnerId === '0' || partnerId === 0) { + partnerId = 44; + } + + const dateStr = selectedDate.format('YYYY-MM-DD'); + const url = `${process.env.REACT_APP_URL}/substitutions?tenant_id=${partnerId}&from_date=${dateStr}&to_date=${dateStr}`; + const res = await axios.get(url); + return res.data.details || []; + } catch (err) { + console.error(err); + return []; + } + } + }); + const { data: subHistoryData, isLoading: subHistoryLoading } = useQuery({ queryKey: ['substitutionsHistory', appId, historyDate, historyStatus], queryFn: async () => { @@ -440,8 +488,7 @@ const Riders = () => { console.error(err); return []; } - }, - enabled: tabvalue === 3 + } }); Geocode.setApiKey(process.env.REACT_APP_GOOGLE_MAPS_API_KEY); @@ -632,10 +679,15 @@ const Riders = () => { const Icon = t.icon; const active = tabvalue === t.key; const count = t.key === 2 - ? Object.keys(substituteAssignments).filter(k => substituteAssignments[k]).length + ? (subRidersLogsData?.length || 0) : t.key === 3 - ? 0 + ? (subHistoryData?.length || 0) : (allRidersSummary?.[t.countKey] ?? 0); + const countLoading = t.key === 2 + ? subRidersLogsLoading + : t.key === 3 + ? subHistoryLoading + : riderSummarysLoading; return ( { - {t.label} + {t.label} - {t.key !== 3 && ( - - {riderSummarysLoading ? : count} - - )} + + {countLoading ? : count} + ); })}