154 lines
4.4 KiB
TypeScript
154 lines
4.4 KiB
TypeScript
"use client";
|
|
import React, { useEffect, useRef, useState } from "react";
|
|
import { getLocations } from "@/utils/CitizenReport";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { ThreeDots } from "react-loader-spinner";
|
|
import ReactMapboxGl, { Marker, ZoomControl } from "react-mapbox-gl";
|
|
import marker from "@/assets/images/location.svg";
|
|
import RequestCard from "../Requests/RequestCard";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
const accessToken = process.env.REACT_APP_ACCESS_TOKEN || "";
|
|
|
|
const MapContainer = ReactMapboxGl({
|
|
accessToken: accessToken,
|
|
minZoom: 8,
|
|
maxZoom: 15,
|
|
// zoom: 9,
|
|
});
|
|
|
|
const OnMapRequests = () => {
|
|
const [selected, setSelected] = useState<any | null>(null);
|
|
const [citizenInstance, setCitizenInstance] = useState<any | null>(null);
|
|
const [locations, setLocations] = useState<any | null>(null);
|
|
|
|
useEffect(() => {
|
|
const CtizenInstances = localStorage.getItem("SH_CT_INSTANCE");
|
|
setCitizenInstance(CtizenInstances);
|
|
|
|
console.log("🚀 ~ useEffect ~ CtizenInstancessssss:", citizenInstance);
|
|
console.log("🚀 ~ useEffect ~ CtizenInstancessssssss:", CtizenInstances);
|
|
|
|
const defaultCoords = {
|
|
// latitude: citizenInstance.latitude,
|
|
// longitude: citizenInstance.longitude,
|
|
};
|
|
}, [citizenInstance]);
|
|
|
|
// refs
|
|
const detailsRef = useRef(null);
|
|
const wrappersRef = useRef(null);
|
|
|
|
// hooks
|
|
// const [detailsClicked, setDetailsClicked] = useClick({
|
|
// element: detailsRef,
|
|
// // whitelists,
|
|
// });
|
|
|
|
// functions
|
|
// const showDetails = (request) => {
|
|
// setSelected(request);
|
|
// setDetailsClicked(true);
|
|
// };
|
|
|
|
const showDetails = () => {
|
|
// setSelected(request);
|
|
// setDetailsClicked(true);
|
|
};
|
|
|
|
// const onMapClicked = (e, x) => {
|
|
// setSelected(null);
|
|
// };
|
|
|
|
const {
|
|
data: LocationData,
|
|
isLoading,
|
|
isError,
|
|
isSuccess,
|
|
refetch,
|
|
} = useQuery({
|
|
queryKey: ["Locations"],
|
|
queryFn: () => getLocations(),
|
|
refetchOnWindowFocus: false,
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (LocationData) {
|
|
console.log("🚀 ~ OnMapRequests ~ LocationData:", LocationData);
|
|
console.log(
|
|
"🚀 ~ OnMapRequestssss ~ LocationDataa:",
|
|
LocationData?.data?.locations[0]?.locations
|
|
);
|
|
setLocations(LocationData?.data?.locations[0]?.locations);
|
|
}
|
|
}, [isSuccess]);
|
|
|
|
if (isError) {
|
|
console.log("have a errorrrrrrr ");
|
|
}
|
|
|
|
return (
|
|
<div className="w-full h-full">
|
|
{isLoading ? (
|
|
<div className="flex justify-center items-center text-center w-full h-full ">
|
|
<ThreeDots
|
|
visible={true}
|
|
height="50"
|
|
width="50"
|
|
color="#003778"
|
|
radius="9"
|
|
ariaLabel="three-dots-loading"
|
|
wrapperStyle={{}}
|
|
wrapperClass=""
|
|
/>
|
|
</div>
|
|
) : (
|
|
<MapContainer
|
|
// center={[
|
|
// store.instance?.longitude || defaultCoords.longitude,
|
|
// store.instance?.latitude || defaultCoords.latitude,
|
|
// ]}
|
|
zoom={[10]}
|
|
center={[31.887935423191124, 54.355335268625275]}
|
|
containerStyle={{ height: "100%", width: "100%", borderRadius: 20 }}
|
|
style={`https://api.parsimap.ir/styles/parsimap-streets-v11?key=${process.env.NEXT_PUBLIC_PMI_TOKEN}`}
|
|
// onClick={onMapClicked}
|
|
>
|
|
{locations?.map((d: any) => (
|
|
<Marker
|
|
key={d.reportId}
|
|
coordinates={[d.longitude, d.latitude]}
|
|
// coordinates={[ 31.887935423191124, 54.355335268625275,]}
|
|
>
|
|
<img src={marker} />
|
|
{/* <img src={marker} onClick={() => showDetails(d)} /> */}
|
|
</Marker>
|
|
))}
|
|
|
|
{/* Open CardData */}
|
|
{/* <Marker
|
|
coordinates={[
|
|
31.887935423191124, 54.355335268625275,
|
|
selected?.address?.longitude || defaultCoords.latitude,
|
|
selected?.address?.latitude || defaultCoords.longitude,
|
|
]}
|
|
>
|
|
<div
|
|
ref={detailsRef}
|
|
className={cn.join("mapDetailsBox", selected ? "show" : "")}
|
|
style={{ width: wrappersRef?.current?.offsetWidth - 20 || 200 }}
|
|
>
|
|
{selected && <RequestCard request={selected} />}
|
|
</div>
|
|
</Marker> */}
|
|
<ZoomControl />
|
|
</MapContainer>
|
|
)}
|
|
|
|
{isError && <p>Map Failed</p>}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default OnMapRequests;
|