From 6192757a85ec3d84e2a30f89eedb019143c1ab2e Mon Sep 17 00:00:00 2001 From: Tami-Co <158340919+Tami-Co@users.noreply.github.com> Date: Wed, 13 Nov 2024 16:23:49 +0200 Subject: [PATCH 1/2] Adding the list of stops in the line profile page --- src/pages/lineProfile/LineProfile.tsx | 81 ++++++++++++++++----------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/src/pages/lineProfile/LineProfile.tsx b/src/pages/lineProfile/LineProfile.tsx index 96eccddf..3e0971f0 100644 --- a/src/pages/lineProfile/LineProfile.tsx +++ b/src/pages/lineProfile/LineProfile.tsx @@ -1,7 +1,7 @@ import Grid from '@mui/material/Unstable_Grid2' import { useTranslation } from 'react-i18next' import { useLoaderData } from 'react-router-dom' -import { useContext, useEffect, useState } from 'react' +import { useCallback, useContext, useEffect, useMemo, useState } from 'react' import moment from 'moment' import { Tooltip } from 'antd' import CircularProgress from '@mui/material/CircularProgress' @@ -15,11 +15,11 @@ import LineProfileHeader from './LineProfileHeader' import { LineProfileDetails } from './LineProfileDetails' import { Route } from './Route.interface' import Widget from 'src/shared/Widget' -import { SearchContext } from 'src/model/pageState' -import { getRoutesAsync } from 'src/api/gtfsService' -import { BusRoute } from 'src/model/busRoute' +import { SearchContext, TimelinePageState } from 'src/model/pageState' +import { getStopsForRouteAsync } from 'src/api/gtfsService' import { useSingleLineData } from 'src/hooks/useSingleLineData' import './LineProfile.scss' +import StopSelector from 'src/pages/components/StopSelector' const LineProfileWrapper = () => ( @@ -29,37 +29,40 @@ const LineProfileWrapper = () => ( const LineProfile = () => { const { t } = useTranslation() - const { - search: { timestamp }, - setSearch, - } = useContext(SearchContext) const route = useLoaderData() as Route & { message?: string } - const [availableRoutes, setAvailableRoutes] = useState(null) - const [selectedRouteKey, setSelectedRouteKey] = useState('') + const [state, setState] = useState({}) + const { stopKey, stops } = state + const { search, setSearch } = useContext(SearchContext) + const { timestamp, routes, routeKey } = search + const selectedRoute = useMemo( + () => routes?.find((route) => route.key === routeKey), + [routes, routeKey], + ) + + const selectedRouteIds = selectedRoute?.routeIds + const clearStops = useCallback(() => { + setState((current) => ({ + ...current, + stops: undefined, + stopName: undefined, + stopKey: undefined, + gtfsHitTimes: undefined, + siriHitTimes: undefined, + })) + }, [setState]) useEffect(() => { - window.scrollTo(0, 0) - }, []) + clearStops() + if (!routeKey || !selectedRouteIds) { + return + } + getStopsForRouteAsync(selectedRouteIds, moment(timestamp)) + .then((stops) => setState((current) => ({ ...current, stops: stops }))) + }, [route, routeKey, clearStops]) useEffect(() => { - const controller = new AbortController() - const signal = controller.signal - - getRoutesAsync( - moment(timestamp), - moment(timestamp), - route.operator_ref.toString(), - route.route_short_name, - signal, - ) - .then((routes) => setAvailableRoutes(routes)) - .catch((err) => { - console.error(err) - controller.abort() - }) - - return () => controller.abort() - }, [route]) + window.scrollTo(0, 0) + }, []) const { filteredPositions, @@ -91,10 +94,22 @@ const LineProfile = () => { } /> setSelectedRouteKey(routeKey)} + routes={routes ?? []} + routeKey={routeKey} + setRouteKey={(key) => setSearch((current) => ({ ...current, routeKey: key }))} /> + {stops && ( + + setState((current) => { + const stop = current.stops?.find((stop) => stop.key === key) + return { ...current, stopKey: key, stopName: stop?.name } + }) + } + /> + )}
{locationsAreLoading && ( From d5c66f99bd61429d042563224225b59ca5fcb268 Mon Sep 17 00:00:00 2001 From: Tami-Co <158340919+Tami-Co@users.noreply.github.com> Date: Mon, 25 Nov 2024 14:12:32 +0200 Subject: [PATCH 2/2] updates --- src/pages/lineProfile/LineProfile.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pages/lineProfile/LineProfile.tsx b/src/pages/lineProfile/LineProfile.tsx index 3e0971f0..0608ab49 100644 --- a/src/pages/lineProfile/LineProfile.tsx +++ b/src/pages/lineProfile/LineProfile.tsx @@ -38,7 +38,7 @@ const LineProfile = () => { () => routes?.find((route) => route.key === routeKey), [routes, routeKey], ) - + const selectedRouteIds = selectedRoute?.routeIds const clearStops = useCallback(() => { setState((current) => ({ @@ -56,8 +56,9 @@ const LineProfile = () => { if (!routeKey || !selectedRouteIds) { return } - getStopsForRouteAsync(selectedRouteIds, moment(timestamp)) - .then((stops) => setState((current) => ({ ...current, stops: stops }))) + getStopsForRouteAsync(selectedRouteIds, moment(timestamp)).then((stops) => + setState((current) => ({ ...current, stops: stops })), + ) }, [route, routeKey, clearStops]) useEffect(() => {