From d3a7919ea0a2389d3773be93616c3a50c8150e64 Mon Sep 17 00:00:00 2001 From: Eli Kinsey Date: Thu, 3 Oct 2024 13:57:58 -0700 Subject: [PATCH] add brilliant quantity to all variants --- src/templates/merch/Product.tsx | 11 +++++++++-- src/templates/merch/ProductPanel.tsx | 16 ++++++++++++++-- src/templates/merch/hooks.ts | 25 ++++++++++++++++++++++--- src/templates/merch/types.ts | 1 + 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/templates/merch/Product.tsx b/src/templates/merch/Product.tsx index e322fff0f18b..bd397b5eecdf 100644 --- a/src/templates/merch/Product.tsx +++ b/src/templates/merch/Product.tsx @@ -15,6 +15,7 @@ import { useCartStore } from './store' import { ShopifyProduct } from './types' import { getProductMetafield } from './utils' import SEO from 'components/seo' +import { IconSpinner } from '@posthog/icons' type ProductPageProps = { className?: string @@ -120,14 +121,20 @@ export default function Product(props: ProductPageProps): React.ReactElement { <> - {outOfStock ? 'Out of Stock' : 'Add to Cart'} + {loading ? ( + + ) : outOfStock ? ( + 'Out of Stock' + ) : ( + 'Add to Cart' + )} - + <> - {outOfStock ? 'Out of Stock' : 'Add to Cart'} + {loading ? ( + + ) : outOfStock ? ( + 'Out of Stock' + ) : ( + 'Add to Cart' + )} e.node) } + +async function assignBrilliantQuantities(variants: StorefrontProductVariantNode[]): Promise { + await Promise.all( + variants.map((v) => { + return fetch( + `${process.env.GATSBY_SQUEAK_API_HOST}/api/brilliant/inventory/${ + v.id.split('gid://shopify/ProductVariant/')[1] + }` + ) + .then((res) => res.json()) + .then((data) => { + v.brilliantQuantity = data?.quantity || 0 + }) + .catch((err) => { + console.error('Error fetching quantity:', err) + v.brilliantQuantity = 0 + }) + }) + ) +} diff --git a/src/templates/merch/types.ts b/src/templates/merch/types.ts index 4c425543cf93..c8635c4acf4f 100644 --- a/src/templates/merch/types.ts +++ b/src/templates/merch/types.ts @@ -151,6 +151,7 @@ export interface StorefrontProductVariantNode { } quantityAvailable: number selectedOptions: VariantSelectedOption[] + brilliantQuantity: number } export type StorefrontProductVariant = StorefrontProductVariantNode