diff --git a/frontend/src/api/index.js b/frontend/src/api/index.js index a357a28..4422bbe 100644 --- a/frontend/src/api/index.js +++ b/frontend/src/api/index.js @@ -37,9 +37,14 @@ export const updateItem = (id, itemData) => export const deleteItem = (id) => axios.delete(`${API_URL}/items/${id}`); -export const getAllCategories = () => axios.get(`${API_URL}/getCategories`) +export const getAllCategories = () => axios.get(`${API_URL}/getCategories`); export const getCart = () => axios.get(`${API_URL}/cart/`); -export const addToCart = (data) => axios.post(`${API_URL}/cart/`, data) -export const updateCartItem = (id, data) => axios.post(`${API_URL}/cart/${id}`, data) -export const removeFromCart = (id) => axios.post(`${API_URL}/cart/${id}`) +export const addToCart = (data) => axios.post(`${API_URL}/cart/`, data); +export const updateCartItem = (id, data) => + axios.post(`${API_URL}/cart/${id}`, data); +export const removeFromCart = (id) => axios.post(`${API_URL}/cart/${id}`); + +export const createOrder = (data) => axios.post(`${API_URL}/orders/`, data); +export const getUserOrders = () => axios.get(`${API_URL}/orders/`); +export const getOrder = (id) => axios.get(`${API_URL}/orders/${id}`); diff --git a/frontend/src/components/CardItem.jsx b/frontend/src/components/CardItem.jsx index 385a4e2..7d11565 100644 --- a/frontend/src/components/CardItem.jsx +++ b/frontend/src/components/CardItem.jsx @@ -20,7 +20,11 @@ export default function CardItem({ item, onAddToCart }) { return (
- {item.name} + {item.name}
{/* Блок с информацией о товаре */} diff --git a/frontend/src/pages/Cart.jsx b/frontend/src/pages/Cart.jsx index 1ea6a9f..5aec4a1 100644 --- a/frontend/src/pages/Cart.jsx +++ b/frontend/src/pages/Cart.jsx @@ -1,6 +1,6 @@ import { useState, useEffect } from "react"; import PageLayout from "../components/PageLayout"; -import { getCart } from "../api/index"; +import { getCart, removeFromCart, createOrder } from "../api/index"; export default function Cart() { const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); @@ -17,6 +17,26 @@ export default function Cart() { const total = items.reduce((sum, ci) => sum + ci.quantity * ci.item.price, 0); + const handleCheckout = async () => { + try { + await createOrder({ delivery_address: "Уточняется" }); + alert("Заказ оформлен"); + setItems([]); + navigate("/shop"); + } catch { + alert("Не удалось оформить заказ"); + } + }; + + const handleRemove = async (id) => { + try { + await removeFromCart(id); + setItems((prev) => prev.filter((ci) => ci.id !== id)); + } catch { + alert("Не удалось удалить"); + } + }; + if (loading) { return ( @@ -38,16 +58,40 @@ export default function Cart() { {items.map((ci) => (
- {ci.item.name} - - {ci.quantity} x {ci.item.price}р. ={" "} - {ci.quantity * ci.item.price}р. - + {ci.item.name} +
+

{ci.item.name}

+

+ {ci.quantity} {ci.item.unit} x {ci.item.price}р. +

+
+
+ {ci.quantity * ci.item.price} р. +
+ +
))} -
Итого: {total}р.
+
+ Итого: {total}р. + +
)}