-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fetch facilities by line * Fix errors * Add types * Rename * Another rename * Render accessibility alerts * Improve alert rendering * Lint * Bold station names * Moving Alerts to Overview * Revert "Moving Alerts to Overview" This reverts commit aec3030. * Try Again * Update mbta_v3.py * Comments * Removing Alerts Page --------- Co-authored-by: Kevin Moses <[email protected]> Co-authored-by: Austin Houck <[email protected]> Co-authored-by: Austin Houck <[email protected]>
- Loading branch information
1 parent
3460d3e
commit d742188
Showing
31 changed files
with
381 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,82 @@ | ||
import type { AlertsResponse, OldAlert } from '../types/alerts'; | ||
import type { LineShort } from '../types/lines'; | ||
import { APP_DATA_BASE_PATH } from '../utils/constants'; | ||
import { getStationKeysFromStations } from '../utils/stations'; | ||
import { apiFetch } from './utils/fetch'; | ||
|
||
const alertsAPIConfig = { | ||
activity: 'BOARD,EXIT,RIDE', | ||
}; | ||
|
||
const accessibilityAlertsAPIConfig = { | ||
activity: 'USING_ESCALATOR,USING_WHEELCHAIR', | ||
}; | ||
|
||
export const fetchAlerts = async ( | ||
route: LineShort, | ||
line: LineShort, | ||
busRoute?: string | ||
): Promise<AlertsResponse[]> => { | ||
if (route === 'Bus' && busRoute) { | ||
if (line === 'Bus' && busRoute) { | ||
return fetchAlertsForBus(busRoute); | ||
} | ||
return fetchAlertsForLine(route); | ||
return fetchAlertsForLine(line); | ||
}; | ||
|
||
const fetchAlertsForLine = async (route: LineShort): Promise<AlertsResponse[]> => { | ||
const url = new URL(`${APP_DATA_BASE_PATH}/api/alerts`, window.location.origin); | ||
const fetchAlertsForLine = async (line: LineShort): Promise<AlertsResponse[]> => { | ||
const options = { ...alertsAPIConfig }; | ||
if (route === 'Green') { | ||
if (line === 'Green') { | ||
// route_type 0 is light rail (green line & Mattapan) | ||
options['route_type'] = '0'; | ||
} else { | ||
options['route_type'] = '1'; | ||
options['route'] = route; | ||
options['route'] = line; | ||
} | ||
Object.entries(options).forEach(([key, value]) => { | ||
url.searchParams.append(key, value.toString()); | ||
|
||
return await apiFetch({ | ||
path: '/api/alerts', | ||
options, | ||
errorMessage: `Failed to fetch alerts for line ${line}`, | ||
}); | ||
const response = await fetch(url.toString()); | ||
if (!response.ok) { | ||
throw new Error('Failed to fetch alerts'); | ||
} | ||
return await response.json(); | ||
}; | ||
|
||
const fetchAlertsForBus = async (busRoute: string): Promise<AlertsResponse[]> => { | ||
const url = new URL(`${APP_DATA_BASE_PATH}/api/alerts`, window.location.origin); | ||
const options = { ...alertsAPIConfig }; | ||
const options = { ...alertsAPIConfig, route: busRoute }; | ||
options['route_type'] = '3'; | ||
options['route'] = busRoute; | ||
Object.entries(options).forEach(([key, value]) => { | ||
url.searchParams.append(key, value.toString()); | ||
|
||
return await apiFetch({ | ||
path: '/api/alerts', | ||
options, | ||
errorMessage: `Failed to fetch alerts for bus route ${busRoute}`, | ||
}); | ||
}; | ||
|
||
export const fetchAccessibilityAlertsForLine = async ( | ||
line: LineShort | ||
): Promise<AlertsResponse[]> => { | ||
const stationKeys = getStationKeysFromStations(line); | ||
const options = { ...accessibilityAlertsAPIConfig, stop: stationKeys.join(',') }; | ||
|
||
return await apiFetch({ | ||
path: '/api/alerts', | ||
options, | ||
errorMessage: 'Failed to fetch accessibility alerts', | ||
}); | ||
const response = await fetch(url.toString()); | ||
if (!response.ok) { | ||
throw new Error('Failed to fetch alerts'); | ||
} | ||
return await response.json(); | ||
}; | ||
|
||
export const fetchHistoricalAlerts = async ( | ||
date: string | undefined, | ||
route: LineShort, | ||
line: LineShort, | ||
busRoute?: string | ||
): Promise<OldAlert[]> => { | ||
const url = new URL(`${APP_DATA_BASE_PATH}/api/alerts/${date}`, window.location.origin); | ||
const options = { route: '' }; | ||
if (route === 'Bus' && busRoute) { | ||
if (line === 'Bus' && busRoute) { | ||
options['route'] = busRoute; | ||
} else { | ||
options['route'] = route; | ||
options['route'] = line; | ||
} | ||
Object.entries(options).forEach(([key, value]) => { | ||
url.searchParams.append(key, value.toString()); | ||
|
||
return await apiFetch({ | ||
path: `/api/alerts/${date}`, | ||
options, | ||
errorMessage: 'Failed to fetch historical alerts', | ||
}); | ||
const response = await fetch(url.toString()); | ||
if (!response.ok) { | ||
throw new Error('Failed to fetch alerts'); | ||
} | ||
return await response.json(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { FacilitiesResponse } from '../types/facilities'; | ||
import type { LineShort } from '../types/lines'; | ||
import { getStationKeysFromStations } from '../utils/stations'; | ||
import { apiFetch } from './utils/fetch'; | ||
|
||
export const fetchAllElevatorsAndEscalators = async ( | ||
line: LineShort | ||
): Promise<FacilitiesResponse> => { | ||
const stationKeys = getStationKeysFromStations(line); | ||
const options = { type: 'ESCALATOR,ELEVATOR', stop: stationKeys.join(',') }; | ||
|
||
return await apiFetch({ | ||
path: '/api/facilities', | ||
options, | ||
errorMessage: 'Failed to fetch elevators and escalators', | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { fetchAllElevatorsAndEscalators } from '../facilities'; | ||
import type { LineShort } from '../../types/lines'; | ||
|
||
export const useElevatorsAndEscalators = (line: LineShort) => { | ||
return useQuery(['elevAndEsc', line], () => fetchAllElevatorsAndEscalators(line)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
import type { FetchScheduledServiceOptions } from '../types/api'; | ||
import { FetchScheduledServiceParams } from '../types/api'; | ||
import type { ScheduledService } from '../types/dataPoints'; | ||
import { APP_DATA_BASE_PATH } from '../utils/constants'; | ||
import { apiFetch } from './utils/fetch'; | ||
|
||
export const fetchScheduledService = async ( | ||
params: FetchScheduledServiceOptions | ||
options: FetchScheduledServiceOptions | ||
): Promise<ScheduledService | undefined> => { | ||
if (!params[FetchScheduledServiceParams.routeId]) return undefined; | ||
const url = new URL(`${APP_DATA_BASE_PATH}/api/scheduledservice`, window.location.origin); | ||
Object.keys(params).forEach((paramKey) => { | ||
url.searchParams.append(paramKey, params[paramKey]); | ||
}); | ||
const response = await fetch(url.toString()); | ||
if (!response.ok) throw new Error('Failed to fetch trip counts'); | ||
if (!options[FetchScheduledServiceParams.routeId]) return undefined; | ||
|
||
return await response.json(); | ||
return await apiFetch({ | ||
path: '/api/scheduledservice', | ||
options, | ||
errorMessage: 'Failed to fetch trip counts', | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
import type { FetchSpeedsOptions as FetchSpeedsOptions } from '../types/api'; | ||
import { FetchSpeedsParams as FetchSpeedsParams } from '../types/api'; | ||
import type { SpeedDataPoint } from '../types/dataPoints'; | ||
import { APP_DATA_BASE_PATH } from '../utils/constants'; | ||
import { apiFetch } from './utils/fetch'; | ||
|
||
export const fetchSpeeds = async (params: FetchSpeedsOptions): Promise<SpeedDataPoint[]> => { | ||
if (!params[FetchSpeedsParams.line]) return []; | ||
const url = new URL(`${APP_DATA_BASE_PATH}/api/speed`, window.location.origin); | ||
Object.keys(params).forEach((paramKey) => { | ||
url.searchParams.append(paramKey, params[paramKey]); | ||
}); | ||
const response = await fetch(url.toString()); | ||
if (!response.ok) throw new Error('Failed to fetch traversal times'); | ||
export const fetchSpeeds = async (options: FetchSpeedsOptions): Promise<SpeedDataPoint[]> => { | ||
if (!options[FetchSpeedsParams.line]) return []; | ||
|
||
return await response.json(); | ||
return await apiFetch({ | ||
path: '/api/speed', | ||
options, | ||
errorMessage: 'Failed to fetch traversal times', | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { APP_DATA_BASE_PATH } from '../../utils/constants'; | ||
|
||
export const apiFetch = async ({ path, options, errorMessage }) => { | ||
const url = new URL(`${APP_DATA_BASE_PATH}${path}`, window.location.origin); | ||
Object.entries(options).forEach(([key, value]: [string, any]) => { | ||
url.searchParams.append(key, value.toString()); | ||
}); | ||
const response = await fetch(url.toString()); | ||
if (!response.ok) throw new Error(errorMessage); | ||
return await response.json(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.