Fix sold out item

This commit is contained in:
cond0r 2021-04-14 10:20:28 +03:00
parent d838f34c73
commit cb79d77c88
8 changed files with 39 additions and 19 deletions

View File

@ -146,8 +146,12 @@ const ProductView: FC<Props> = ({ product }) => {
className={s.button} className={s.button}
onClick={addToCart} onClick={addToCart}
loading={loading} loading={loading}
disabled={variant?.availableForSale === false}
> >
Add to Cart {variant?.isInStock === false &&
variant?.availableForSale === false
? 'Sold Out'
: 'Add To Cart'}
</Button> </Button>
</div> </div>
</div> </div>

View File

@ -190,6 +190,10 @@ interface ProductImage {
interface ProductVariant2 { interface ProductVariant2 {
id: string | number id: string | number
options: ProductOption[] options: ProductOption[]
// Indicates whether this product variant is in stock.
isInStock?: boolean
// Indicates if the product variant is available for sale.
availableForSale?: boolean
} }
interface ProductPrice { interface ProductPrice {

View File

@ -10,11 +10,14 @@ export const handler: SWRHook<Customer | null> = {
query: getCustomerQuery, query: getCustomerQuery,
}, },
async fetcher({ options, fetch }) { async fetcher({ options, fetch }) {
const customerAccessToken = getCustomerToken()
if (customerAccessToken) {
const data = await fetch<any | null>({ const data = await fetch<any | null>({
...options, ...options,
variables: { customerAccessToken: getCustomerToken() }, variables: { customerAccessToken },
}) })
return data.customer ?? null return data.customer
}
}, },
useHook: ({ useData }) => (input) => { useHook: ({ useData }) => (input) => {
return useData({ return useData({

View File

@ -27,22 +27,15 @@ export type CheckoutPayload =
| CheckoutQuery | CheckoutQuery
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => { const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
if (!checkoutPayload) {
throw new CommerceError({
message: 'Missing checkout payload from response',
})
}
const checkout = checkoutPayload?.checkout
throwUserErrors(checkoutPayload?.checkoutUserErrors) throwUserErrors(checkoutPayload?.checkoutUserErrors)
if (!checkout) { if (!checkoutPayload?.checkout) {
throw new CommerceError({ throw new CommerceError({
message: 'Missing checkout object from response', message: 'Missing checkout object from response',
}) })
} }
return normalizeCart(checkout) return normalizeCart(checkoutPayload?.checkout)
} }
export default checkoutToCart export default checkoutToCart

View File

@ -53,7 +53,17 @@ const normalizeProductImages = ({ edges }: ImageConnection) =>
const normalizeProductVariants = ({ edges }: ProductVariantConnection) => { const normalizeProductVariants = ({ edges }: ProductVariantConnection) => {
return edges?.map( return edges?.map(
({ ({
node: { id, selectedOptions, sku, title, priceV2, compareAtPriceV2 }, node: {
id,
selectedOptions,
sku,
title,
priceV2,
compareAtPriceV2,
requiresShipping,
availableForSale,
quantityAvailable,
},
}) => { }) => {
return { return {
id, id,
@ -61,7 +71,9 @@ const normalizeProductVariants = ({ edges }: ProductVariantConnection) => {
sku: sku ?? id, sku: sku ?? id,
price: +priceV2.amount, price: +priceV2.amount,
listPrice: +compareAtPriceV2?.amount, listPrice: +compareAtPriceV2?.amount,
requiresShipping: true, requiresShipping,
availableForSale,
isInStock: Number(quantityAvailable) > 0,
options: selectedOptions.map(({ name, value }: SelectedOption) => { options: selectedOptions.map(({ name, value }: SelectedOption) => {
const options = normalizeProductOption({ const options = normalizeProductOption({
id, id,

View File

@ -7,6 +7,7 @@ edges {
node { node {
id id
title title
availableForSale
vendor vendor
handle handle
description description

View File

@ -33,6 +33,9 @@ const getProductQuery = /* GraphQL */ `
id id
title title
sku sku
availableForSale
quantityAvailable
requiresShipping
selectedOptions { selectedOptions {
name name
value value