-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kelvinbe
committed
Jan 15, 2024
1 parent
0a267c0
commit 727efcc
Showing
12 changed files
with
1,530 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 48 additions & 112 deletions
160
app/components/organism/Maps/LiveMapComponent/LiveMapComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,48 @@ | ||
import React, { useMemo } from "react"; | ||
import { Wrapper } from "@googlemaps/react-wrapper"; | ||
import { IStation, IVehicle } from "../../../../globaltypes"; | ||
import CarMarkerComponent from "../CarMarkerComponent/CarMarkerComponent"; | ||
import { Flex } from "@chakra-ui/react"; | ||
import { FlexRowCenterCenter } from "../../../../utils/theme/FlexConfigs"; | ||
import getConfig from "next/config"; | ||
|
||
interface IProps { | ||
vehicles?: Partial<IVehicle & { | ||
station?: Partial<IStation> | ||
}>[]; | ||
marketId?: string; | ||
loading?: boolean; | ||
} | ||
|
||
const LiveMapComponent = (props: IProps) => { | ||
const { vehicles, loading } = props; | ||
|
||
const initialCenter = useMemo(()=>{ | ||
const firstStation = vehicles?.[0]?.station | ||
|
||
return { | ||
lat: firstStation?.latitude ?? 0, | ||
lng: firstStation?.longitude ?? 0 | ||
} | ||
}, [vehicles?.length]) | ||
|
||
/** | ||
* @todo add logic to fetch the location of the market id | ||
*/ | ||
return ( | ||
<Flex | ||
w="full" | ||
h="full" | ||
{...FlexRowCenterCenter} | ||
borderRadius="20px" | ||
overflow="hidden" | ||
border="1px solid" | ||
borderColor="gray.200" | ||
data-testid="map-component" | ||
> | ||
{ | ||
loading ? ( | ||
<div className="grid w-full bg-slate-200 h-[400px] animate-pulse "></div> | ||
) : ( | ||
<Wrapper apiKey={getConfig().publicRuntimeConfig.GOOGLE_MAPS_API_KEY}> | ||
<Map | ||
latitude={ | ||
initialCenter?.lat ?? 0 | ||
} | ||
longitude={initialCenter?.lng ?? 0} | ||
zoom={ | ||
// for now, the zoom will be fixed | ||
5 | ||
} | ||
mapTypeId="roadmap" | ||
> | ||
{vehicles?.map((vehicle, i) => ( | ||
<CarMarkerComponent key={i} {...vehicle} /> | ||
))} | ||
</Map> | ||
</Wrapper> | ||
) | ||
} | ||
</Flex> | ||
); | ||
}; | ||
|
||
interface IMapProps extends google.maps.MapOptions { | ||
longitude?: number; | ||
latitude?: number; | ||
children?: React.ReactNode; | ||
} | ||
|
||
const Map = (props: IMapProps) => { | ||
const { latitude, longitude, children, zoom } = props; | ||
const ref = React.useRef(null); | ||
const [map, setMap] = React.useState<google.maps.Map | null>(null); | ||
|
||
React.useEffect(() => { | ||
if (ref.current && !map) { | ||
setMap( | ||
new google.maps.Map(ref.current, { | ||
zoomControl: true, | ||
mapTypeControl: false, | ||
streetViewControl: false, | ||
center: { | ||
lat: latitude ?? 0, | ||
lng: longitude ?? 0, | ||
}, | ||
zoom: zoom ?? 0, | ||
}) | ||
); | ||
} | ||
}, [ref, map, latitude, longitude, zoom]); | ||
|
||
return ( | ||
<> | ||
<div ref={ref} style={{ height: "100%", width: "100%" }} data-cy={'live-map'}/> | ||
{React.Children.map(children, (child) => { | ||
if (React.isValidElement(child)) { | ||
// set the map prop on the child component | ||
// @ts-ignore | ||
return React.cloneElement(child, { map }); | ||
} | ||
})} | ||
</> | ||
); | ||
}; | ||
|
||
export default LiveMapComponent; | ||
import React, { useMemo } from "react"; | ||
import { Wrapper } from "@googlemaps/react-wrapper"; | ||
import { IStation, IVehicle } from "../../../../globaltypes"; | ||
import CarMarkerComponent from "../CarMarkerComponent/CarMarkerComponent"; | ||
import { Flex } from "@chakra-ui/react"; | ||
import { MapContainer, Marker, TileLayer, Tooltip, Popup } from "react-leaflet" | ||
import "leaflet/dist/leaflet.css" | ||
import "leaflet-defaulticon-compatibility" | ||
import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css" | ||
|
||
|
||
const LiveMapComponent = (props: any) => { | ||
const geoUrl = "https://raw.githubusercontent.com/deldersveld/topojson/master/world-countries.json" | ||
|
||
const position = [51.505, -0.09] | ||
|
||
return ( | ||
<Flex | ||
w="100%" // Set the width to 100% | ||
h="100%" // Set the height to 100% | ||
align="center" // Align the content to the center vertically | ||
justify="center" | ||
borderRadius="20px" | ||
overflow="hidden" | ||
border="1px solid" | ||
borderColor="gray.200" | ||
data-testid="map-component" | ||
> | ||
|
||
<MapContainer center={[51.505, -0.09]} zoom={13} scrollWheelZoom={false}> | ||
<TileLayer | ||
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' | ||
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" | ||
/> | ||
<Marker position={[51.505, -0.09]}> | ||
<Popup> | ||
A pretty CSS3 popup. <br /> Easily customizable. | ||
</Popup> | ||
</Marker> | ||
</MapContainer> | ||
</Flex> | ||
); | ||
}; | ||
|
||
|
||
|
||
|
||
export default LiveMapComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.