Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Brilliant quantity to all variants #9542

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/templates/merch/Product.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,14 +121,20 @@ export default function Product(props: ProductPageProps): React.ReactElement {
<Quantity value={quantity} onChange={setQuantity} />

<CallToAction
disabled={outOfStock}
disabled={loading || outOfStock}
onClick={handleAddToCart}
type="primary"
className="relative w-full"
>
<>
<span className={cn('', isAdding && 'invisible')}>
{outOfStock ? 'Out of Stock' : 'Add to Cart'}
{loading ? (
<IconSpinner className="w-5 mx-auto animate-spin" />
) : outOfStock ? (
'Out of Stock'
) : (
'Add to Cart'
)}
</span>
<LoaderIcon
className={cn(
Expand Down
16 changes: 14 additions & 2 deletions src/templates/merch/ProductPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ShopifyProduct } from './types'
import { getProductMetafield } from './utils'
import { GatsbyImage, getImage } from 'gatsby-plugin-image'
import { getShopifyImage } from 'gatsby-source-shopify'
import { IconSpinner } from '@posthog/icons'

type ProductPanelProps = {
className?: string
Expand Down Expand Up @@ -104,10 +105,21 @@ export function ProductPanel(props: ProductPanelProps): React.ReactElement {

<Quantity value={quantity} onChange={setQuantity} />

<CallToAction disabled={outOfStock} onClick={handleAddToCart} type="primary" className="relative w-full">
<CallToAction
disabled={loading || outOfStock}
onClick={handleAddToCart}
type="primary"
className="relative w-full"
>
<>
<span className={cn('', isAdding && 'invisible')}>
{outOfStock ? 'Out of Stock' : 'Add to Cart'}
{loading ? (
<IconSpinner className="w-5 mx-auto animate-spin" />
) : outOfStock ? (
'Out of Stock'
) : (
'Add to Cart'
)}
</span>
<LoaderIcon
className={cn(
Expand Down
25 changes: 22 additions & 3 deletions src/templates/merch/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ export function useProduct(id: string): {
})
}

const outOfStock = selectedVariant
? selectedVariant?.quantityAvailable <= 0 && !selectedVariant?.currentlyNotInStock
: false
const outOfStock = (selectedVariant?.brilliantQuantity || 0) <= 0

return { selectedOptions, setOptionAtIndex, selectedVariant, loading, outOfStock }
}
Expand Down Expand Up @@ -166,6 +164,7 @@ function useFetchProductOptions(id: string): { product: StorefrontProduct | null

const json = (await response.json()) as StorefrontShopRequestBody
const responseData = getProduct(json.data)
await assignBrilliantQuantities(responseData.variants)
setProductData(responseData)
setLoading(false)
} catch (err) {
Expand All @@ -189,3 +188,23 @@ function getProduct(responseData: StorefrontShopResponse): StorefrontProduct {
function getVariants(variants: StorefrontProductVariantsEdges): StorefrontProductVariantNode[] {
return variants.edges.map((e: StorefrontProductVariantEdge) => e.node)
}

async function assignBrilliantQuantities(variants: StorefrontProductVariantNode[]): Promise<void> {
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
})
})
)
}
1 change: 1 addition & 0 deletions src/templates/merch/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export interface StorefrontProductVariantNode {
}
quantityAvailable: number
selectedOptions: VariantSelectedOption[]
brilliantQuantity: number
}

export type StorefrontProductVariant = StorefrontProductVariantNode
Expand Down
Loading