@opentripplanner/vehicle-rental-overlay 2.0.0-alpha.1 → 2.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.tsx CHANGED
@@ -1,13 +1,14 @@
1
- import { Source, Layer, useMap, Popup } from "react-map-gl";
2
- import { EventData } from "mapbox-gl";
3
- import React, { useEffect, useState } from "react";
1
+ import { MarkerWithPopup } from "@opentripplanner/base-map";
4
2
  import {
5
3
  Company,
6
- Station,
4
+ ConfiguredCompany,
7
5
  MapLocationActionArg,
8
- ConfiguredCompany
6
+ Station
9
7
  } from "@opentripplanner/types";
10
- import { MarkerWithPopup } from "@opentripplanner/base-map";
8
+ import { EventData } from "mapbox-gl";
9
+ import React, { useEffect, useState } from "react";
10
+ import { Layer, Popup, Source, useMap } from "react-map-gl";
11
+
11
12
  import StationPopup from "./StationPopup";
12
13
  import { BaseBikeRentalIcon, StationMarker } from "./styled";
13
14
 
@@ -42,15 +43,15 @@ const checkIfPositionInViewport = (
42
43
  };
43
44
 
44
45
  type Props = {
45
- /**
46
- * The entire companies config array.
47
- */
48
- configCompanies: ConfiguredCompany[];
49
46
  /**
50
47
  * A list of companies that are applicable to just this instance of the
51
48
  * overlay.
52
49
  */
53
50
  companies?: string[];
51
+ /**
52
+ * The entire companies config array.
53
+ */
54
+ configCompanies: ConfiguredCompany[];
54
55
  /**
55
56
  * An id, used to make this layer uniquely identifiable
56
57
  */
@@ -102,21 +103,20 @@ type Props = {
102
103
  * types. This layer can be configured to show different styles of markers at
103
104
  * different zoom levels.
104
105
  */
105
- const VehicleRentalOverlay = (props: Props): JSX.Element => {
106
- const { mainMap } = useMap();
107
- const zoom = mainMap?.getZoom();
108
- const bounds = mainMap?.getBounds();
106
+ const VehicleRentalOverlay = ({
107
+ companies,
108
+ configCompanies,
109
+ getStationName,
110
+ id,
111
+ refreshVehicles,
112
+ setLocation,
113
+ stations,
114
+ visible
115
+ }: Props): JSX.Element => {
116
+ const { current: map } = useMap();
117
+ const zoom = map?.getZoom();
118
+ const bounds = map?.getBounds();
109
119
 
110
- const {
111
- configCompanies,
112
- companies,
113
- getStationName,
114
- id,
115
- refreshVehicles,
116
- setLocation,
117
- stations,
118
- visible
119
- } = props;
120
120
  const layerId = `rental-vehicles-${id}`;
121
121
  const [clickedVehicle, setClickedVehicle] = useState(null);
122
122
 
@@ -133,20 +133,21 @@ const VehicleRentalOverlay = (props: Props): JSX.Element => {
133
133
  useEffect(() => {
134
134
  const VEHICLE_LAYERS = [layerId];
135
135
  VEHICLE_LAYERS.forEach(stopLayer => {
136
- mainMap?.on("mouseenter", stopLayer, () => {
137
- mainMap.getCanvas().style.cursor = "pointer";
136
+ map?.on("mouseenter", stopLayer, () => {
137
+ map.getCanvas().style.cursor = "pointer";
138
138
  });
139
- mainMap?.on("mouseleave", stopLayer, () => {
140
- mainMap.getCanvas().style.cursor = "";
139
+ map?.on("mouseleave", stopLayer, () => {
140
+ map.getCanvas().style.cursor = "";
141
141
  });
142
- mainMap?.on("click", stopLayer, (event: EventData) => {
142
+ map?.on("click", stopLayer, (event: EventData) => {
143
143
  setClickedVehicle(event.features?.[0].properties);
144
144
  });
145
145
  });
146
- }, [mainMap]);
146
+ }, [map]);
147
147
 
148
148
  // Don't render if no map or no stops are defined.
149
149
  if (visible === false || !stations || stations.length === 0) {
150
+ // Null can't be returned here -- react-map-gl dislikes null values as children
150
151
  return <></>;
151
152
  }
152
153
 
@@ -162,14 +163,14 @@ const VehicleRentalOverlay = (props: Props): JSX.Element => {
162
163
  )
163
164
  .map(vehicle => ({
164
165
  type: "Feature",
166
+ geometry: { type: "Point", coordinates: [vehicle.x, vehicle.y] },
165
167
  properties: {
166
168
  ...vehicle,
167
169
  networks: JSON.stringify(vehicle.networks),
168
170
  "stroke-width":
169
171
  vehicle.isFloatingBike || vehicle.isFloatingVehicle ? 1 : 2,
170
172
  color: getColorForStation(vehicle)
171
- },
172
- geometry: { type: "Point", coordinates: [vehicle.x, vehicle.y] }
173
+ }
173
174
  }))
174
175
  };
175
176
 
@@ -179,13 +180,13 @@ const VehicleRentalOverlay = (props: Props): JSX.Element => {
179
180
  <Source type="geojson" data={vehiclesGeoJSON}>
180
181
  <Layer
181
182
  id={layerId}
182
- type="circle"
183
183
  paint={{
184
184
  "circle-color": ["get", "color"],
185
185
  "circle-opacity": 0.9,
186
- "circle-stroke-width": ["get", "stroke-width"],
187
- "circle-stroke-color": "#333"
186
+ "circle-stroke-color": "#333",
187
+ "circle-stroke-width": ["get", "stroke-width"]
188
188
  }}
189
+ type="circle"
189
190
  />
190
191
  {/* this is where we add the symbols layer. add a second layer that gets swapped in and out dynamically */}
191
192
  </Source>
@@ -198,7 +199,6 @@ const VehicleRentalOverlay = (props: Props): JSX.Element => {
198
199
  .map(station => (
199
200
  <MarkerWithPopup
200
201
  key={station.id}
201
- position={[station.y, station.x]}
202
202
  popupContents={
203
203
  <StationPopup
204
204
  configCompanies={configCompanies}
@@ -210,6 +210,7 @@ const VehicleRentalOverlay = (props: Props): JSX.Element => {
210
210
  station={station}
211
211
  />
212
212
  }
213
+ position={[station.y, station.x]}
213
214
  >
214
215
  {station.bikesAvailable !== undefined &&
215
216
  !station.isFloatingBike &&
@@ -228,20 +229,20 @@ const VehicleRentalOverlay = (props: Props): JSX.Element => {
228
229
  ))}
229
230
  {clickedVehicle && (
230
231
  <Popup
232
+ latitude={clickedVehicle.y}
233
+ longitude={clickedVehicle.x}
234
+ maxWidth="100%"
231
235
  onClose={() => {
232
236
  setClickedVehicle(null);
233
237
  }}
234
- longitude={clickedVehicle.x}
235
- latitude={clickedVehicle.y}
236
- maxWidth="100%"
237
238
  >
238
239
  <StationPopup
239
240
  configCompanies={configCompanies}
241
+ getStationName={getStationName}
240
242
  setLocation={location => {
241
243
  setClickedVehicle(null);
242
244
  setLocation(location);
243
245
  }}
244
- getStationName={getStationName}
245
246
  station={{
246
247
  ...clickedVehicle,
247
248
  networks: JSON.parse(clickedVehicle.networks)
package/src/styled.ts CHANGED
@@ -36,17 +36,17 @@ const getPctIcon = (percent: number) => {
36
36
  * their own unique icon.
37
37
  */
38
38
  export const BaseBikeRentalIcon = styled.div<{ percent: number }>`
39
- background-size: contain;
40
39
  background-position: center;
41
40
  background-repeat: no-repeat;
41
+ background-size: contain;
42
+ height: 24px;
42
43
  margin: auto;
43
44
  width: 24px;
44
- height: 24px;
45
45
  ${props => getPctIcon(props.percent)}
46
46
  `;
47
47
 
48
48
  export const StationMarker = styled(MapMarkerAlt)`
49
+ color: ${props => props.color};
49
50
  height: 12;
50
51
  width: 12;
51
- color: ${props => props.color};
52
52
  `;