mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 22:16:58 +00:00
72 lines
1.6 KiB
TypeScript
72 lines
1.6 KiB
TypeScript
'use server';
|
|
|
|
import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify';
|
|
import { cookies } from 'next/headers';
|
|
|
|
export const addItem = async (variantId: string | undefined): Promise<String | undefined> => {
|
|
// let cartId = cookies().get('cartId')?.value;
|
|
// let cart;
|
|
//
|
|
// if (cartId) {
|
|
// cart = await getCart(cartId);
|
|
// }
|
|
//
|
|
// if (!cartId || !cart) {
|
|
// cart = await createCart();
|
|
// cartId = cart.id;
|
|
// // TODO: this is not working under older Next.js versions
|
|
// // cookies().set('cartId', cartId);
|
|
// }
|
|
//
|
|
// if (!variantId) {
|
|
// return 'Missing product variant ID';
|
|
// }
|
|
//
|
|
// try {
|
|
// await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }]);
|
|
// } catch (e) {
|
|
// return 'Error adding item to cart';
|
|
// }
|
|
return undefined;
|
|
};
|
|
|
|
export const removeItem = async (lineId: string): Promise<String | undefined> => {
|
|
const cartId = cookies().get('cartId')?.value;
|
|
|
|
if (!cartId) {
|
|
return 'Missing cart ID';
|
|
}
|
|
try {
|
|
await removeFromCart(cartId, [lineId]);
|
|
} catch (e) {
|
|
return 'Error removing item from cart';
|
|
}
|
|
};
|
|
|
|
export const updateItemQuantity = async ({
|
|
lineId,
|
|
variantId,
|
|
quantity
|
|
}: {
|
|
lineId: string;
|
|
variantId: string;
|
|
quantity: number;
|
|
}): Promise<String | undefined> => {
|
|
const cartId = cookies().get('cartId')?.value;
|
|
|
|
if (!cartId) {
|
|
return 'Missing cart ID';
|
|
}
|
|
try {
|
|
await updateCart(cartId, [
|
|
{
|
|
id: lineId,
|
|
merchandiseId: variantId,
|
|
quantity
|
|
}
|
|
]);
|
|
} catch (e) {
|
|
return 'Error updating item quantity';
|
|
}
|
|
};
|