From 5ceba75b922a01bf9b8ceb769f2d038167ae0c2e Mon Sep 17 00:00:00 2001 From: andrei Date: Sat, 5 Oct 2024 17:07:11 +0500 Subject: [PATCH] ._. --- src/geocoder.jsx | 25 +++++++++++++++++++++++++ src/pages/DeliveryOrderDetails.jsx | 29 +++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/geocoder.jsx diff --git a/src/geocoder.jsx b/src/geocoder.jsx new file mode 100644 index 0000000..d6b47d5 --- /dev/null +++ b/src/geocoder.jsx @@ -0,0 +1,25 @@ +import axios from "axios"; + +const API_KEY = "d101001e-49f5-4a17-ac39-2d67f64b3f9b"; + +export const getCoordinates = async (city) => { + try { + const response = await axios.get( + `https://geocode-maps.yandex.ru/1.x/?apikey=${API_KEY}&geocode=${city}&format=json` + ); + + const coordinates = + response.data?.response?.GeoObjectCollection?.featureMember[0]?.GeoObject + ?.Point?.pos; + + if (coordinates) { + const [longitude, latitude] = coordinates.split(" "); + return { longitude, latitude }; + } + + throw new Error("Координаты не найдены"); + } catch (error) { + console.error("Ошибка при получении координат:", error); + throw error; + } +}; diff --git a/src/pages/DeliveryOrderDetails.jsx b/src/pages/DeliveryOrderDetails.jsx index 53fcc97..44f5540 100644 --- a/src/pages/DeliveryOrderDetails.jsx +++ b/src/pages/DeliveryOrderDetails.jsx @@ -1,11 +1,13 @@ import React, { useState, useEffect } from "react"; import { getDeliveryAccessories } from "../api.jsx"; import { useParams } from "react-router-dom"; +import { getCoordinates } from "../geocoder.jsx"; const DeliveryOrderDetails = () => { const { id: deliveryOrderId } = useParams(); const [deliveryAccessories, setDeliveryAccessories] = useState([]); const [loading, setLoading] = useState(true); + const [coordinates, setCoordinates] = useState([]); useEffect(() => { fetchDeliveryAccessories(); @@ -13,9 +15,16 @@ const DeliveryOrderDetails = () => { const fetchDeliveryAccessories = async () => { try { - console.log(deliveryOrderId); const accessories = await getDeliveryAccessories(deliveryOrderId); setDeliveryAccessories(accessories); + + const coords = await Promise.all( + accessories.map(async (accessory) => { + const coords = await getCoordinates(accessory.city_name); + return { city: accessory.city_name, ...coords }; + }) + ); + setCoordinates(coords); } catch (error) { console.error("Ошибка при загрузке доставок:", error); } finally { @@ -31,11 +40,19 @@ const DeliveryOrderDetails = () => { ) : ( )}