|
1 | 1 | import React, { FunctionComponent, useCallback, useState, useRef } from 'react'
|
2 | 2 | import { Root } from './ConfigurableProduct.styled'
|
3 |
| -import Form, { TextSwatchesProps, TextSwatches, Select, Quantity } from '@storystore/ui/dist/components/Form' |
| 3 | +import Form, { TextSwatchesProps, TextSwatches, Select, Quantity, Error } from '@storystore/ui/dist/components/Form' |
4 | 4 | import { useCart } from '~/hooks/useCart/useCart'
|
5 | 5 | import { useStoryStore } from '~/lib/storystore'
|
6 | 6 | import { useRouter } from 'next/router'
|
@@ -48,6 +48,8 @@ export const ConfigurableProduct: FunctionComponent<ConfigurableProductProps> =
|
48 | 48 |
|
49 | 49 | const [variantSku, setVariantSku] = useState(sku)
|
50 | 50 |
|
| 51 | + const [error, setError] = useState<string | null>(null) |
| 52 | + |
51 | 53 | const inStock = stock === 'IN_STOCK'
|
52 | 54 |
|
53 | 55 | const { setGallery, setPrice } = useProductLayout()
|
@@ -111,11 +113,17 @@ export const ConfigurableProduct: FunctionComponent<ConfigurableProductProps> =
|
111 | 113 | async ({ quantity = 1 }) => {
|
112 | 114 | if (!cartId || !inStock || addingConfigurableProductToCart.loading) return
|
113 | 115 |
|
114 |
| - await addConfigurableProductToCart({ sku, variantSku, quantity }) |
| 116 | + try { |
| 117 | + setError(null) |
| 118 | + await addConfigurableProductToCart({ sku, variantSku, quantity }) |
115 | 119 |
|
116 |
| - await history.push('/cart') |
| 120 | + await history.push('/cart') |
117 | 121 |
|
118 |
| - window.scrollTo(0, 0) |
| 122 | + window.scrollTo(0, 0) |
| 123 | + } catch (e) { |
| 124 | + console.log(e) |
| 125 | + setError(e.message) |
| 126 | + } |
119 | 127 | },
|
120 | 128 | [sku, variantSku, addConfigurableProductToCart, inStock, addingConfigurableProductToCart, history, cartId]
|
121 | 129 | )
|
@@ -173,6 +181,8 @@ export const ConfigurableProduct: FunctionComponent<ConfigurableProductProps> =
|
173 | 181 | <Quantity name="quantity" defaultValue={1} minValue={1} addLabel="Add" removeLabel="Remove" rules={{ required: true, min: 1 }} hideError />
|
174 | 182 |
|
175 | 183 | <Button type="submit" as="button" text={inStock ? 'Add to Cart' : 'Sold Out'} disabled={!inStock} loading={addingConfigurableProductToCart.loading} />
|
| 184 | + |
| 185 | + {error && <Error>{error}</Error>} |
176 | 186 | </Root>
|
177 | 187 | </div>
|
178 | 188 | )
|
|
0 commit comments