Skip to content

Commit

Permalink
Handle new updated_on date in json (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
devinmatte authored Dec 19, 2023
1 parent f627e66 commit ebecaca
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
14 changes: 11 additions & 3 deletions common/api/slowzones.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import type { DayDelayTotals, SlowZoneResponse, SpeedRestriction } from '../types/dataPoints';
import type {
DayDelayTotals,
SlowZoneAllSlowResponse,
SlowZoneDayTotalsResponse,
SlowZoneResponse,
SpeedRestriction,
} from '../types/dataPoints';
import type { FetchSpeedRestrictionsOptions, FetchSpeedRestrictionsResponse } from '../types/api';
import { getGtfsRailLineId } from '../utils/lines';
import { apiFetch } from './utils/fetch';

export const fetchDelayTotals = (): Promise<DayDelayTotals[]> => {
// TODO: Remove the Array option once the slowzone change is mature
export const fetchDelayTotals = (): Promise<SlowZoneDayTotalsResponse | DayDelayTotals[]> => {
const url = new URL(`/static/slowzones/delay_totals.json`, window.location.origin);
return fetch(url.toString()).then((resp) => resp.json());
};

export const fetchAllSlow = (): Promise<SlowZoneResponse[]> => {
// TODO: Remove the Array option once the slowzone change is mature
export const fetchAllSlow = (): Promise<SlowZoneAllSlowResponse | SlowZoneResponse[]> => {
const all_slow_url = new URL(`/static/slowzones/all_slow.json`, window.location.origin);
return fetch(all_slow_url.toString()).then((resp) => resp.json());
};
Expand Down
10 changes: 10 additions & 0 deletions common/types/dataPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ export type SlowZoneResponse = {
to_id: string;
};

export interface SlowZoneAllSlowResponse {
data: SlowZoneResponse[];
updated_on: string;
}

export interface SlowZoneDayTotalsResponse {
data: DayDelayTotals[];
updated_on: string;
}

export interface SpeedDataPoint {
count: number;
date: string;
Expand Down
7 changes: 4 additions & 3 deletions modules/slowzones/SlowZonesDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useState } from 'react';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

import { isArray } from 'lodash';
import { useDelimitatedRoute } from '../../common/utils/router';
import { WidgetTitle } from '../dashboard/WidgetTitle';
import { ChartPlaceHolder } from '../../common/components/graphics/ChartPlaceHolder';
Expand Down Expand Up @@ -63,7 +64,7 @@ export function SlowZonesDetails() {
<div className="relative flex flex-col">
{totalSlowTimeReady ? (
<TotalSlowTimeWrapper
data={delayTotals.data}
data={isArray(delayTotals.data) ? delayTotals.data : delayTotals.data.data}
startDateUTC={startDateUTC}
endDateUTC={endDateUTC}
line={line}
Expand All @@ -82,7 +83,7 @@ export function SlowZonesDetails() {
{allSlow.data && speedRestrictions.data && canShowSlowZonesMap ? (
<SlowZonesMap
key={lineShort}
slowZones={allSlow.data}
slowZones={isArray(allSlow.data) ? allSlow.data : allSlow.data.data}
speedRestrictions={speedRestrictions.data}
lineName={lineShort}
direction="horizontal-on-desktop"
Expand All @@ -105,7 +106,7 @@ export function SlowZonesDetails() {
<div className="relative flex flex-col">
{segmentsReady ? (
<SlowZonesSegmentsWrapper
data={allSlow.data}
data={isArray(allSlow.data) ? allSlow.data : allSlow.data.data}
lineShort={lineShort}
linePath={linePath}
endDateUTC={endDateUTC}
Expand Down
3 changes: 2 additions & 1 deletion modules/slowzones/SlowZonesWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React from 'react';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { isArray } from 'lodash';
import { useDelimitatedRoute } from '../../common/utils/router';
import { HomescreenWidgetTitle } from '../dashboard/HomescreenWidgetTitle';
import { ChartPlaceHolder } from '../../common/components/graphics/ChartPlaceHolder';
Expand Down Expand Up @@ -31,7 +32,7 @@ export const SlowZonesWidget: React.FC = () => {
<HomescreenWidgetTitle title="Slow zones" tab="slowzones" />
{totalSlowTimeReady ? (
<TotalSlowTimeWrapper
data={delayTotals.data}
data={isArray(delayTotals.data) ? delayTotals.data : delayTotals.data.data}
startDateUTC={startDateUTC}
endDateUTC={endDateUTC}
line={line}
Expand Down
11 changes: 8 additions & 3 deletions modules/slowzones/SystemSlowZonesDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dayjs from 'dayjs';
import React, { useMemo, useState } from 'react';

import { isArray } from 'lodash';
import {
useSlowzoneAllData,
useSlowzoneDelayTotalData,
Expand Down Expand Up @@ -51,7 +52,11 @@ export function SystemSlowZonesDetails({ showTitle = false }: SystemSlowZonesDet
const lineSegmentsReady = !allData.isError && allData.data && startDateUTC && endDateUTC;
const graphData = useMemo(() => {
if (allData.data && startDateUTC && endDateUTC) {
const fitleredData = filterAllSlow(allData.data, startDateUTC, endDateUTC);
const fitleredData = filterAllSlow(
isArray(allData.data) ? allData.data : allData.data.data,
startDateUTC,
endDateUTC
);
return formatSegments(fitleredData, startDateUTC, direction);
} else return [];
}, [allData.data, endDateUTC, startDateUTC, direction]);
Expand All @@ -66,7 +71,7 @@ export function SystemSlowZonesDetails({ showTitle = false }: SystemSlowZonesDet
<div className="relative flex flex-col">
{totalSlowTimeReady ? (
<TotalSlowTime
data={delayTotals.data}
data={isArray(delayTotals.data) ? delayTotals.data : delayTotals.data.data}
startDateUTC={startDateUTC}
endDateUTC={endDateUTC}
showTitle={showTitle}
Expand All @@ -84,7 +89,7 @@ export function SystemSlowZonesDetails({ showTitle = false }: SystemSlowZonesDet
{allData.data && speedRestrictions.data && canShowSlowZonesMap ? (
<SlowZonesMap
key={lineShort}
slowZones={allData.data}
slowZones={isArray(allData.data) ? allData.data : allData.data.data}
speedRestrictions={speedRestrictions.data}
lineName={lineShort}
direction="horizontal-on-desktop"
Expand Down

0 comments on commit ebecaca

Please sign in to comment.