winofy-front/src/lib/api/cart.ts
2026-08-19 13:01:19 +03:30

38 lines
1.4 KiB
TypeScript

"use server";
import { revalidatePath } from "next/cache";
import { winofyFetch } from "./winofy";
import { callApi, type ActionResult } from "./result";
import type { Cart } from "@/types/api";
export async function getCart(): Promise<Cart> {
return winofyFetch<Cart>("/v1/cart/");
}
export async function addToCart(productUuid: string, quantity: number = 1): Promise<ActionResult> {
const result = await callApi(() =>
winofyFetch<undefined>("/v1/cart/items/", { method: "POST", body: { product_uuid: productUuid, quantity } }),
);
if (result.ok) revalidatePath("/cart");
return result;
}
export async function updateCartItemQuantity(itemUuid: string, quantity: number): Promise<ActionResult> {
const result = await callApi(() =>
winofyFetch<undefined>(`/v1/cart/items/${itemUuid}/`, { method: "PATCH", body: { quantity } }),
);
if (result.ok) revalidatePath("/cart");
return result;
}
export async function removeCartItem(itemUuid: string): Promise<ActionResult> {
const result = await callApi(() => winofyFetch<undefined>(`/v1/cart/items/${itemUuid}/`, { method: "DELETE" }));
if (result.ok) revalidatePath("/cart");
return result;
}
export async function clearCart(): Promise<ActionResult> {
const result = await callApi(() => winofyFetch<undefined>("/v1/cart/", { method: "DELETE" }));
if (result.ok) revalidatePath("/cart");
return result;
}