added hints on map
This commit is contained in:
@@ -1,20 +1,40 @@
|
||||
import { useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import CloseIcon from "../components/icons/CloseIcon";
|
||||
import { GEO_STATUS } from "../hooks/useGeolocation";
|
||||
import { useYandexMap, ADDRESS_ZOOM } from "../hooks/useYandexMap";
|
||||
import { useGeolocation, GEO_STATUS } from "../hooks/useGeolocation";
|
||||
|
||||
/**
|
||||
* MapModal — модальное окно с картой для выбора местоположения.
|
||||
* MapModal — модальное окно с картой для выбора места.
|
||||
*
|
||||
* @param {object} mapRef — ref, в который монтируется Яндекс.Карта
|
||||
* @param {string} address — текущий (выбранный) адрес
|
||||
* @param {boolean} mapLoading — идёт ли подгрузка адреса по координатам
|
||||
* @param {string} geoStatus — статус геолокации (GEO_STATUS.*)
|
||||
* @param {function} onClose — закрыть окно без сохранения
|
||||
* @param {function} onSave — сохранить выбранное местоположение
|
||||
* Клик по карте / перетаскивание метки определяет адрес; в форму он попадает
|
||||
* только по кнопке «Выбрать». Закрытие окна ничего не меняет.
|
||||
*
|
||||
* @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({ mapRef, address, mapLoading, geoStatus, onClose, onSave }) {
|
||||
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;
|
||||
@@ -23,17 +43,17 @@ export default function MapModal({ mapRef, address, mapLoading, geoStatus, onClo
|
||||
}, []);
|
||||
|
||||
const geoHint =
|
||||
geoStatus === GEO_STATUS.SUCCESS ? t('post.geoSuccess')
|
||||
: 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;
|
||||
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">
|
||||
{/* Header */}
|
||||
<div className="f-modal-head">
|
||||
<span className="f-modal-title">{t('post.mapLabel')}</span>
|
||||
<button className="f-close-btn" type="button" onClick={onClose}>
|
||||
@@ -41,28 +61,31 @@ export default function MapModal({ mapRef, address, mapLoading, geoStatus, onClo
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Map */}
|
||||
<div className="f-map-frame f-map-modal-frame">
|
||||
<div className={`f-map-frame f-map-modal-frame${loading ? " loading" : ""}`}>
|
||||
<div ref={mapRef} className="f-map-inner" />
|
||||
</div>
|
||||
|
||||
{geoHint && (
|
||||
<div className="f-map-geo">{geoHint}</div>
|
||||
)}
|
||||
{!coords && geoHint && <div className="f-map-geo">{geoHint}</div>}
|
||||
{loadError && <p className="f-err-msg">{loadError}</p>}
|
||||
|
||||
{/* Selected address */}
|
||||
<div className="f-map-selected">
|
||||
<span className="f-map-selected-label">{t('post.selectedAddress')}</span>
|
||||
<span className="f-map-selected-value">
|
||||
{mapLoading ? t('post.mapLoading') : (address || t('post.mapEmpty'))}
|
||||
{loading ? t('post.mapLoading')
|
||||
: addressError ? t('post.addressNotDetected')
|
||||
: (coords && address) || t('post.mapEmpty')}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<button className="f-m-submit" type="button" onClick={onSave} disabled={mapLoading}>
|
||||
<div className="f-m-submit-row">{t('post.saveLocation')}</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>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user