This commit is contained in:
Андрей Дувакин 2024-10-05 21:52:36 +05:00
parent e648aeaea5
commit 05f77f2ad6
3 changed files with 29 additions and 14 deletions

View File

@ -523,3 +523,21 @@ export const getDeliveryAccessories = async (deliveryOrderId) => {
throw error;
}
};
export const getDeliveryOrderDetails = async (deliveryOrderId) => {
try {
const response = await axios.get(
`${API_URL}/delivery-orders/${deliveryOrderId}`,
{
headers: {
Authorization: `Bearer ${getAuthToken()}`,
Accept: "application/json",
},
}
);
return response.data;
} catch (error) {
console.log("Ошибка при загрузке деталей заказа доставки:", error);
throw error;
}
};

View File

@ -1,5 +1,5 @@
import React, { useState, useEffect } from "react";
import { getDeliveryAccessories } from "../api.jsx";
import { getDeliveryAccessories, getDeliveryOrderDetails } from "../api.jsx";
import { useParams } from "react-router-dom";
import { getCoordinates } from "../geocoder.jsx";
import {
@ -25,6 +25,7 @@ const DeliveryOrderDetails = () => {
const [loading, setLoading] = useState(true);
const [coordinates, setCoordinates] = useState([]);
const [route, setRoute] = useState([]);
const [totalCost, setTotalCost] = useState(0);
useEffect(() => {
fetchDeliveryAccessories();
@ -32,12 +33,16 @@ const DeliveryOrderDetails = () => {
const fetchDeliveryAccessories = async () => {
try {
const deliveryOrderDetails = await getDeliveryOrderDetails(
deliveryOrderId
);
setTotalCost(deliveryOrderDetails.price);
const accessories = await getDeliveryAccessories(deliveryOrderId);
setDeliveryAccessories(accessories);
const coords = await Promise.all(
accessories.map(async (accessory) => {
console.log(accessory);
if (accessory.latitude && accessory.longitude) {
return {
city: accessory.city_name,
@ -84,6 +89,7 @@ const DeliveryOrderDetails = () => {
</div>
) : (
<div className="content-container">
<h2>Общая стоимость подзаказа: {totalCost} руб.</h2>{" "}
<ol className="city-list">
{deliveryAccessories.map((accessory, index) => {
const coord = coordinates.find(
@ -91,7 +97,7 @@ const DeliveryOrderDetails = () => {
);
return (
<li key={accessory.id} className="city-item">
<strong>{index + 1}. Доставка:</strong> {accessory.name}
<strong>Доставка:</strong> {accessory.accessory_name}
<span className="city-info">
(Город: {accessory.city_name}, Координаты:{" "}
{coord
@ -103,8 +109,7 @@ const DeliveryOrderDetails = () => {
);
})}
<li className="city-item">
<strong>{deliveryAccessories.length + 1}. Конечный пункт:</strong>{" "}
{DELIVERY_CITY.name}
<strong>Конечный пункт:</strong> {DELIVERY_CITY.name}
<span className="city-info">
(Координаты: {DELIVERY_CITY.latitude}, {DELIVERY_CITY.longitude}
)

View File

@ -17,14 +17,6 @@ const Home = () => {
setEndPoint(e.target.value);
};
const handleSearch = () => {
// Замените на реальный геокодер для получения координат на основе адресов
const start = { lat: 55.7558, lng: 37.6173 }; // Москва
const end = { lat: 59.9343, lng: 30.3351 }; // Санкт-Петербург
setStartCoords(start);
setEndCoords(end);
};
useEffect(() => {
if (startCoords && endCoords) {
const routeUrl = `https://router.project-osrm.org/route/v1/driving/${startCoords.lng},${startCoords.lat};${endCoords.lng},${endCoords.lat}?overview=full`;