Skip to content

Commit e0b6258

Browse files
authored
Merge pull request #4928 from HSLdevcom/clean-index-page
chore: refactor IndexPage
2 parents 603c177 + 1bde751 commit e0b6258

File tree

1 file changed

+66
-65
lines changed

1 file changed

+66
-65
lines changed

app/component/IndexPage.js

+66-65
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,70 @@ class IndexPage extends React.Component {
186186
}${this.context.config.trafficNowLink[lang]}`;
187187
};
188188

189+
clickStopNearIcon = (url, kbdEvent) => {
190+
if (kbdEvent && !isKeyboardSelectionEvent(kbdEvent)) {
191+
return;
192+
}
193+
this.context.router.push(url);
194+
};
195+
196+
NearStops(CtrlPanel) {
197+
const { intl, config } = this.context;
198+
const { colors, fontWeights } = config;
199+
const { lang } = this.props;
200+
const transportModes = getTransportModes(config);
201+
const nearYouModes = getNearYouModes(config);
202+
203+
// Styles are defined by which button type is configured (narrow/wide)
204+
const narrowButtons = config.narrowNearYouButtons;
205+
const modeTitles = filterObject(
206+
transportModes,
207+
'availableForSelection',
208+
true,
209+
);
210+
// If nearYouModes is configured, display those. Otherwise, display all configured transport modes
211+
const modes =
212+
nearYouModes?.length > 0 ? nearYouModes : Object.keys(modeTitles);
213+
214+
const alertsContext = {
215+
currentTime: this.props.currentTime,
216+
getModesWithAlerts,
217+
feedIds: config.feedIds,
218+
};
219+
220+
return config.showNearYouButtons ? (
221+
<CtrlPanel.NearStopsAndRoutes
222+
modeArray={modes}
223+
urlPrefix={`/${PREFIX_NEARYOU}`}
224+
language={lang}
225+
showTitle
226+
alertsContext={alertsContext}
227+
origin={this.props.origin}
228+
omitLanguageUrl
229+
onClick={this.clickStopNearIcon}
230+
buttonStyle={narrowButtons ? undefined : config.nearYouButton}
231+
title={narrowButtons ? undefined : config.nearYouTitle}
232+
modes={narrowButtons ? undefined : modeTitles}
233+
modeSet={config.nearbyModeSet || config.iconModeSet}
234+
modeIconColors={colors.iconColors}
235+
fontWeights={fontWeights}
236+
/>
237+
) : (
238+
<div className="stops-near-you-text">
239+
<h2>
240+
{' '}
241+
{intl.formatMessage({
242+
id: 'stop-near-you-title',
243+
defaultMessage: 'Stops and lines near you',
244+
})}
245+
</h2>
246+
</div>
247+
);
248+
}
249+
189250
/* eslint-disable jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
190251
render() {
191-
const { intl, config, router } = this.context;
252+
const { intl, config } = this.context;
192253
const { trafficNowLink, colors, fontWeights } = config;
193254
const color = colors.primary;
194255
const hoverColor = colors.hover || LightenDarkenColor(colors.primary, -20);
@@ -218,12 +279,6 @@ class IndexPage extends React.Component {
218279
'MapPosition',
219280
];
220281

221-
const alertsContext = {
222-
currentTime: this.props.currentTime,
223-
getModesWithAlerts,
224-
feedIds: config.feedIds,
225-
};
226-
227282
const showSpinner =
228283
(origin.type === 'CurrentLocation' && !origin.address) ||
229284
(destination.type === 'CurrentLocation' && !destination.address);
@@ -249,7 +304,7 @@ class IndexPage extends React.Component {
249304
onGeolocationStart: this.onSelectLocation,
250305
fromMap: this.props.fromMap,
251306
fontWeights,
252-
modeIconColors: config.colors.iconColors,
307+
modeIconColors: colors.iconColors,
253308
modeSet: config.iconModeSet,
254309
};
255310

@@ -269,7 +324,7 @@ class IndexPage extends React.Component {
269324
sources,
270325
targets: stopAndRouteSearchTargets,
271326
fontWeights,
272-
modeIconColors: config.colors.iconColors,
327+
modeIconColors: colors.iconColors,
273328
modeSet: config.iconModeSet,
274329
geocodingSize: 25,
275330
};
@@ -282,60 +337,6 @@ class IndexPage extends React.Component {
282337
results.filter(config.stopSearchFilter);
283338
}
284339

285-
const transportModes = getTransportModes(config);
286-
const nearYouModes = getNearYouModes(config);
287-
288-
// TODO this probably needs refactoring as nested components are against best practices
289-
// eslint-disable-next-line react/no-unstable-nested-components
290-
const NearStops = CtrlPanel => {
291-
// Styles are defined by which button type is configured (narrow/wide)
292-
const narrowButtons = config.narrowNearYouButtons;
293-
const modeTitles = filterObject(
294-
transportModes,
295-
'availableForSelection',
296-
true,
297-
);
298-
// If nearYouModes is configured, display those. Otherwise, display all configured transport modes
299-
const modes =
300-
nearYouModes?.length > 0 ? nearYouModes : Object.keys(modeTitles);
301-
302-
const clickStopNearIcon = (url, kbdEvent) => {
303-
if (kbdEvent && !isKeyboardSelectionEvent(kbdEvent)) {
304-
return;
305-
}
306-
router.push(url);
307-
};
308-
309-
return config.showNearYouButtons ? (
310-
<CtrlPanel.NearStopsAndRoutes
311-
modeArray={modes}
312-
urlPrefix={`/${PREFIX_NEARYOU}`}
313-
language={lang}
314-
showTitle
315-
alertsContext={alertsContext}
316-
origin={origin}
317-
omitLanguageUrl
318-
onClick={clickStopNearIcon}
319-
buttonStyle={narrowButtons ? undefined : config.nearYouButton}
320-
title={narrowButtons ? undefined : config.nearYouTitle}
321-
modes={narrowButtons ? undefined : modeTitles}
322-
modeSet={config.nearbyModeSet || config.iconModeSet}
323-
modeIconColors={config.colors.iconColors}
324-
fontWeights={fontWeights}
325-
/>
326-
) : (
327-
<div className="stops-near-you-text">
328-
<h2>
329-
{' '}
330-
{intl.formatMessage({
331-
id: 'stop-near-you-title',
332-
defaultMessage: 'Stops and lines near you',
333-
})}
334-
</h2>
335-
</div>
336-
);
337-
};
338-
339340
return (
340341
<LazilyLoad modules={modules}>
341342
{({
@@ -394,7 +395,7 @@ class IndexPage extends React.Component {
394395

395396
{!config.hideStopRouteSearch && (
396397
<>
397-
<>{NearStops(CtrlPanel)}</>
398+
<>{this.NearStops(CtrlPanel)}</>
398399
<StopRouteSearch {...stopRouteSearchProps} />{' '}
399400
<CtrlPanel.SeparatorLine />
400401
</>
@@ -444,7 +445,7 @@ class IndexPage extends React.Component {
444445
isMobile
445446
/>
446447
<CtrlPanel.SeparatorLine />
447-
<>{NearStops(CtrlPanel)}</>
448+
<>{this.NearStops(CtrlPanel)}</>
448449
<div className="stop-route-search-container">
449450
<StopRouteSearch isMobile {...stopRouteSearchProps} />
450451
</div>

0 commit comments

Comments
 (0)