import { DialogTitle } from "@radix-ui/react-dialog"; import { DialogFooter, DialogHeader } from "../ui/dialog"; import { t } from "@/utils"; import { MdArrowBack } from "react-icons/md"; import SearchAutocomplete from "./SearchAutocomplete"; import { BiCurrentLocation } from "react-icons/bi"; import { useState } from "react"; import { getMaxRange, getMinRange } from "@/redux/reducer/settingSlice"; import { useDispatch, useSelector } from "react-redux"; import { getIsBrowserSupported, getKilometerRange, resetCityData, saveCity, setKilometerRange, } from "@/redux/reducer/locationSlice"; import { Slider } from "../ui/slider"; import dynamic from "next/dynamic"; import { Button } from "../ui/button"; import { toast } from "sonner"; import { usePathname } from "next/navigation"; import { CurrentLanguageData, getIsRtl } from "@/redux/reducer/languageSlice"; import { useNavigate } from "../Common/useNavigate"; import useGetLocation from "../Layout/useGetLocation"; const GetLocationWithMap = dynamic(() => import("./GetLocationWithMap"), { ssr: false, loading: () =>
, }); const MapLocation = ({ OnHide, selectedCity, setSelectedCity, setIsMapLocation, IsPaidApi, }) => { const CurrentLanguage = useSelector(CurrentLanguageData); const dispatch = useDispatch(); const { navigate } = useNavigate(); const pathname = usePathname(); const radius = useSelector(getKilometerRange); const [KmRange, setKmRange] = useState(radius || 0); const [IsFetchingLocation, setIsFetchingLocation] = useState(false); const min_range = useSelector(getMinRange); const max_range = useSelector(getMaxRange); const IsBrowserSupported = useSelector(getIsBrowserSupported); const isRTL = useSelector(getIsRtl); const { fetchLocationData } = useGetLocation(); const getCurrentLocation = async () => { if (navigator.geolocation) { setIsFetchingLocation(true); navigator.geolocation.getCurrentPosition( async (position) => { try { const { latitude, longitude } = position.coords; const data = await fetchLocationData({ lat: latitude, lng: longitude }); setSelectedCity(data); } catch (error) { console.error("Error fetching location data:", error); toast.error(t("errorOccurred")); } finally { setIsFetchingLocation(false); } }, (error) => { console.log(error); toast.error(t("locationNotGranted")); setIsFetchingLocation(false); } ); } else { toast.error(t("geoLocationNotSupported")); } }; const getLocationWithMap = async (pos) => { try { const data = await fetchLocationData(pos); setSelectedCity(data); } catch (error) { console.error("Error fetching location data:", error); } }; const handleSave = () => { const isInvalidLocation = !selectedCity?.lat || !selectedCity?.long; if (isInvalidLocation) { toast.error(t("pleaseSelectLocation")); return; } dispatch(setKilometerRange(KmRange)); saveCity(selectedCity); toast.success(t("locationSaved")); OnHide(); // avoid redirect if already on home page otherwise router.push triggering server side api calls if (pathname !== "/") { navigate("/"); } }; const handleReset = () => { resetCityData(); min_range > 0 ? dispatch(setKilometerRange(min_range)) : dispatch(setKilometerRange(0)); OnHide(); // avoid redirect if already on home page otherwise router.push triggering server side api calls if (pathname !== "/") { navigate("/"); } }; return ( <>{selectedCity?.address_translated || selectedCity?.formattedAddress}
) : ( t("addYourAddress") )}