Skip to content

Commit 702520b

Browse files
authored
Merge pull request #185 from opentripplanner/update-overlay-visibility
fix(default-map): Bring back vehicle rental overlay visibility support
2 parents ded87b0 + 1469ede commit 702520b

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Diff for: lib/components/map/default-map.js

+63
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
carRentalQuery,
99
vehicleRentalQuery
1010
} from '../../actions/api'
11+
import { updateOverlayVisibility } from '../../actions/config'
1112
import {
1213
setLocation,
1314
setMapPopupLocation,
@@ -42,6 +43,57 @@ const MapContainer = styled.div`
4243
`
4344

4445
class DefaultMap extends Component {
46+
/**
47+
* Checks whether the modes have changed between old and new queries and
48+
* whether to update the map overlays accordingly (e.g., to show rental vehicle
49+
* options on the map).
50+
*/
51+
_handleQueryChange = (oldQuery, newQuery) => {
52+
const { overlays } = this.props
53+
if (overlays && oldQuery.mode) {
54+
// Determine any added/removed modes
55+
const oldModes = oldQuery.mode.split(',')
56+
const newModes = newQuery.mode.split(',')
57+
const removed = oldModes.filter(m => !newModes.includes(m))
58+
const added = newModes.filter(m => !oldModes.includes(m))
59+
const overlayVisibility = {}
60+
for (const oConfig of overlays) {
61+
if (!oConfig.modes || oConfig.modes.length !== 1) continue
62+
// TODO: support multi-mode overlays
63+
const overlayMode = oConfig.modes[0]
64+
65+
if (
66+
(
67+
overlayMode === 'CAR_RENT' ||
68+
overlayMode === 'CAR_HAIL' ||
69+
overlayMode === 'MICROMOBILITY_RENT'
70+
) &&
71+
oConfig.companies
72+
) {
73+
// Special handling for company-based mode overlays (e.g. carshare, car-hail)
74+
const overlayCompany = oConfig.companies[0] // TODO: handle multi-company overlays
75+
if (added.includes(overlayMode)) {
76+
// Company-based mode was just selected; enable overlay iff overlay's company is active
77+
if (newQuery.companies.includes(overlayCompany)) overlayVisibility[oConfig.name] = true
78+
} else if (removed.includes(overlayMode)) {
79+
// Company-based mode was just deselected; disable overlay (regardless of company)
80+
overlayVisibility[oConfig.name] = false
81+
} else if (newModes.includes(overlayMode) && oldQuery.companies !== newQuery.companies) {
82+
// Company-based mode remains selected but companies change
83+
overlayVisibility[oConfig.name] = newQuery.companies.includes(overlayCompany)
84+
}
85+
} else { // Default handling for other modes
86+
if (added.includes(overlayMode)) overlayVisibility[oConfig.name] = true
87+
if (removed.includes(overlayMode)) overlayVisibility[oConfig.name] = false
88+
}
89+
}
90+
// Only trigger update action if there are overlays to update.
91+
if (Object.keys(overlayVisibility).length > 0) {
92+
this.props.updateOverlayVisibility(overlayVisibility)
93+
}
94+
}
95+
}
96+
4597
onMapClick = (e) => {
4698
this.props.setMapPopupLocationAndGeocode(e)
4799
}
@@ -56,6 +108,11 @@ class DefaultMap extends Component {
56108
setLocation(payload)
57109
}
58110

111+
componentDidUpdate (prevProps) {
112+
// Check if any overlays should be toggled due to mode change
113+
this._handleQueryChange(prevProps.query, this.props.query)
114+
}
115+
59116
render () {
60117
const {
61118
bikeRentalQuery,
@@ -146,11 +203,16 @@ class DefaultMap extends Component {
146203
// connect to the redux store
147204

148205
const mapStateToProps = (state, ownProps) => {
206+
const overlays = state.otp.config.map && state.otp.config.map.overlays
207+
? state.otp.config.map.overlays
208+
: []
149209
return {
150210
bikeRentalStations: state.otp.overlay.bikeRental.stations,
151211
carRentalStations: state.otp.overlay.carRental.stations,
152212
mapConfig: state.otp.config.map,
153213
mapPopupLocation: state.otp.ui.mapPopupLocation,
214+
overlays,
215+
query: state.otp.currentQuery,
154216
vehicleRentalStations: state.otp.overlay.vehicleRental.stations
155217
}
156218
}
@@ -161,6 +223,7 @@ const mapDispatchToProps = {
161223
setLocation,
162224
setMapPopupLocation,
163225
setMapPopupLocationAndGeocode,
226+
updateOverlayVisibility,
164227
vehicleRentalQuery
165228
}
166229

0 commit comments

Comments
 (0)