._.
This commit is contained in:
parent
f41cd7c0af
commit
5ceba75b92
25
src/geocoder.jsx
Normal file
25
src/geocoder.jsx
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -1,11 +1,13 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import { getDeliveryAccessories } from "../api.jsx";
|
import { getDeliveryAccessories } from "../api.jsx";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
|
import { getCoordinates } from "../geocoder.jsx";
|
||||||
|
|
||||||
const DeliveryOrderDetails = () => {
|
const DeliveryOrderDetails = () => {
|
||||||
const { id: deliveryOrderId } = useParams();
|
const { id: deliveryOrderId } = useParams();
|
||||||
const [deliveryAccessories, setDeliveryAccessories] = useState([]);
|
const [deliveryAccessories, setDeliveryAccessories] = useState([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [coordinates, setCoordinates] = useState([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchDeliveryAccessories();
|
fetchDeliveryAccessories();
|
||||||
@ -13,9 +15,16 @@ const DeliveryOrderDetails = () => {
|
|||||||
|
|
||||||
const fetchDeliveryAccessories = async () => {
|
const fetchDeliveryAccessories = async () => {
|
||||||
try {
|
try {
|
||||||
console.log(deliveryOrderId);
|
|
||||||
const accessories = await getDeliveryAccessories(deliveryOrderId);
|
const accessories = await getDeliveryAccessories(deliveryOrderId);
|
||||||
setDeliveryAccessories(accessories);
|
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) {
|
} catch (error) {
|
||||||
console.error("Ошибка при загрузке доставок:", error);
|
console.error("Ошибка при загрузке доставок:", error);
|
||||||
} finally {
|
} finally {
|
||||||
@ -31,11 +40,19 @@ const DeliveryOrderDetails = () => {
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<ul>
|
<ul>
|
||||||
{deliveryAccessories.map((accessory) => (
|
{deliveryAccessories.map((accessory) => {
|
||||||
<li key={accessory.id}>
|
const coord = coordinates.find(
|
||||||
Доставка: {accessory.name} (Город: {accessory.city_name})
|
(c) => c.city === accessory.city_name
|
||||||
</li>
|
);
|
||||||
))}
|
return (
|
||||||
|
<li key={accessory.id}>
|
||||||
|
Доставка: {accessory.name} (Город: {accessory.city_name},
|
||||||
|
Координаты:{" "}
|
||||||
|
{coord ? `${coord.latitude}, ${coord.longitude}` : "Не найдены"}
|
||||||
|
)
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</ul>
|
</ul>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user