feat: add cart on front

This commit is contained in:
2026-06-23 20:38:41 +03:00
parent d9745d972a
commit 733e3f6208
5 changed files with 31 additions and 8 deletions
+2 -2
View File
@@ -6,8 +6,8 @@ import (
type CartItem struct { type CartItem struct {
ID uint `json:"id" gorm:"primaryKey"` ID uint `json:"id" gorm:"primaryKey"`
UserID uint `json:"user_id" gorm:"not null;uniqueIndex;idx_user_item"` UserID uint `json:"user_id" gorm:"not null;uniqueIndex:idx_user_item"`
ItemID uint `json:"item_id" gorm:"not null;uniqueIndex;idx_user_item"` ItemID uint `json:"item_id" gorm:"not null;uniqueIndex:idx_user_item"`
Quantity float64 `json:"quantity" gorm:"not null;default:1"` Quantity float64 `json:"quantity" gorm:"not null;default:1"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
+4 -1
View File
@@ -77,7 +77,10 @@ export default function HeaderDesktop() {
/> />
</svg> </svg>
</button> </button>
<button className="text-slate-200 hover:text-gold transition-colors duration-200"> <button
className="text-slate-200 hover:text-gold transition-colors duration-200"
onClick={() => navigate("/cart")}
>
<svg <svg
width="24" width="24"
height="24" height="24"
+4 -1
View File
@@ -55,7 +55,10 @@ export default function HeaderMobile() {
/> />
</svg> </svg>
</button> </button>
<button className="text-slate-200 hover:text-gold transition-colors duration-200"> <button
className="text-slate-200 hover:text-gold transition-colors duration-200"
onClick={() => navigate("/cart")}
>
<svg <svg
width="24" width="24"
height="24" height="24"
+2 -2
View File
@@ -42,8 +42,8 @@ export default function Cart() {
> >
<span className="font-medium ">{ci.item.name}</span> <span className="font-medium ">{ci.item.name}</span>
<span> <span>
{ci.quanitity} x {ci.item.price} ={" "} {ci.quantity} x {ci.item.price}р. ={" "}
{ci.quantity * ci.item.price} {ci.quantity * ci.item.price}р.
</span> </span>
</div> </div>
))} ))}
+19 -2
View File
@@ -34,10 +34,27 @@ export default function Shop() {
}); });
}, [items, activeCategory]); }, [items, activeCategory]);
const handleAddToCart = (product, selectedWeight) => { const handleAddToCart = async (product, selectedWeight) => {
console.log(`Добавлен товар ${product.name}, Вес ${selectedWeight} кг`); try {
await api.addToCart({
item_id: product.id,
quanity: selectedWeight,
});
alert(`${product.name} добавлен в корзину`);
} catch (err) {
console.error("Ошибка при добавлении товара в корзину", err);
alert("Не удалось добавить товар в корзину");
}
}; };
if (loading) {
return(
<PageLayout>
<p className="text-white grow">Загрузка...</p>
</PageLayout>
)
}
return ( return (
<PageLayout> <PageLayout>
<main className="px-6 md:px-16 grow w-full "> <main className="px-6 md:px-16 grow w-full ">