Skip to content

Commit

Permalink
Refactor LinesContextProvider to use CachedResource for service metri…
Browse files Browse the repository at this point in the history
…cs data
  • Loading branch information
joao-vasconcelos committed Dec 15, 2024
1 parent 5d1f61b commit b62112c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions frontend/contexts/Lines.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/* * */

import type { CachedResource } from '@carrismetropolitana/api-types/common';
import type { DemandMetricsByLine, ServiceMetrics } from '@carrismetropolitana/api-types/metrics';
import type { Line, Route } from '@carrismetropolitana/api-types/network';

Expand Down Expand Up @@ -52,7 +53,7 @@ export const LinesContextProvider = ({ children }) => {
const { data: allLinesData, isLoading: allLinesLoading } = useSWR<Line[], Error>(`${Routes.API}/lines`);
const { data: allRoutesData, isLoading: allRoutesLoading } = useSWR<Route[], Error>(`${Routes.API}/routes`);
const { data: demandByLineData, isLoading: demandByLineDataLoading } = useSWR<DemandMetricsByLine[], Error>(`${Routes.API}/metrics/demand/by_line`, { refreshInterval: 300000 });
const { data: serviceMetricsData, isLoading: serviceMetricsLoading } = useSWR<ServiceMetrics[], Error>(`${Routes.API}/metrics/service/all`);
const { data: serviceMetricsData, isLoading: serviceMetricsLoading } = useSWR<CachedResource<ServiceMetrics[]>, Error>(`${Routes.API}/metrics/service/all`);

//
// B. Handle actions
Expand All @@ -70,7 +71,7 @@ export const LinesContextProvider = ({ children }) => {
};

const getServiceMetricsByLineId = (lineId: string) => {
return serviceMetricsData?.filter(serviceMetrics => serviceMetrics.line_id === lineId);
return serviceMetricsData?.data.filter(serviceMetrics => serviceMetrics.line_id === lineId);
};

//
Expand All @@ -87,7 +88,7 @@ export const LinesContextProvider = ({ children }) => {
demand_metrics: demandByLineData || [],
lines: allLinesData || [],
routes: allRoutesData || [],
service_metrics: serviceMetricsData || [],
service_metrics: serviceMetricsData?.data || [],
},
flags: {
is_loading: allLinesLoading || allRoutesLoading || demandByLineDataLoading || serviceMetricsLoading,
Expand Down

0 comments on commit b62112c

Please sign in to comment.