92 lines
3.9 KiB
JavaScript
92 lines
3.9 KiB
JavaScript
import { useEffect } from "react";
|
||
import { useTranslation } from "react-i18next";
|
||
import CloseIcon from "../components/icons/CloseIcon";
|
||
import { useYandexMap, ADDRESS_ZOOM } from "../hooks/useYandexMap";
|
||
import { useGeolocation, GEO_STATUS } from "../hooks/useGeolocation";
|
||
|
||
/**
|
||
* MapModal — модальное окно с картой для выбора места.
|
||
*
|
||
* Клик по карте / перетаскивание метки определяет адрес; в форму он попадает
|
||
* только по кнопке «Выбрать». Закрытие окна ничего не меняет.
|
||
*
|
||
* @param {{ latitude: number, longitude: number } | null} initialCoords — текущая точка формы
|
||
* @param {string} initialAddress — текущий адрес формы
|
||
* @param {function} onClose — закрыть окно без выбора
|
||
* @param {function} onPick — выбрано место: ({ address, coords: { latitude, longitude } }) => void
|
||
*/
|
||
export default function MapModal({ initialCoords, initialAddress, onClose, onPick }) {
|
||
const { t } = useTranslation();
|
||
const { mapRef, coords, address, loading, loadError, addressError, moveTo } =
|
||
useYandexMap(true, initialCoords, initialCoords ? initialAddress : "");
|
||
|
||
const { coords: geoCoords, status: geoStatus, requestLocation } = useGeolocation();
|
||
|
||
// Точки ещё нет — пробуем поставить метку на место пользователя.
|
||
// При отказе/недоступности остаётся центр по умолчанию.
|
||
useEffect(() => {
|
||
if (!initialCoords) requestLocation();
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
}, [requestLocation]);
|
||
|
||
useEffect(() => {
|
||
if (!initialCoords && geoStatus === GEO_STATUS.SUCCESS && geoCoords) {
|
||
moveTo(geoCoords.latitude, geoCoords.longitude, { zoom: ADDRESS_ZOOM });
|
||
}
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
}, [geoStatus, geoCoords, moveTo]);
|
||
|
||
useEffect(() => {
|
||
const prev = document.body.style.overflow;
|
||
document.body.style.overflow = "hidden";
|
||
return () => { document.body.style.overflow = prev; };
|
||
}, []);
|
||
|
||
const geoHint =
|
||
geoStatus === GEO_STATUS.LOADING ? t('post.geoLoading')
|
||
: geoStatus === GEO_STATUS.DENIED ? t('post.geoDenied')
|
||
: geoStatus === GEO_STATUS.UNSUPPORTED ? t('post.geoUnsupported')
|
||
: geoStatus === GEO_STATUS.TIMEOUT ? t('post.geoTimeout')
|
||
: null;
|
||
|
||
const canPick = !!coords && !!address && !loading;
|
||
|
||
return (
|
||
<div className="f-modal-bg" onClick={e => e.target === e.currentTarget && onClose()}>
|
||
<div className="f-modal f-map-modal">
|
||
<div className="f-modal-head">
|
||
<span className="f-modal-title">{t('post.mapLabel')}</span>
|
||
<button className="f-close-btn" type="button" onClick={onClose}>
|
||
<CloseIcon />
|
||
</button>
|
||
</div>
|
||
|
||
<div className={`f-map-frame f-map-modal-frame${loading ? " loading" : ""}`}>
|
||
<div ref={mapRef} className="f-map-inner" />
|
||
</div>
|
||
|
||
{!coords && geoHint && <div className="f-map-geo">{geoHint}</div>}
|
||
{loadError && <p className="f-err-msg">{loadError}</p>}
|
||
|
||
<div className="f-map-selected">
|
||
<span className="f-map-selected-label">{t('post.selectedAddress')}</span>
|
||
<span className="f-map-selected-value">
|
||
{loading ? t('post.mapLoading')
|
||
: addressError ? t('post.addressNotDetected')
|
||
: (coords && address) || t('post.mapEmpty')}
|
||
</span>
|
||
</div>
|
||
|
||
<button
|
||
className="f-m-submit"
|
||
type="button"
|
||
disabled={!canPick}
|
||
onClick={() => onPick({ address, coords })}
|
||
>
|
||
<div className="f-m-submit-row">{t('post.pickLocation')}</div>
|
||
</button>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|