122 lines
3.3 KiB
TypeScript
122 lines
3.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 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,
|
|
});
|
|
|
|
interface MyItemType {
|
|
id: number;
|
|
name: string;
|
|
description: string;
|
|
abbreviation: string;
|
|
cityId: number;
|
|
city: string;
|
|
englishName: string;
|
|
latitude: number;
|
|
longitude: number;
|
|
}
|
|
|
|
interface LocationData {
|
|
reportId: number;
|
|
longitude: number;
|
|
latitude: number;
|
|
}
|
|
|
|
const OnMapRequests: React.FC = () => {
|
|
const [selected, setSelected] = useState<any | null>(null);
|
|
const [citizenInstance, setCitizenInstance] = useState<MyItemType | null>(null);
|
|
const [locations, setLocations] = useState<LocationData[] | null>(null);
|
|
|
|
useEffect(() => {
|
|
const CtizenInstances = localStorage.getItem("SH_CT_INSTANCE");
|
|
if (CtizenInstances) {
|
|
const parsedItem: MyItemType = JSON.parse(CtizenInstances);
|
|
setCitizenInstance(parsedItem);
|
|
}
|
|
}, []); // Remove citizenInstance from the dependency array
|
|
|
|
const defaultCoords = {
|
|
latitude: citizenInstance?.latitude || 0,
|
|
longitude: citizenInstance?.longitude || 0,
|
|
};
|
|
|
|
const detailsRef = useRef<HTMLDivElement | null>(null);
|
|
const wrappersRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
const showDetails = () => {};
|
|
|
|
const {
|
|
data: locationData,
|
|
isLoading,
|
|
isError,
|
|
isSuccess,
|
|
refetch,
|
|
} = useQuery({
|
|
queryKey: ["Locations"],
|
|
queryFn: getLocations,
|
|
refetchOnWindowFocus: false,
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (locationData) {
|
|
setLocations(locationData?.data?.locations[0]?.locations);
|
|
}
|
|
}, [locationData]); // Use locationData instead of 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}`}
|
|
>
|
|
{locations?.map((d: LocationData) => (
|
|
<Marker key={d.reportId} coordinates={[d.longitude, d.latitude] as [number, number]}>
|
|
<img src={marker} alt="location marker" />
|
|
</Marker>
|
|
))}
|
|
<ZoomControl />
|
|
</MapContainer>
|
|
)}
|
|
|
|
{isError && <p>Map Failed</p>}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default OnMapRequests;
|