lab_6_start

This commit is contained in:
2025-05-23 21:47:29 +02:00
parent d1b2cea66c
commit 038f60d61e
13 changed files with 339 additions and 44 deletions

28
src/pages/MainPage.jsx Normal file
View File

@@ -0,0 +1,28 @@
import React, { useState } from "react";
import useProducts from "../hooks/useProducts";
import ProductList from "../components/ProductList";
import ProductForm from "../components/ProductForm";
export default function MainPage() {
const { products, add, update, remove } = useProducts();
const [editing, setEditing] = useState(null);
const [showForm, setShowForm] = useState(false);
const handleAdd = () => { setEditing(null); setShowForm(true); };
const handleEdit = prod => { setEditing(prod); setShowForm(true); };
const handleDelete = id => remove(id);
const handleSave = prod => {
editing ? update({ ...prod, id: editing.id }) : add(prod);
setShowForm(false);
};
const handleCancel = () => setShowForm(false);
return (
<main className="container my-4">
<button className="btn btn-success mb-3" onClick={handleAdd}>Добавить товар</button>
{showForm && <ProductForm initial={editing} onSave={handleSave} onCancel={handleCancel} />}
<h2 className="text-center my-3">Рекомендуемые товары:</h2>
<ProductList products={products} onEdit={handleEdit} onDelete={handleDelete} />
</main>
);
}