195 lines
5.3 KiB
TypeScript
195 lines
5.3 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";
|
|
import mapboxgl from "mapbox-gl";
|
|
|
|
const accessToken = process.env.REACT_APP_ACCESS_TOKEN || "";
|
|
mapboxgl.setRTLTextPlugin(
|
|
"https://cdn.parsimap.ir/third-party/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.2.3/mapbox-gl-rtl-text.js",
|
|
() => {}
|
|
);
|
|
const MapContainer = ReactMapboxGl({
|
|
accessToken: accessToken,
|
|
minZoom: 8,
|
|
maxZoom: 15,
|
|
// zoom: 9,
|
|
});
|
|
|
|
interface MyItemType {
|
|
id: number;
|
|
name: string;
|
|
description: string;
|
|
abbreviation: string;
|
|
cityId: number;
|
|
city: string;
|
|
englishName: string;
|
|
latitude: number;
|
|
longitude: number;
|
|
}
|
|
const OnMapRequests = () => {
|
|
const [selected, setSelected] = useState<any | null>(null);
|
|
const [citizenInstance, setCitizenInstance] = useState<MyItemType | null>(
|
|
null
|
|
);
|
|
const [locations, setLocations] = useState<any | null>(null);
|
|
|
|
useEffect(() => {
|
|
const CtizenInstances = localStorage.getItem("SH_CT_INSTANCE");
|
|
|
|
if (CtizenInstances) {
|
|
const parsedItem = JSON.parse(CtizenInstances);
|
|
setCitizenInstance(parsedItem);
|
|
}
|
|
}, [citizenInstance]);
|
|
|
|
const defaultCoords = {
|
|
latitude: citizenInstance?.latitude || 0,
|
|
longitude: citizenInstance?.longitude || 0,
|
|
};
|
|
// 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) {
|
|
setLocations(LocationData?.data?.locations[0]?.locations);
|
|
}
|
|
}, [isSuccess]);
|
|
|
|
|
|
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={[defaultCoords.longitude, defaultCoords.latitude]}
|
|
zoom={[10]}
|
|
containerStyle={{ height: "100%", width: "100%", borderRadius: 20 , direction: "rtl",}}
|
|
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]}>
|
|
<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;
|
|
// return (
|
|
// <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>
|
|
// );
|
|
// }
|
|
|
|
// if (isError) {
|
|
// return <p>Map Failed</p>;
|
|
// }
|
|
|
|
// return (
|
|
// <div className="w-full h-full">
|
|
// <MapContainer
|
|
// center={[defaultCoords.longitude, defaultCoords.latitude]}
|
|
// zoom={[10]}
|
|
// containerStyle={{ height: "100%", width: "100%", borderRadius: 20 }}
|
|
// style={`https://api.parsimap.ir/styles/parsimap-streets-v11?key=${process.env.NEXT_PUBLIC_PMI_TOKEN}`}
|
|
// >
|
|
// {locations?.map((d: any) => (
|
|
// <Marker key={d.reportId} coordinates={[d.longitude, d.latitude]}>
|
|
// <img src={marker} />
|
|
// </Marker>
|
|
// ))}
|
|
// <ZoomControl />
|
|
// </MapContainer>
|
|
// </div>
|
|
// );
|
|
// };
|
|
|
|
// export default OnMapRequests;
|