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

Handle out of stock #149

Merged
merged 6 commits into from
Oct 8, 2018
Merged
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
20 changes: 15 additions & 5 deletions src/components/ProductPreview/AddToCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,21 @@ export default class AddToCart extends Component {

render() {
const { variants } = this.props;
const id = this.props.productId.substring(58, 64)
const id = this.props.productId.substring(58, 64);
const hasVariants = variants.length > 1;

/*
* For products without variants, we disable the whole Add to Cart button
* and change the text. This flag prevents us from duplicating the logic in
* multiple places.
*/
const isOutOfStock = !hasVariants && !variants[0].availableForSale;

return (
<StoreContext.Consumer>
{({ addVariantToCart }) => (
<Form onSubmit={this.handleSubmit(addVariantToCart)}>
{variants.length > 1 && (
{hasVariants && (
<>
<HiddenLabel htmlFor={`variant_${id}`}>Choose a size:</HiddenLabel>
<Size
Expand All @@ -120,15 +128,15 @@ export default class AddToCart extends Component {
Choose Size
</option>
{variants.map(variant => (
<option value={variant.shopifyId} key={variant.shopifyId}>
<option disabled={!variant.availableForSale} value={variant.shopifyId} key={variant.shopifyId}>
{variant.title}
</option>
))}
</Size>
<HiddenLabel htmlFor="quantity">Quantity:</HiddenLabel>
</>
)}
{variants.length <= 1 && (
{!hasVariants && (
<VisibleLabel htmlFor={`quantity_${id}`}>Quantity:</VisibleLabel>
)}
<Quantity
Expand All @@ -140,7 +148,9 @@ export default class AddToCart extends Component {
onChange={this.handleChange}
value={this.state.quantity}
/>
<Button type="submit">Add to Cart</Button>
<Button type="submit" disabled={isOutOfStock}>
{isOutOfStock ? 'Out of Stock' : 'Add to Cart'}
</Button>
</Form>
)}
</StoreContext.Consumer>
Expand Down
1 change: 1 addition & 0 deletions src/components/Store/ProductListings.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default () => (
shopifyId
title
price
availableForSale
}
images {
id
Expand Down