Skip to content

Commit 3231706

Browse files
committed
'mo swatches
1 parent b809118 commit 3231706

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

components/Product/Product.styled.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export const Info = styled.div`
129129
grid-auto-rows: max-content;
130130
grid-gap: 3rem;
131131
padding: 2rem ${props => props.theme.layout.margin};
132+
width: 100%;
132133
`
133134

134135
export const Header = styled.header`

components/Product/Product.tsx

+23-17
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ import { ProductImageSkeleton } from './ProductImage.skeleton'
3434
import Price from '@storystore/ui/dist/components/Price'
3535
import Button from '@storystore/ui/dist/components/Button'
3636
import Breadcrumbs from '@storystore/ui/dist/components/Breadcrumbs'
37-
import Form, { Input } from '@storystore/ui/dist/components/Form'
37+
import Form, { Input, Select } from '@storystore/ui/dist/components/Form'
3838
import { useStoryStore } from '~/hooks/useStoryStore/useStoryStore'
3939
import TextSwatches from '@storystore/ui/dist/components/Form/TextSwatches'
4040
import ThumbSwatches from '@storystore/ui/dist/components/Form/ThumbSwatches'
41+
import ColorSwatches from '@storystore/ui/dist/components/Form/ColorSwatches'
4142
import PageBuilder from '~/components/PageBuilder'
4243

4344
const ProductCarousel = dynamic(() => import('@storystore/ui/dist/components/ProductCarousel'), { ssr: false })
@@ -253,20 +254,23 @@ export const Product: FunctionComponent<ProductProps> = ({ urlKey }) => {
253254
label: selected ? `${label}: ${selected.label}` : label,
254255
name: `options.${code}`,
255256
rules: { required },
256-
items: items?.map(({ id, label, value, image }: any) => ({
257-
_id: id,
258-
label,
259-
type: 'radio',
260-
value,
261-
image: image && {
262-
alt: image.label || '',
263-
src: resolveImage(image.url, {
264-
width: 200,
265-
}),
266-
width: 160,
267-
height: 198,
268-
},
269-
})),
257+
items: items?.map(({ id, label, value, swatch }: any) => {
258+
return {
259+
_id: id,
260+
label,
261+
type: 'radio',
262+
value,
263+
color: swatch?.color,
264+
image: swatch?.image && {
265+
alt: label || '',
266+
src: resolveImage(swatch.image, {
267+
width: 200,
268+
}),
269+
width: 160,
270+
height: 198,
271+
},
272+
}
273+
}),
270274
},
271275
}
272276
})
@@ -275,8 +279,10 @@ export const Product: FunctionComponent<ProductProps> = ({ urlKey }) => {
275279
return (
276280
<fieldset key={_id || index}>
277281
<Field>
278-
{type === 'text' && <TextSwatches {...swatches} />}
279-
{type === 'thumb' && <ThumbSwatches {...swatches} />}
282+
{type === 'TextSwatchData' && <TextSwatches {...swatches} />}
283+
{type === 'ImageSwatchData' && <ThumbSwatches {...swatches} />}
284+
{type === 'ColorSwatchData' && <ColorSwatches {...swatches} />}
285+
{type === undefined && <Select {...swatches} />}
280286
</Field>
281287
</fieldset>
282288
)

components/Product/graphql/product.graphql

+11-1
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,19 @@ query ProductQuery($urlKey: String!) {
5252
label
5353
value: value_index
5454
swatch: swatch_data {
55+
__typename
5556
value
57+
58+
... on TextSwatchData {
59+
text: value
60+
}
61+
62+
... on ColorSwatchData {
63+
color: value
64+
}
65+
5666
... on ImageSwatchData {
57-
thumbnail
67+
image: thumbnail
5868
}
5969
}
6070
}

components/Product/useProduct.ts

+5-17
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,18 @@ export const useProduct = ({ urlKey }: UseProduct) => {
6868
)
6969

7070
const options = _product.options
71-
?.sort((a: any, b: any) => b.position - a.position)
71+
?.sort((a: any, b: any) => a.position - b.position)
7272
.map((option: any) => {
73-
const { id, label, code, items } = option
74-
const thumbKey = /color/.test(code) ? code : undefined
75-
const type = thumbKey ? 'thumb' : 'text'
73+
const { id, label, code, items = [] } = option
74+
75+
const type = items[0]?.swatch?.__typename
7676

7777
return {
7878
id,
7979
type,
8080
label,
8181
code,
82-
items: items.map((item: any) => {
83-
const disabled = item.stock !== 'IN_STOCK'
84-
85-
const { id, value, label } = item
86-
87-
return {
88-
id,
89-
value,
90-
label,
91-
disabled,
92-
image: thumbKey && variants.find((x: any) => x[thumbKey] === value).product.thumbnail,
93-
}
94-
}),
82+
items,
9583
}
9684
})
9785

0 commit comments

Comments
 (0)