diff --git a/components/cart/actions.ts b/components/cart/actions.ts index 4b619dd2a..a0b8d64f2 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -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'; diff --git a/lib/fourthwall/index.ts b/lib/fourthwall/index.ts index 1b5c71bf7..82793462f 100644 --- a/lib/fourthwall/index.ts +++ b/lib/fourthwall/index.ts @@ -102,6 +102,78 @@ export async function getCart(cartId: string | undefined): Promise { + const res = await fourthwallPost(`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 { + + const items = lines.map((line) => ({ + variantId: line.merchandiseId, + quantity: line.quantity + })); + + const res = await fourthwallPost(`${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 { + const items = lineIds.map((id) => ({ + variantId: id + })); + + const res = await fourthwallPost(`${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 { + const items = lines.map((line) => ({ + variantId: line.merchandiseId, + quantity: line.quantity + })); + + const res = await fourthwallPost(`${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 diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index 887e11988..52fb8029e 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -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 { - const res = await shopifyFetch({ - query: createCartMutation, - cache: 'no-store' - }); - - return reshapeCart(res.body.data.cartCreate.cart); -} - -export async function addToCart( - cartId: string, - lines: { merchandiseId: string; quantity: number }[] -): Promise { - const res = await shopifyFetch({ - query: addToCartMutation, - variables: { - cartId, - lines - }, - cache: 'no-store' - }); - return reshapeCart(res.body.data.cartLinesAdd.cart); -} - -export async function removeFromCart(cartId: string, lineIds: string[]): Promise { - const res = await shopifyFetch({ - 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 { - const res = await shopifyFetch({ - query: editCartItemsMutation, - variables: { - cartId, - lines - }, - cache: 'no-store' - }); - - return reshapeCart(res.body.data.cartLinesUpdate.cart); -} - export async function getCollection(handle: string): Promise { const res = await shopifyFetch({ query: getCollectionQuery,