Skip to content

Commit 1d06ccb

Browse files
committed
feat(stop-viewer.js): Rendering list of Stop IDs
1 parent fb581ac commit 1d06ccb

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

lib/actions/api.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ const findStopResponse = createAction('FIND_STOP_RESPONSE')
293293
const findStopError = createAction('FIND_STOP_ERROR')
294294

295295
export function findStop (params) {
296+
console.log('params', params)
296297
return createQueryAction(
297298
`index/stops/${params.stopId}`,
298299
findStopResponse,
@@ -308,6 +309,18 @@ export function findStop (params) {
308309
)
309310
}
310311

312+
export function fetchStopInfo (stop) {
313+
return async function (dispatch, getState) {
314+
await dispatch(findStop({ stopId: stop.stopId }))
315+
const state = getState()
316+
317+
const fetchedStop = state.otp.transitIndex.stops[stop.stopId]
318+
const {lat, lon} = fetchedStop
319+
const radius = 250
320+
await dispatch(findNearbyStops({lat, lon, radius}, stop.stopId))
321+
}
322+
}
323+
311324
// TODO: Optionally substitute GraphQL queries? Note: this is not currently
312325
// possible because gtfsdb (the alternative transit index used by TriMet) does not
313326
// support GraphQL queries.
@@ -474,6 +487,7 @@ const findStopTimesForStopError = createAction('FIND_STOP_TIMES_FOR_STOP_ERROR')
474487
* Stop times for stop query (used in stop viewer).
475488
*/
476489
export function findStopTimesForStop (params) {
490+
console.log('findStopTimesForStop params :', params)
477491
return function (dispatch, getState) {
478492
const state = getState()
479493
dispatch(fetchingStopTimesForStop(params))
@@ -757,7 +771,7 @@ export function getTransportationNetworkCompanyRideEstimate (params) {
757771
const receivedNearbyStopsResponse = createAction('NEARBY_STOPS_RESPONSE')
758772
const receivedNearbyStopsError = createAction('NEARBY_STOPS_ERROR')
759773

760-
export function findNearbyStops (params) {
774+
export function findNearbyStops (params, parentStopId) {
761775
return createQueryAction(
762776
`index/stops?${qs.stringify({radius: 1000, ...params})}`,
763777
receivedNearbyStopsResponse,
@@ -776,7 +790,7 @@ export function findNearbyStops (params) {
776790
stops.sort((a, b) => { return a.distance - b.distance })
777791
if (params.max && stops.length > params.max) stops = stops.slice(0, params.max)
778792
}
779-
return {stops}
793+
return {parentStopId, stops}
780794
},
781795
// retrieve routes for each stop
782796
postprocess: (stops, dispatch, getState) => {
@@ -862,6 +876,7 @@ window.setInterval(() => {
862876
*/
863877

864878
function createQueryAction (endpoint, responseAction, errorAction, options = {}) {
879+
// eslint-disable-next-line complexity
865880
return async function (dispatch, getState) {
866881
const state = getState()
867882
const { config } = state.otp

lib/components/viewers/stop-viewer.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as uiActions from '../../actions/ui'
1414
import Icon from '../narrative/icon'
1515
import { getShowUserSettings, getStopViewerConfig } from '../../util/state'
1616

17+
import ChildStopViewer from './child-stop-viewer'
1718
import LiveStopTimes from './live-stop-times'
1819
import StopScheduleTable from './stop-schedule-table'
1920

@@ -72,6 +73,7 @@ class StopViewer extends Component {
7273
componentDidMount () {
7374
// Load the viewed stop in the store when the Stop Viewer first mounts
7475
this.props.findStop({ stopId: this.props.viewedStop.stopId })
76+
console.log('this.props.viewedStop.stopId', this.props.viewedStop.stopId)
7577
}
7678

7779
_toggleFavorite = () => {
@@ -225,6 +227,8 @@ class StopViewer extends Component {
225227
render () {
226228
const {
227229
autoRefreshStopTimes,
230+
childStops,
231+
findStop,
228232
findStopTimesForStop,
229233
homeTimezone,
230234
stopData,
@@ -238,7 +242,6 @@ class StopViewer extends Component {
238242
const { date, scheduleView } = this.state
239243
const { showBlockIds } = stopViewerConfig
240244
const hasStopTimesAndRoutes = !!(stopData && stopData.stopTimes && stopData.stopTimes.length > 0 && stopData.routes)
241-
242245
let contents
243246
if (!hasStopTimesAndRoutes) {
244247
contents = <div>No stop times found for date.</div>
@@ -279,6 +282,10 @@ class StopViewer extends Component {
279282
{this._renderControls()}
280283
<Scrollable>
281284
{contents}
285+
{!scheduleView &&
286+
// <RelatedStops childStops={childStops} />
287+
<ChildStopViewer childStops={childStops} findStops={findStop} />
288+
}
282289
</Scrollable>
283290
{/* Future: add stop details */}
284291
</div>
@@ -293,13 +300,17 @@ class StopViewer extends Component {
293300
const mapStateToProps = (state, ownProps) => {
294301
const showUserSettings = getShowUserSettings(state)
295302
const stopViewerConfig = getStopViewerConfig(state)
303+
const stopLookup = state.otp.transitIndex.stops
304+
const stopData = stopLookup[state.otp.ui.viewedStop.stopId]
305+
const childStops = stopData?.childStops?.map(stopId => stopLookup[stopId])
296306
return {
297307
autoRefreshStopTimes: state.otp.user.autoRefreshStopTimes,
308+
childStops,
298309
favoriteStops: state.otp.user.favoriteStops,
299310
homeTimezone: state.otp.config.homeTimezone,
300311
viewedStop: state.otp.ui.viewedStop,
301312
showUserSettings,
302-
stopData: state.otp.transitIndex.stops[state.otp.ui.viewedStop.stopId],
313+
stopData,
303314
stopViewerArriving: state.otp.config.language.stopViewerArriving,
304315
stopViewerConfig,
305316
timeFormat: getTimeFormat(state.otp.config),

lib/reducers/create-otp-reducer.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ function createOtpReducer (config) {
250250
// validate the initial state
251251
validateInitialState(initialState)
252252

253+
// eslint-disable-next-line complexity
253254
return (state = initialState, action) => {
254255
const searchId = action.payload && action.payload.searchId
255256
const requestId = action.payload && action.payload.requestId
@@ -666,16 +667,38 @@ function createOtpReducer (config) {
666667
})
667668

668669
case 'NEARBY_STOPS_RESPONSE':
670+
const {parentStopId, stops} = action.payload
669671
const stopLookup = {}
670-
action.payload.stops.forEach(s => {
672+
stops.forEach(s => {
671673
stopLookup[s.id] = s
672674
})
673-
return update(state, {
674-
location: {
675-
nearbyStops: { $set: action.payload.stops.map(s => s.id) }
676-
},
677-
transitIndex: { stops: { $merge: stopLookup } }
678-
})
675+
676+
const stopIds = stops.map(stop => stop.id) // ['12345', 'abd']
677+
console.log('action.payload :', action.payload)
678+
console.log('parentStopId :', parentStopId)
679+
console.log('stops :', stops)
680+
console.log('stopLookup[parentStopId] :', stopLookup[parentStopId])
681+
682+
if (!parentStopId) {
683+
return update(state, {
684+
location: {
685+
nearbyStops: { $set: stopIds }
686+
},
687+
transitIndex: { stops: { $merge: stopLookup } }
688+
})
689+
} else {
690+
// We need to remove the parent stop from the stop lookup to avoid overwriting it and
691+
// losing the child stops field we're adding here.
692+
delete stopLookup[parentStopId]
693+
return update(state, {
694+
transitIndex: { stops: {
695+
// For the parent stop, we want to add the nearby stops as a new child stops field
696+
[parentStopId]: {$merge: {childStops: stopIds}},
697+
// We'll keep all of the main child stop objects in the stops lookup (to match how the state currently looks)
698+
$merge: stopLookup // going to include the parentStop
699+
} }
700+
})
701+
}
679702
case 'STOPS_WITHIN_BBOX_RESPONSE':
680703
return update(state, {
681704
overlay: {
@@ -937,7 +960,7 @@ function createOtpReducer (config) {
937960
})
938961
case 'UPDATE_OVERLAY_VISIBILITY':
939962
const mapOverlays = clone(state.config.map.overlays)
940-
for (let key in action.payload) {
963+
for (const key in action.payload) {
941964
const overlay = mapOverlays.find(o => o.name === key)
942965
overlay.visible = action.payload[key]
943966
}

0 commit comments

Comments
 (0)