28 lines
728 B
TypeScript
28 lines
728 B
TypeScript
"use client";
|
||
|
||
import { useState } from "react";
|
||
|
||
export function AddToCart({ productId }: { productId: number }) {
|
||
const [message, setMessage] = useState("");
|
||
|
||
async function add() {
|
||
setMessage("Добавляю…");
|
||
const response = await fetch("/backend/cart/items", {
|
||
method: "POST",
|
||
credentials: "include",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ product_id: productId, qty: 1 }),
|
||
});
|
||
setMessage(response.ok ? "В корзине" : "Не удалось добавить");
|
||
}
|
||
|
||
return (
|
||
<p>
|
||
<button type="button" onClick={add}>
|
||
В корзину
|
||
</button>{" "}
|
||
{message}
|
||
</p>
|
||
);
|
||
}
|