feat: add cart on front
This commit is contained in:
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
type CartItem struct {
|
||||
ID uint `json:"id" gorm:"primaryKey"`
|
||||
UserID uint `json:"user_id" gorm:"not null;uniqueIndex;idx_user_item"`
|
||||
ItemID uint `json:"item_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"`
|
||||
Quantity float64 `json:"quantity" gorm:"not null;default:1"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
@@ -77,7 +77,10 @@ export default function HeaderDesktop() {
|
||||
/>
|
||||
</svg>
|
||||
</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
|
||||
width="24"
|
||||
height="24"
|
||||
|
||||
@@ -55,7 +55,10 @@ export default function HeaderMobile() {
|
||||
/>
|
||||
</svg>
|
||||
</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
|
||||
width="24"
|
||||
height="24"
|
||||
|
||||
@@ -42,8 +42,8 @@ export default function Cart() {
|
||||
>
|
||||
<span className="font-medium ">{ci.item.name}</span>
|
||||
<span>
|
||||
{ci.quanitity} x {ci.item.price} ={" "}
|
||||
{ci.quantity * ci.item.price}
|
||||
{ci.quantity} x {ci.item.price}р. ={" "}
|
||||
{ci.quantity * ci.item.price}р.
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -34,10 +34,27 @@ export default function Shop() {
|
||||
});
|
||||
}, [items, activeCategory]);
|
||||
|
||||
const handleAddToCart = (product, selectedWeight) => {
|
||||
console.log(`Добавлен товар ${product.name}, Вес ${selectedWeight} кг`);
|
||||
const handleAddToCart = async (product, 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 (
|
||||
<PageLayout>
|
||||
<main className="px-6 md:px-16 grow w-full ">
|
||||
|
||||
Reference in New Issue
Block a user