split pages CreateUpdatePost on two part and fixed button map
This commit is contained in:
@@ -12,9 +12,16 @@ export function useYandexMap(enabled, initialCoords = null, initialAddress = "")
|
||||
const [coords, setCoords] = useState(initialCoords);
|
||||
const [address, setAddress] = useState(initialAddress ?? "");
|
||||
const addressRef = useRef(initialAddress ?? "");
|
||||
const [loading, setLoading] = useState(false);
|
||||
// Всегда хранит последние координаты, чтобы при повторной инициализации
|
||||
// карты (например, при повторном открытии модалки) маркер появлялся там,
|
||||
// где пользователь его оставил, а не сбрасывался на initialCoords.
|
||||
const coordsRef = useRef(initialCoords);
|
||||
useEffect(() => { coordsRef.current = coords; }, [coords]);
|
||||
|
||||
const resolveAddress = useCallback((lat, lng) => {
|
||||
const requestId = ++requestIdRef.current;
|
||||
setLoading(true);
|
||||
|
||||
fetch(
|
||||
`https://geocode-maps.yandex.ru/1.x/?apikey=${YANDEX_GEOCODER_API_KEY}&geocode=${lng},${lat}&format=json&lang=ru_RU`
|
||||
@@ -29,6 +36,7 @@ export function useYandexMap(enabled, initialCoords = null, initialAddress = "")
|
||||
const resolved = found || `${lat.toFixed(5)}, ${lng.toFixed(5)}`;
|
||||
setAddress(resolved);
|
||||
addressRef.current = resolved;
|
||||
setLoading(false);
|
||||
})
|
||||
.catch(() => {
|
||||
if (requestId !== requestIdRef.current) return;
|
||||
@@ -36,6 +44,7 @@ export function useYandexMap(enabled, initialCoords = null, initialAddress = "")
|
||||
const fallback = `${lat.toFixed(5)}, ${lng.toFixed(5)}`;
|
||||
setAddress(fallback);
|
||||
addressRef.current = fallback;
|
||||
setLoading(false);
|
||||
});
|
||||
}, []);
|
||||
|
||||
@@ -47,8 +56,9 @@ export function useYandexMap(enabled, initialCoords = null, initialAddress = "")
|
||||
const init = () => {
|
||||
if (ymapRef.current) return;
|
||||
|
||||
const center = initialCoords
|
||||
? [initialCoords.latitude, initialCoords.longitude]
|
||||
const startCoords = coordsRef.current;
|
||||
const center = startCoords
|
||||
? [startCoords.latitude, startCoords.longitude]
|
||||
: [55.751574, 37.573856];
|
||||
|
||||
const map = new window.ymaps.Map(mapRef.current, {
|
||||
@@ -58,11 +68,11 @@ export function useYandexMap(enabled, initialCoords = null, initialAddress = "")
|
||||
});
|
||||
ymapRef.current = map;
|
||||
|
||||
if (initialCoords) {
|
||||
if (startCoords) {
|
||||
const pm = new window.ymaps.Placemark(center, {}, { draggable: true });
|
||||
map.geoObjects.add(pm);
|
||||
placemarkRef.current = pm;
|
||||
resolveAddress(initialCoords.latitude, initialCoords.longitude);
|
||||
resolveAddress(startCoords.latitude, startCoords.longitude);
|
||||
|
||||
pm.events.add("dragend", () => {
|
||||
const [lat, lng] = pm.geometry.getCoordinates();
|
||||
@@ -102,12 +112,11 @@ export function useYandexMap(enabled, initialCoords = null, initialAddress = "")
|
||||
placemarkRef.current = null;
|
||||
initializedRef.current = false;
|
||||
};
|
||||
// initialCoords намеренно не в deps — нужен только при первой инициализации,
|
||||
// а initializedRef защищает от повторного запуска. Объект initialCoords
|
||||
// пересоздаётся на каждый рендер родителя, поэтому включение его в deps
|
||||
// приводило бы к пересозданию карты на каждый ввод в форме.
|
||||
// enabled намеренно в deps — карта инициализируется/разрушается при открытии
|
||||
// и закрытии модалки. Начальные координаты читаются из coordsRef, чтобы
|
||||
// при повторном открытии маркер вставал на последний выбранный адрес.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [enabled, resolveAddress]);
|
||||
|
||||
return { mapRef, coords, address, addressRef };
|
||||
return { mapRef, coords, address, addressRef, loading };
|
||||
}
|
||||
Reference in New Issue
Block a user