-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathbatch-settings.tsx
155 lines (141 loc) · 4.65 KB
/
batch-settings.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { connect } from 'react-redux'
import { decodeQueryParams } from 'use-query-params'
import { MetroModeSelector } from '@opentripplanner/trip-form'
import { ModeButtonDefinition } from '@opentripplanner/types'
import { Search } from '@styled-icons/fa-solid/Search'
import { SyncAlt } from '@styled-icons/fa-solid/SyncAlt'
import { useIntl } from 'react-intl'
import React, { useCallback, useContext, useState } from 'react'
import * as apiActions from '../../actions/api'
import * as formActions from '../../actions/form'
import { ComponentContext } from '../../util/contexts'
import { getActiveSearch, hasValidLocation } from '../../util/state'
import { getBaseColor, getDarkenedBaseColor } from '../util/colors'
import { StyledIconWrapper } from '../util/styledIcon'
import {
addModeButtonIcon,
alertUserTripPlan,
modesQueryParamConfig,
onSettingsUpdate,
pipe,
setModeButton
} from './util'
import {
MainSettingsRow,
ModeSelectorContainer,
PlanTripButton,
TripFormButtonContainer
} from './batch-styled'
import AdvancedSettingsButton from './advanced-settings-button'
import DateTimeModal from './date-time-modal'
// TYPESCRIPT TODO: better types
type Props = {
activeSearch: any
currentQuery: any
enabledModeButtons: string[]
fillModeIcons?: boolean
modeButtonOptions: ModeButtonDefinition[]
onPlanTripClick: () => void
openAdvancedSettings: () => void
routingQuery: any
setQueryParam: (evt: any) => void
}
export function setModeButtonEnabled(enabledKeys: string[]) {
return (modeButton: ModeButtonDefinition): ModeButtonDefinition => {
return {
...modeButton,
enabled: enabledKeys.includes(modeButton.key)
}
}
}
/**
* Main panel for the batch/trip comparison form.
*/
function BatchSettings({
activeSearch,
currentQuery,
enabledModeButtons,
fillModeIcons,
modeButtonOptions,
onPlanTripClick,
openAdvancedSettings,
routingQuery,
setQueryParam
}: Props) {
const intl = useIntl()
// @ts-expect-error Context not typed
const { ModeIcon } = useContext(ComponentContext)
const processedModeButtons = modeButtonOptions.map(
pipe(addModeButtonIcon(ModeIcon), setModeButtonEnabled(enabledModeButtons))
)
const _planTrip = useCallback(() => {
alertUserTripPlan(intl, currentQuery, onPlanTripClick, routingQuery)
}, [currentQuery, intl, onPlanTripClick, routingQuery])
const baseColor = getBaseColor()
const accentColor = getDarkenedBaseColor()
return (
<MainSettingsRow className="main-settings-row">
<DateTimeModal />
<ModeSelectorContainer>
<MetroModeSelector
accentColor={baseColor}
activeHoverColor={accentColor.toHexString()}
fillModeIcons={fillModeIcons}
label={intl.formatMessage({
id: 'components.BatchSearchScreen.modeSelectorLabel'
})}
modeButtons={processedModeButtons}
onSettingsUpdate={onSettingsUpdate(setQueryParam)}
onToggleModeButton={setModeButton(
enabledModeButtons,
onSettingsUpdate(setQueryParam)
)}
/>
<TripFormButtonContainer className="trip-form-buttons">
<AdvancedSettingsButton onClick={openAdvancedSettings} />
<PlanTripButton
id="plan-trip"
onClick={_planTrip}
title={intl.formatMessage({
id: 'components.BatchSettings.planTripTooltip'
})}
>
<StyledIconWrapper style={{ fontSize: '1.6em' }}>
{hasValidLocation(currentQuery, 'from') &&
hasValidLocation(currentQuery, 'to') &&
!!activeSearch ? (
<SyncAlt />
) : (
<Search />
)}
</StyledIconWrapper>
</PlanTripButton>
</TripFormButtonContainer>
</ModeSelectorContainer>
</MainSettingsRow>
)
}
// connect to the redux store
// TODO: Typescript
const mapStateToProps = (state: any) => {
const urlSearchParams = new URLSearchParams(state.router.location.search)
const { modes } = state.otp.config
return {
activeSearch: getActiveSearch(state),
currentQuery: state.otp.currentQuery,
// TODO: Duplicated in apiv2.js
enabledModeButtons:
decodeQueryParams(modesQueryParamConfig, {
modeButtons: urlSearchParams.get('modeButtons')
})?.modeButtons ||
modes?.initialState?.enabledModeButtons ||
{},
fillModeIcons: state.otp.config.itinerary?.fillModeIcons,
modeButtonOptions: modes?.modeButtons || []
}
}
const mapDispatchToProps = {
routingQuery: apiActions.routingQuery,
setQueryParam: formActions.setQueryParam
}
export default connect(mapStateToProps, mapDispatchToProps)(BatchSettings)