2023-06-17 13:18:00 -05:00
|
|
|
'use server';
|
|
|
|
|
2023-10-10 21:45:55 -05:00
|
|
|
import { TAGS } from 'lib/constants';
|
2025-02-09 11:38:22 -06:00
|
|
|
import {
|
|
|
|
addToCart,
|
|
|
|
createCart,
|
|
|
|
getCart,
|
|
|
|
removeFromCart,
|
|
|
|
updateCart
|
|
|
|
} from 'lib/shopify';
|
2023-10-10 21:45:55 -05:00
|
|
|
import { revalidateTag } from 'next/cache';
|
2023-06-17 13:18:00 -05:00
|
|
|
import { cookies } from 'next/headers';
|
2024-07-25 13:56:53 -05:00
|
|
|
import { redirect } from 'next/navigation';
|
2023-06-17 13:18:00 -05:00
|
|
|
|
2025-02-09 11:38:22 -06:00
|
|
|
export async function addItem(
|
|
|
|
prevState: any,
|
|
|
|
selectedVariantId: string | undefined
|
|
|
|
) {
|
|
|
|
if (!selectedVariantId) {
|
2024-07-28 22:58:59 -05:00
|
|
|
return 'Error adding item to cart';
|
2023-06-17 13:18:00 -05:00
|
|
|
}
|
2023-08-04 22:21:57 -05:00
|
|
|
|
2023-06-17 13:18:00 -05:00
|
|
|
try {
|
2025-02-09 11:38:22 -06:00
|
|
|
await addToCart([{ merchandiseId: selectedVariantId, quantity: 1 }]);
|
2023-10-10 21:45:55 -05:00
|
|
|
revalidateTag(TAGS.cart);
|
2023-06-17 13:18:00 -05:00
|
|
|
} catch (e) {
|
2023-08-04 22:21:57 -05:00
|
|
|
return 'Error adding item to cart';
|
2023-06-17 13:18:00 -05:00
|
|
|
}
|
2023-10-10 21:45:55 -05:00
|
|
|
}
|
2023-06-17 13:18:00 -05:00
|
|
|
|
2024-07-28 22:58:59 -05:00
|
|
|
export async function removeItem(prevState: any, merchandiseId: string) {
|
2023-06-17 13:18:00 -05:00
|
|
|
try {
|
2025-02-09 11:38:22 -06:00
|
|
|
const cart = await getCart();
|
2024-07-28 22:58:59 -05:00
|
|
|
|
|
|
|
if (!cart) {
|
|
|
|
return 'Error fetching cart';
|
|
|
|
}
|
|
|
|
|
2025-02-09 11:38:22 -06:00
|
|
|
const lineItem = cart.lines.find(
|
|
|
|
(line) => line.merchandise.id === merchandiseId
|
|
|
|
);
|
2024-07-28 22:58:59 -05:00
|
|
|
|
|
|
|
if (lineItem && lineItem.id) {
|
2025-02-09 11:38:22 -06:00
|
|
|
await removeFromCart([lineItem.id]);
|
2024-07-28 22:58:59 -05:00
|
|
|
revalidateTag(TAGS.cart);
|
|
|
|
} else {
|
|
|
|
return 'Item not found in cart';
|
|
|
|
}
|
2023-06-17 13:18:00 -05:00
|
|
|
} catch (e) {
|
2023-08-04 22:21:57 -05:00
|
|
|
return 'Error removing item from cart';
|
2023-06-17 13:18:00 -05:00
|
|
|
}
|
2023-10-10 21:45:55 -05:00
|
|
|
}
|
2023-06-17 13:18:00 -05:00
|
|
|
|
2023-10-10 21:45:55 -05:00
|
|
|
export async function updateItemQuantity(
|
|
|
|
prevState: any,
|
|
|
|
payload: {
|
2024-07-28 22:58:59 -05:00
|
|
|
merchandiseId: string;
|
2023-10-10 21:45:55 -05:00
|
|
|
quantity: number;
|
|
|
|
}
|
|
|
|
) {
|
2024-07-28 22:58:59 -05:00
|
|
|
const { merchandiseId, quantity } = payload;
|
2023-10-10 21:45:55 -05:00
|
|
|
|
2023-06-17 13:18:00 -05:00
|
|
|
try {
|
2025-02-09 11:38:22 -06:00
|
|
|
const cart = await getCart();
|
2024-07-28 22:58:59 -05:00
|
|
|
|
|
|
|
if (!cart) {
|
|
|
|
return 'Error fetching cart';
|
2023-10-10 21:45:55 -05:00
|
|
|
}
|
|
|
|
|
2025-02-09 11:38:22 -06:00
|
|
|
const lineItem = cart.lines.find(
|
|
|
|
(line) => line.merchandise.id === merchandiseId
|
|
|
|
);
|
2024-07-28 22:58:59 -05:00
|
|
|
|
|
|
|
if (lineItem && lineItem.id) {
|
|
|
|
if (quantity === 0) {
|
2025-02-09 11:38:22 -06:00
|
|
|
await removeFromCart([lineItem.id]);
|
2024-07-28 22:58:59 -05:00
|
|
|
} else {
|
2025-02-09 11:38:22 -06:00
|
|
|
await updateCart([
|
2024-07-28 22:58:59 -05:00
|
|
|
{
|
|
|
|
id: lineItem.id,
|
|
|
|
merchandiseId,
|
|
|
|
quantity
|
|
|
|
}
|
|
|
|
]);
|
2023-06-17 13:18:00 -05:00
|
|
|
}
|
2024-07-28 22:58:59 -05:00
|
|
|
} else if (quantity > 0) {
|
|
|
|
// If the item doesn't exist in the cart and quantity > 0, add it
|
2025-02-09 11:38:22 -06:00
|
|
|
await addToCart([{ merchandiseId, quantity }]);
|
2024-07-28 22:58:59 -05:00
|
|
|
}
|
|
|
|
|
2023-10-10 21:45:55 -05:00
|
|
|
revalidateTag(TAGS.cart);
|
2023-06-17 13:18:00 -05:00
|
|
|
} catch (e) {
|
2024-07-28 22:58:59 -05:00
|
|
|
console.error(e);
|
2023-08-04 22:21:57 -05:00
|
|
|
return 'Error updating item quantity';
|
2023-06-17 13:18:00 -05:00
|
|
|
}
|
2023-10-10 21:45:55 -05:00
|
|
|
}
|
2024-07-25 13:56:53 -05:00
|
|
|
|
2024-07-28 22:58:59 -05:00
|
|
|
export async function redirectToCheckout() {
|
2025-02-09 11:38:22 -06:00
|
|
|
let cart = await getCart();
|
2024-12-06 08:23:35 -06:00
|
|
|
redirect(cart!.checkoutUrl);
|
2024-07-28 22:58:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function createCartAndSetCookie() {
|
|
|
|
let cart = await createCart();
|
2024-10-15 22:07:55 -05:00
|
|
|
(await cookies()).set('cartId', cart.id!);
|
2024-07-25 13:56:53 -05:00
|
|
|
}
|