-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update Name * update getProducts * Align with SDK * Update Product details
- Loading branch information
Showing
12 changed files
with
235 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 45 additions & 46 deletions
91
apps/web/components/ProductProperties/ProductProperties.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,57 @@ | ||
import { SfChip, SfThumbnail } from '@storefront-ui/react'; | ||
import { useTranslation } from 'next-i18next'; | ||
import type { ProductPropertiesProps } from '~/components'; | ||
import { Divider, type ProductPropertiesProps } from '~/components'; | ||
import { useProductAttribute } from '~/hooks'; | ||
|
||
export function ProductProperties({ product, showColors = true, ...attributes }: ProductPropertiesProps): JSX.Element { | ||
const { t } = useTranslation(); | ||
|
||
const { getAttributeList, getAttribute, setAttribute } = useProductAttribute(product, ['Color', 'Size']); | ||
const { getOptions, getAttributeList, getAttribute, setAttribute } = useProductAttribute(product, ['Color', 'Size']); | ||
const options = getOptions(); | ||
|
||
const sizes = getAttributeList('Size'); | ||
const colors = getAttributeList('Color'); | ||
const selectedSize = getAttribute('Size'); | ||
const selectedColor = getAttribute('Color'); | ||
if (options.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="px-4" {...attributes}> | ||
{sizes.length > 1 && ( | ||
<> | ||
<span className="block mb-2 mt-2 text-base font-medium leading-6 text-neutral-900">{t('size')}</span> | ||
{sizes.map(({ label, value }) => ( | ||
<div className="mr-2 mb-2 uppercase inline-block" key={value}> | ||
<SfChip | ||
className="min-w-[48px]" | ||
size="sm" | ||
inputProps={{ | ||
checked: value === selectedSize, | ||
onChange: () => setAttribute('Size', value), | ||
}} | ||
> | ||
{label} | ||
</SfChip> | ||
</div> | ||
))} | ||
</> | ||
)} | ||
{colors.length > 1 && ( | ||
<> | ||
<span className="block mb-2 mt-2 text-base font-medium leading-6 text-neutral-900">{t('color')}</span> | ||
{colors.map(({ value, label }) => ( | ||
<div key={value} className="mr-2 mb-2 inline-block"> | ||
<SfChip | ||
slotPrefix={showColors ? <SfThumbnail size="sm" style={{ background: value }} /> : null} | ||
size="sm" | ||
inputProps={{ | ||
checked: value === selectedColor, | ||
onChange: () => setAttribute('Color', value), | ||
}} | ||
> | ||
{label} | ||
</SfChip> | ||
</div> | ||
))} | ||
</> | ||
)} | ||
</div> | ||
<> | ||
<Divider className="mb-6" /> | ||
<div className="px-4" {...attributes}> | ||
{options.map((option) => { | ||
const optionValues = getAttributeList(option); | ||
const selectedValue = getAttribute(option); | ||
|
||
return ( | ||
optionValues.length > 1 && ( | ||
<div key={option}> | ||
<span className="block mb-2 mt-2 text-base font-medium leading-6 text-neutral-900"> | ||
{t(option.toLowerCase())} | ||
</span> | ||
{optionValues.map(({ label, value }) => ( | ||
<div className="mr-2 mb-2 uppercase inline-block" key={value}> | ||
<SfChip | ||
className="min-w-[48px]" | ||
size="sm" | ||
slotPrefix={ | ||
option === 'Color' && showColors ? ( | ||
<SfThumbnail size="sm" style={{ background: value }} /> | ||
) : null | ||
} | ||
inputProps={{ | ||
checked: value === selectedValue, | ||
onChange: () => setAttribute(option, value), | ||
}} | ||
> | ||
{label} | ||
</SfChip> | ||
</div> | ||
))} | ||
</div> | ||
) | ||
); | ||
})} | ||
</div> | ||
<Divider className="mt-4 mb-2 md:mt-8" /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 43 additions & 43 deletions
86
apps/web/hooks/useProductAttribute/useProductAttribute.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,45 @@ | ||
import { useState } from 'react'; | ||
import { SfAttribute, SfProduct } from '@vue-storefront/unified-data-model'; | ||
import { get, map, defaults as withDefaults, zipObject, groupBy, uniqBy, pick, mapValues } from 'lodash-es'; | ||
|
||
/** | ||
* Hook for getting product attributes data | ||
* @param {SfProduct} product Product object | ||
*/ | ||
export function useProductAttribute<TAttribute extends string>(product: SfProduct, attributesNames: TAttribute[] = []) { | ||
// const attributes = groupBy( | ||
// uniqBy( | ||
// (product?.variants || []).flatMap((variant) => variant?.attributes), | ||
// 'value', | ||
// ), | ||
// 'name', | ||
// ); | ||
// const mapAttribute = (attributes: SfAttribute[] = []) => { | ||
// const mappedAttributes = mapValues( | ||
// pick(groupBy(attributes, 'name'), attributesNames), | ||
// (attribute) => attribute[0].value, | ||
// ); | ||
|
||
// const defaults = zipObject( | ||
// attributesNames, | ||
// map(attributesNames, () => null), | ||
// ); | ||
// return withDefaults(mappedAttributes, defaults); | ||
// }; | ||
const defaultAttributes = Object.entries(product.attributes) | ||
.map(([name, values]) => ({ [name]: values[0].value })) | ||
.reduce((obj, attr) => ({ ...obj, ...attr }), {}); | ||
|
||
const [selectedAttrs, setSelectedAttrs] = useState(defaultAttributes); | ||
|
||
return { | ||
getAttributeList: (attributeName: TAttribute) => get(product.attributes, attributeName, [] as SfAttribute[]), | ||
getAttribute: (attributeName: TAttribute) => get(selectedAttrs, attributeName, null), | ||
setAttribute: (attributeName: TAttribute, attributeValue: string) => { | ||
setSelectedAttrs((previous) => ({ | ||
...previous, | ||
[attributeName]: attributeValue, | ||
})); | ||
}, | ||
}; | ||
import { Product } from '~/sdk/shopify/types'; | ||
|
||
interface AttributeValue { | ||
label: string; | ||
value: string; | ||
} | ||
|
||
interface UseProductAttribute { | ||
getAttributeList: (attributeName: string) => AttributeValue[]; | ||
getAttribute: (attributeName: string) => string; | ||
setAttribute: (attributeName: string, value: string) => void; | ||
getOptions: () => string[]; | ||
} | ||
|
||
export const useProductAttribute = (product: Product, attributes: string[]): UseProductAttribute => { | ||
const initialAttributes = attributes.reduce<Record<string, string>>((accumulator, attribute) => { | ||
const option = product.options.find((opt) => opt.name === attribute); | ||
if (option && option.values.length > 0) { | ||
accumulator[attribute] = option.values[0]; | ||
} | ||
return accumulator; | ||
}, {}); | ||
|
||
const [selectedAttributes, setSelectedAttributes] = useState<Record<string, string>>(initialAttributes); | ||
|
||
const getAttributeList = (attributeName: string): AttributeValue[] => { | ||
const attribute = product.options.find((option) => option.name === attributeName); | ||
return attribute ? attribute.values.map((value) => ({ label: value, value })) : []; | ||
}; | ||
|
||
const getAttribute = (attributeName: string): string => { | ||
return selectedAttributes[attributeName] || ''; | ||
}; | ||
|
||
const setAttribute = (attributeName: string, value: string): void => { | ||
setSelectedAttributes((previous) => ({ ...previous, [attributeName]: value })); | ||
}; | ||
|
||
const getOptions = (): string[] => { | ||
return product.options.map((option) => option.name); | ||
}; | ||
|
||
return { getAttributeList, getAttribute, setAttribute, getOptions }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "web", | ||
"name": "shopify-storefront", | ||
"version": "0.1.0", | ||
"private": true, | ||
"main": "", | ||
|
Oops, something went wrong.