mirror of
https://github.com/vercel/commerce.git
synced 2025-06-01 14:06:58 +00:00
add rm update cart
This commit is contained in:
parent
868f250705
commit
4bb59981b4
@ -1,8 +1,7 @@
|
|||||||
'use server';
|
'use server';
|
||||||
|
|
||||||
import { TAGS } from 'lib/constants';
|
import { TAGS } from 'lib/constants';
|
||||||
import { getCart } from 'lib/fourthwall';
|
import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/fourthwall';
|
||||||
import { addToCart, createCart, removeFromCart, updateCart } from 'lib/shopify';
|
|
||||||
import { revalidateTag } from 'next/cache';
|
import { revalidateTag } from 'next/cache';
|
||||||
import { cookies } from 'next/headers';
|
import { cookies } from 'next/headers';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
@ -102,6 +102,78 @@ export async function getCart(cartId: string | undefined): Promise<Cart | undefi
|
|||||||
return reshapeCart(res.body);
|
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
|
* TODO: Stubbed out
|
||||||
|
@ -4,12 +4,6 @@ import { ensureStartsWith } from 'lib/utils';
|
|||||||
import { revalidateTag } from 'next/cache';
|
import { revalidateTag } from 'next/cache';
|
||||||
import { headers } from 'next/headers';
|
import { headers } from 'next/headers';
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
import {
|
|
||||||
addToCartMutation,
|
|
||||||
createCartMutation,
|
|
||||||
editCartItemsMutation,
|
|
||||||
removeFromCartMutation
|
|
||||||
} from './mutations/cart';
|
|
||||||
import {
|
import {
|
||||||
getCollectionQuery,
|
getCollectionQuery,
|
||||||
getCollectionsQuery
|
getCollectionsQuery
|
||||||
@ -27,20 +21,16 @@ import {
|
|||||||
Image,
|
Image,
|
||||||
Page,
|
Page,
|
||||||
Product,
|
Product,
|
||||||
ShopifyAddToCartOperation,
|
|
||||||
ShopifyCart,
|
ShopifyCart,
|
||||||
ShopifyCollection,
|
ShopifyCollection,
|
||||||
ShopifyCollectionOperation,
|
ShopifyCollectionOperation,
|
||||||
ShopifyCollectionsOperation,
|
ShopifyCollectionsOperation,
|
||||||
ShopifyCreateCartOperation,
|
|
||||||
ShopifyPageOperation,
|
ShopifyPageOperation,
|
||||||
ShopifyPagesOperation,
|
ShopifyPagesOperation,
|
||||||
ShopifyProduct,
|
ShopifyProduct,
|
||||||
ShopifyProductOperation,
|
ShopifyProductOperation,
|
||||||
ShopifyProductRecommendationsOperation,
|
ShopifyProductRecommendationsOperation,
|
||||||
ShopifyProductsOperation,
|
ShopifyProductsOperation
|
||||||
ShopifyRemoveFromCartOperation,
|
|
||||||
ShopifyUpdateCartOperation
|
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
const domain = process.env.SHOPIFY_STORE_DOMAIN
|
const domain = process.env.SHOPIFY_STORE_DOMAIN
|
||||||
@ -194,59 +184,6 @@ const reshapeProducts = (products: ShopifyProduct[]) => {
|
|||||||
return reshapedProducts;
|
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> {
|
export async function getCollection(handle: string): Promise<Collection | undefined> {
|
||||||
const res = await shopifyFetch<ShopifyCollectionOperation>({
|
const res = await shopifyFetch<ShopifyCollectionOperation>({
|
||||||
query: getCollectionQuery,
|
query: getCollectionQuery,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user