8
8
carRentalQuery ,
9
9
vehicleRentalQuery
10
10
} from '../../actions/api'
11
+ import { updateOverlayVisibility } from '../../actions/config'
11
12
import {
12
13
setLocation ,
13
14
setMapPopupLocation ,
@@ -42,6 +43,57 @@ const MapContainer = styled.div`
42
43
`
43
44
44
45
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
+
45
97
onMapClick = ( e ) => {
46
98
this . props . setMapPopupLocationAndGeocode ( e )
47
99
}
@@ -56,6 +108,13 @@ class DefaultMap extends Component {
56
108
setLocation ( payload )
57
109
}
58
110
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
+
59
118
render ( ) {
60
119
const {
61
120
bikeRentalQuery,
@@ -146,11 +205,16 @@ class DefaultMap extends Component {
146
205
// connect to the redux store
147
206
148
207
const mapStateToProps = ( state , ownProps ) => {
208
+ const overlays = state . otp . config . map && state . otp . config . map . overlays
209
+ ? state . otp . config . map . overlays
210
+ : [ ]
149
211
return {
150
212
bikeRentalStations : state . otp . overlay . bikeRental . stations ,
151
213
carRentalStations : state . otp . overlay . carRental . stations ,
152
214
mapConfig : state . otp . config . map ,
153
215
mapPopupLocation : state . otp . ui . mapPopupLocation ,
216
+ overlays,
217
+ query : state . otp . currentQuery ,
154
218
vehicleRentalStations : state . otp . overlay . vehicleRental . stations
155
219
}
156
220
}
@@ -161,6 +225,7 @@ const mapDispatchToProps = {
161
225
setLocation,
162
226
setMapPopupLocation,
163
227
setMapPopupLocationAndGeocode,
228
+ updateOverlayVisibility,
164
229
vehicleRentalQuery
165
230
}
166
231
0 commit comments