From bd71225a23e2a3198e1ee19c3dfc986d2b59c0d7 Mon Sep 17 00:00:00 2001 From: andrei Date: Sat, 5 Oct 2024 11:06:22 +0500 Subject: [PATCH] ._. --- src/AppRouter.jsx | 2 + src/api.jsx | 63 ++++++++++++ src/components/Header.jsx | 5 + src/pages/Accessories.jsx | 210 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 280 insertions(+) create mode 100644 src/pages/Accessories.jsx diff --git a/src/AppRouter.jsx b/src/AppRouter.jsx index f19e626..95834a1 100755 --- a/src/AppRouter.jsx +++ b/src/AppRouter.jsx @@ -1,6 +1,7 @@ import { Routes, Route, Navigate } from "react-router-dom"; import Login from "./pages/Login"; import Home from "./pages/Home.jsx"; +import Accessories from "./pages/Accessories.jsx"; import PrivateRoute from "./components/PrivateRoute"; const RoutesComponent = () => ( @@ -8,6 +9,7 @@ const RoutesComponent = () => ( } /> }> } /> + } /> ); diff --git a/src/api.jsx b/src/api.jsx index fc873f9..3886247 100755 --- a/src/api.jsx +++ b/src/api.jsx @@ -32,3 +32,66 @@ export const loginUser = async (loginData) => { throw error.response ? error.response.data : error; } }; + +export const getAccessories = async () => { + try { + const response = await axios.get(`${API_URL}/accessories`, { + headers: { + Authorization: `Bearer ${getAuthToken()}`, + Accept: "application/json", + }, + }); + return response.data; + } catch (error) { + console.log("Ошибка при получении аксессуаров:", error); + throw error; + } +}; + +export const createAccessory = async (accessoryData) => { + try { + const response = await axios.post(`${API_URL}/accessories`, accessoryData, { + headers: { + Authorization: `Bearer ${getAuthToken()}`, + "Content-Type": "application/json", + }, + }); + return response.data; + } catch (error) { + console.log("Ошибка при создании аксессуара:", error); + throw error; + } +}; + +export const updateAccessory = async (id, accessoryData) => { + try { + const response = await axios.put( + `${API_URL}/accessories/${id}`, + accessoryData, + { + headers: { + Authorization: `Bearer ${getAuthToken()}`, + "Content-Type": "application/json", + }, + } + ); + return response.data; + } catch (error) { + console.log("Ошибка при обновлении аксессуара:", error); + throw error; + } +}; + +export const deleteAccessory = async (id) => { + try { + await axios.delete(`${API_URL}/accessories/${id}`, { + headers: { + Authorization: `Bearer ${getAuthToken()}`, + Accept: "application/json", + }, + }); + } catch (error) { + console.log("Ошибка при удалении аксессуара:", error); + throw error; + } +}; diff --git a/src/components/Header.jsx b/src/components/Header.jsx index 842f407..6285c4e 100644 --- a/src/components/Header.jsx +++ b/src/components/Header.jsx @@ -25,6 +25,11 @@ const Header = () => { Заказы +
  • + + Компоненты + +
  • {isAuthenticated ? ( diff --git a/src/pages/Accessories.jsx b/src/pages/Accessories.jsx new file mode 100644 index 0000000..2f5a09e --- /dev/null +++ b/src/pages/Accessories.jsx @@ -0,0 +1,210 @@ +import React, { useState, useEffect } from "react"; +import { + getAccessories, + createAccessory, + updateAccessory, + deleteAccessory, +} from "../api.jsx"; + +const Accessories = () => { + const [accessories, setAccessories] = useState([]); + const [newAccessory, setNewAccessory] = useState({ + name: "", + volume: "", + weight: "", + period: "", + city_id: "", + }); + const [editingAccessoryId, setEditingAccessoryId] = useState(null); + const [error, setError] = useState(null); + + useEffect(() => { + fetchAccessories(); + }, []); + + const fetchAccessories = async () => { + try { + const data = await getAccessories(); + setAccessories(data); + } catch (error) { + console.error("Ошибка при загрузке аксессуаров:", error); + } + }; + + const handleInputChange = (e) => { + const { name, value } = e.target; + setNewAccessory({ ...newAccessory, [name]: value }); + }; + + const handleSubmit = async (e) => { + e.preventDefault(); + + if ( + !newAccessory.name || + !newAccessory.volume || + !newAccessory.weight || + !newAccessory.period + ) { + setError("Пожалуйста, заполните все поля."); + return; + } + + try { + if (editingAccessoryId) { + await updateAccessory(editingAccessoryId, newAccessory); + } else { + await createAccessory(newAccessory); + } + fetchAccessories(); + resetForm(); + } catch (error) { + console.error("Ошибка при добавлении или обновлении аксессуара:", error); + } + }; + + const handleEdit = (accessory) => { + setNewAccessory({ + name: accessory.name, + volume: accessory.volume, + weight: accessory.weight, + period: accessory.period, + city_id: accessory.city_id, + }); + setEditingAccessoryId(accessory.id); + }; + + const handleDelete = async (accessoryId) => { + try { + await deleteAccessory(accessoryId); + fetchAccessories(); + } catch (error) { + console.error("Ошибка при удалении аксессуара:", error); + } + }; + + const resetForm = () => { + setNewAccessory({ + name: "", + volume: "", + weight: "", + period: "", + city_id: "", + }); + setEditingAccessoryId(null); + }; + + return ( +
    +

    Аксессуары

    +
    +
    + + +
    +
    + + +
    +
    + + +
    +
    + + +
    +
    + + +
    + {error && ( +
    + {error} +
    + )} +
    +

    Список аксессуаров

    + + + + + + + + + + + + + {accessories.map((accessory) => ( + + + + + + + + + ))} + +
    IDНазваниеОбъемВесПериодДействия
    {accessory.id}{accessory.name}{accessory.volume}{accessory.weight}{accessory.period} +
    + + +
    +
    +
    + ); +}; + +export default Accessories;