mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 23:46:58 +00:00
fix: simplify logic core-charge
Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
This commit is contained in:
parent
0471e07633
commit
3cdc94d0ce
@ -17,23 +17,17 @@ type MerchandiseSearchParams = {
|
|||||||
[key: string]: string;
|
[key: string]: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const CoreCharge = ({
|
const CoreCharge = ({ coreCharge, quantity }: { coreCharge?: CartItem; quantity: number }) => {
|
||||||
coreCharge,
|
|
||||||
quantity
|
|
||||||
}: {
|
|
||||||
coreCharge: CartItem['coreCharge'];
|
|
||||||
quantity: number;
|
|
||||||
}) => {
|
|
||||||
if (!coreCharge) return null;
|
if (!coreCharge) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="ml-20 mt-2 flex flex-row items-center">
|
<div className="ml-20 mt-2 flex flex-row items-center">
|
||||||
<PlusIcon className="mr-1.5 size-3" />
|
<PlusIcon className="mr-1.5 size-3" />
|
||||||
<div className="flex flex-row items-center justify-start gap-2">
|
<div className="flex flex-row items-center justify-start gap-2">
|
||||||
{coreCharge.selectedOptions[0] ? (
|
{coreCharge.merchandise.selectedOptions[0] ? (
|
||||||
<Price
|
<Price
|
||||||
className="text-xs font-medium"
|
className="text-xs font-medium"
|
||||||
amount={coreCharge.selectedOptions[0].value}
|
amount={coreCharge.merchandise.selectedOptions[0].value}
|
||||||
currencyCode="USD"
|
currencyCode="USD"
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
|
@ -40,6 +40,9 @@ const cartFragment = /* GraphQL */ `
|
|||||||
product {
|
product {
|
||||||
...product
|
...product
|
||||||
}
|
}
|
||||||
|
coreVariantId: metafield(key: "coreVariant", namespace: "custom") {
|
||||||
|
value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,14 +35,12 @@ import { getPageQuery, getPagesQuery } from './queries/page';
|
|||||||
import {
|
import {
|
||||||
getProductQuery,
|
getProductQuery,
|
||||||
getProductRecommendationsQuery,
|
getProductRecommendationsQuery,
|
||||||
getProductVariantQuery,
|
|
||||||
getProductsQuery
|
getProductsQuery
|
||||||
} from './queries/product';
|
} from './queries/product';
|
||||||
import {
|
import {
|
||||||
Cart,
|
Cart,
|
||||||
CartAttributeInput,
|
CartAttributeInput,
|
||||||
CartItem,
|
CartItem,
|
||||||
CartProductVariant,
|
|
||||||
Collection,
|
Collection,
|
||||||
Connection,
|
Connection,
|
||||||
Filter,
|
Filter,
|
||||||
@ -54,7 +52,6 @@ import {
|
|||||||
PageInfo,
|
PageInfo,
|
||||||
Product,
|
Product,
|
||||||
ProductVariant,
|
ProductVariant,
|
||||||
ProductVariantOperation,
|
|
||||||
ShopifyAddToCartOperation,
|
ShopifyAddToCartOperation,
|
||||||
ShopifyCart,
|
ShopifyCart,
|
||||||
ShopifyCartOperation,
|
ShopifyCartOperation,
|
||||||
@ -398,45 +395,22 @@ export async function getCart(cartId: string): Promise<Cart | undefined> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const cart = reshapeCart(res.body.data.cart);
|
const cart = reshapeCart(res.body.data.cart);
|
||||||
let extendedCartLines = cart.lines;
|
|
||||||
|
|
||||||
const lineIdMap = {} as { [key: string]: string };
|
// attach core charge as an additional attribute of a cart line, and remove the core charge line from cart
|
||||||
// get product variants details including core charge variant data
|
const extendedCartLines = cart?.lines.reduce((lines, item) => {
|
||||||
const productVariantPromises =
|
const coreVariantId = item.merchandise.coreVariantId?.value;
|
||||||
cart?.lines.map((line) => {
|
if (coreVariantId) {
|
||||||
lineIdMap[line.merchandise.id] = line.id;
|
const relatedCoreCharge = cart.lines.find((line) => line.merchandise.id === coreVariantId);
|
||||||
return getProductVariant(line?.merchandise.id);
|
|
||||||
}) || [];
|
|
||||||
|
|
||||||
if (productVariantPromises.length) {
|
|
||||||
const productVariantsById = (await Promise.allSettled(productVariantPromises))
|
|
||||||
.filter((result) => result.status === 'fulfilled')
|
|
||||||
.reduce(
|
|
||||||
(acc, result) => {
|
|
||||||
const _result = result as PromiseFulfilledResult<CartProductVariant>;
|
|
||||||
return {
|
|
||||||
...acc,
|
|
||||||
[_result.value.id]: { ..._result.value, lineId: lineIdMap[_result.value.id] }
|
|
||||||
};
|
|
||||||
},
|
|
||||||
{} as { [key: string]: CartProductVariant & { lineId?: string } }
|
|
||||||
);
|
|
||||||
|
|
||||||
// add core charge field to cart line item if any
|
|
||||||
extendedCartLines = cart?.lines.reduce((lines, item) => {
|
|
||||||
const productVariant = productVariantsById[item.merchandise.id];
|
|
||||||
if (productVariant && productVariant.coreVariantId) {
|
|
||||||
const coreCharge = productVariantsById[productVariant.coreVariantId];
|
|
||||||
return lines.concat([
|
return lines.concat([
|
||||||
{
|
{
|
||||||
...item,
|
...item,
|
||||||
coreCharge
|
coreCharge: relatedCoreCharge
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
}, [] as CartItem[]);
|
}, [] as CartItem[]);
|
||||||
}
|
|
||||||
|
|
||||||
const totalQuantity = extendedCartLines.reduce((sum, line) => sum + line.quantity, 0);
|
const totalQuantity = extendedCartLines.reduce((sum, line) => sum + line.quantity, 0);
|
||||||
|
|
||||||
@ -636,19 +610,6 @@ export async function getProduct(handle: string): Promise<Product | undefined> {
|
|||||||
return reshapeProduct(res.body.data.product, false);
|
return reshapeProduct(res.body.data.product, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getProductVariant(id: string) {
|
|
||||||
const res = await shopifyFetch<ProductVariantOperation>({
|
|
||||||
query: getProductVariantQuery,
|
|
||||||
tags: [TAGS.products],
|
|
||||||
variables: {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const variant = res.body.data.node;
|
|
||||||
return { ...variant, coreVariantId: variant.coreVariantId?.value || null };
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getProductRecommendations(productId: string): Promise<Product[]> {
|
export async function getProductRecommendations(productId: string): Promise<Product[]> {
|
||||||
const res = await shopifyFetch<ShopifyProductRecommendationsOperation>({
|
const res = await shopifyFetch<ShopifyProductRecommendationsOperation>({
|
||||||
query: getProductRecommendationsQuery,
|
query: getProductRecommendationsQuery,
|
||||||
|
@ -35,21 +35,3 @@ export const getProductRecommendationsQuery = /* GraphQL */ `
|
|||||||
}
|
}
|
||||||
${productFragment}
|
${productFragment}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const getProductVariantQuery = /* GraphQL */ `
|
|
||||||
query getProductVariant($id: ID!) {
|
|
||||||
node(id: $id) {
|
|
||||||
... on ProductVariant {
|
|
||||||
id
|
|
||||||
title
|
|
||||||
selectedOptions {
|
|
||||||
name
|
|
||||||
value
|
|
||||||
}
|
|
||||||
coreVariantId: metafield(namespace: "custom", key: "coreVariant") {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
@ -26,16 +26,9 @@ export type CartItem = {
|
|||||||
value: string;
|
value: string;
|
||||||
}[];
|
}[];
|
||||||
product: Product;
|
product: Product;
|
||||||
|
coreVariantId: { value: string } | null;
|
||||||
};
|
};
|
||||||
coreCharge?: {
|
coreCharge?: CartItem;
|
||||||
id: string;
|
|
||||||
title: string;
|
|
||||||
lineId?: string;
|
|
||||||
selectedOptions: {
|
|
||||||
name: string;
|
|
||||||
value: string;
|
|
||||||
}[];
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Collection = ShopifyCollection & {
|
export type Collection = ShopifyCollection & {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user