add rm update cart

This commit is contained in:
Jieren Chen 2024-09-08 12:20:03 -04:00
parent 868f250705
commit 4bb59981b4
No known key found for this signature in database
GPG Key ID: 2FF322D21B5DB10B
3 changed files with 74 additions and 66 deletions

View File

@ -1,8 +1,7 @@
'use server';
import { TAGS } from 'lib/constants';
import { getCart } from 'lib/fourthwall';
import { addToCart, createCart, removeFromCart, updateCart } from 'lib/shopify';
import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/fourthwall';
import { revalidateTag } from 'next/cache';
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';

View File

@ -102,6 +102,78 @@ export async function getCart(cartId: string | undefined): Promise<Cart | undefi
return reshapeCart(res.body);
}
export async function createCart(): Promise<Cart> {
const res = await fourthwallPost<FourthwallCart>(`https://api.staging.fourthwall.com/api/public/v1.0/carts?secret=${process.env.FW_SECRET}`, {
items: []
}, {
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
}
});
return reshapeCart(res.body);
}
export async function addToCart(
cartId: string,
lines: { merchandiseId: string; quantity: number }[]
): Promise<Cart> {
const items = lines.map((line) => ({
variantId: line.merchandiseId,
quantity: line.quantity
}));
const res = await fourthwallPost<FourthwallCart>(`${process.env.FW_URL}/api/public/v1.0/carts/${cartId}/add?secret=${process.env.FW_SECRET}`, {
items,
}, {
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
},
cache: 'no-store'
});
return reshapeCart(res.body);
}
export async function removeFromCart(cartId: string, lineIds: string[]): Promise<Cart> {
const items = lineIds.map((id) => ({
variantId: id
}));
const res = await fourthwallPost<FourthwallCart>(`${process.env.FW_URL}/api/public/v1.0/carts/${cartId}/remove?secret=${process.env.FW_SECRET}`, {
items,
}, {
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
},
cache: 'no-store'
});
return reshapeCart(res.body);
}
export async function updateCart(
cartId: string,
lines: { id: string; merchandiseId: string; quantity: number }[]
): Promise<Cart> {
const items = lines.map((line) => ({
variantId: line.merchandiseId,
quantity: line.quantity
}));
const res = await fourthwallPost<FourthwallCart>(`${process.env.FW_URL}/api/public/v1.0/carts/${cartId}/change?secret=${process.env.FW_SECRET}`, {
items,
}, {
headers: {
'X-ShopId': process.env.FW_SHOPID || ''
},
cache: 'no-store'
});
return reshapeCart(res.body);
}
/**
* TODO: Stubbed out

View File

@ -4,12 +4,6 @@ import { ensureStartsWith } from 'lib/utils';
import { revalidateTag } from 'next/cache';
import { headers } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';
import {
addToCartMutation,
createCartMutation,
editCartItemsMutation,
removeFromCartMutation
} from './mutations/cart';
import {
getCollectionQuery,
getCollectionsQuery
@ -27,20 +21,16 @@ import {
Image,
Page,
Product,
ShopifyAddToCartOperation,
ShopifyCart,
ShopifyCollection,
ShopifyCollectionOperation,
ShopifyCollectionsOperation,
ShopifyCreateCartOperation,
ShopifyPageOperation,
ShopifyPagesOperation,
ShopifyProduct,
ShopifyProductOperation,
ShopifyProductRecommendationsOperation,
ShopifyProductsOperation,
ShopifyRemoveFromCartOperation,
ShopifyUpdateCartOperation
ShopifyProductsOperation
} from './types';
const domain = process.env.SHOPIFY_STORE_DOMAIN
@ -194,59 +184,6 @@ const reshapeProducts = (products: ShopifyProduct[]) => {
return reshapedProducts;
};
export async function createCart(): Promise<Cart> {
const res = await shopifyFetch<ShopifyCreateCartOperation>({
query: createCartMutation,
cache: 'no-store'
});
return reshapeCart(res.body.data.cartCreate.cart);
}
export async function addToCart(
cartId: string,
lines: { merchandiseId: string; quantity: number }[]
): Promise<Cart> {
const res = await shopifyFetch<ShopifyAddToCartOperation>({
query: addToCartMutation,
variables: {
cartId,
lines
},
cache: 'no-store'
});
return reshapeCart(res.body.data.cartLinesAdd.cart);
}
export async function removeFromCart(cartId: string, lineIds: string[]): Promise<Cart> {
const res = await shopifyFetch<ShopifyRemoveFromCartOperation>({
query: removeFromCartMutation,
variables: {
cartId,
lineIds
},
cache: 'no-store'
});
return reshapeCart(res.body.data.cartLinesRemove.cart);
}
export async function updateCart(
cartId: string,
lines: { id: string; merchandiseId: string; quantity: number }[]
): Promise<Cart> {
const res = await shopifyFetch<ShopifyUpdateCartOperation>({
query: editCartItemsMutation,
variables: {
cartId,
lines
},
cache: 'no-store'
});
return reshapeCart(res.body.data.cartLinesUpdate.cart);
}
export async function getCollection(handle: string): Promise<Collection | undefined> {
const res = await shopifyFetch<ShopifyCollectionOperation>({
query: getCollectionQuery,