Skip to content

Commit c043a22

Browse files
fix(default-map): Bring back updating vehicle rental overlay visibility when clicking mode selector.
1 parent ded87b0 commit c043a22

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

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

+65
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,13 @@ class DefaultMap extends Component {
56108
setLocation(payload)
57109
}
58110

111+
componentDidUpdate (prevProps) {
112+
// TODO: reinstate this call below.
113+
// this._updateBounds(prevProps, this.props)
114+
// Check if any overlays should be toggled due to mode change
115+
this._handleQueryChange(prevProps.query, this.props.query)
116+
}
117+
59118
render () {
60119
const {
61120
bikeRentalQuery,
@@ -146,11 +205,16 @@ class DefaultMap extends Component {
146205
// connect to the redux store
147206

148207
const mapStateToProps = (state, ownProps) => {
208+
const overlays = state.otp.config.map && state.otp.config.map.overlays
209+
? state.otp.config.map.overlays
210+
: []
149211
return {
150212
bikeRentalStations: state.otp.overlay.bikeRental.stations,
151213
carRentalStations: state.otp.overlay.carRental.stations,
152214
mapConfig: state.otp.config.map,
153215
mapPopupLocation: state.otp.ui.mapPopupLocation,
216+
overlays,
217+
query: state.otp.currentQuery,
154218
vehicleRentalStations: state.otp.overlay.vehicleRental.stations
155219
}
156220
}
@@ -161,6 +225,7 @@ const mapDispatchToProps = {
161225
setLocation,
162226
setMapPopupLocation,
163227
setMapPopupLocationAndGeocode,
228+
updateOverlayVisibility,
164229
vehicleRentalQuery
165230
}
166231

0 commit comments

Comments
 (0)