mirror of
https://github.com/vercel/commerce.git
synced 2025-05-13 13:17:51 +00:00
87 lines
1.8 KiB
TypeScript
87 lines
1.8 KiB
TypeScript
'use server';
|
|
|
|
import { TAGS } from 'lib/constants';
|
|
import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify';
|
|
import { revalidateTag } from 'next/cache';
|
|
import { cookies } from 'next/headers';
|
|
|
|
export async function addItem(prevState: any, selectedVariantIds: Array<string>) {
|
|
let cartId = cookies().get('cartId')?.value;
|
|
let cart;
|
|
|
|
if (cartId) {
|
|
cart = await getCart(cartId);
|
|
}
|
|
|
|
if (!cartId || !cart) {
|
|
cart = await createCart();
|
|
cartId = cart.id;
|
|
cookies().set('cartId', cartId);
|
|
}
|
|
|
|
if (!selectedVariantIds.length) {
|
|
return 'Missing product variant ID';
|
|
}
|
|
|
|
try {
|
|
await addToCart(
|
|
cartId,
|
|
selectedVariantIds.map((variantId) => ({ merchandiseId: variantId, quantity: 1 }))
|
|
);
|
|
revalidateTag(TAGS.cart);
|
|
} catch (e) {
|
|
return 'Error adding item to cart';
|
|
}
|
|
}
|
|
|
|
export async function removeItem(prevState: any, lineId: string) {
|
|
const cartId = cookies().get('cartId')?.value;
|
|
|
|
if (!cartId) {
|
|
return 'Missing cart ID';
|
|
}
|
|
|
|
try {
|
|
await removeFromCart(cartId, [lineId]);
|
|
revalidateTag(TAGS.cart);
|
|
} catch (e) {
|
|
return 'Error removing item from cart';
|
|
}
|
|
}
|
|
|
|
export async function updateItemQuantity(
|
|
prevState: any,
|
|
payload: {
|
|
lineId: string;
|
|
variantId: string;
|
|
quantity: number;
|
|
}
|
|
) {
|
|
const cartId = cookies().get('cartId')?.value;
|
|
|
|
if (!cartId) {
|
|
return 'Missing cart ID';
|
|
}
|
|
|
|
const { lineId, variantId, quantity } = payload;
|
|
|
|
try {
|
|
if (quantity === 0) {
|
|
await removeFromCart(cartId, [lineId]);
|
|
revalidateTag(TAGS.cart);
|
|
return;
|
|
}
|
|
|
|
await updateCart(cartId, [
|
|
{
|
|
id: lineId,
|
|
merchandiseId: variantId,
|
|
quantity
|
|
}
|
|
]);
|
|
revalidateTag(TAGS.cart);
|
|
} catch (e) {
|
|
return 'Error updating item quantity';
|
|
}
|
|
}
|