Move to components, and updates

This commit is contained in:
cond0r 2022-12-02 15:01:05 +02:00
parent c46ac015ef
commit 0968c0268a
8 changed files with 74 additions and 26 deletions

View File

@ -130,7 +130,7 @@ export interface ProductMetafield {
* {
* // Namespace, the container for a set of metadata
* reviews: {
* // Key of the metafield, used to differentiate between metafields of the same namespace
* // Key of the metafield
* rating: {
* key: 'rating',
* value: 5,
@ -216,7 +216,7 @@ export interface Product {
* {
* // Namespace, the container for a set of metadata
* reviews: {
* // Key of the metafield, used to differentiate between metafields of the same namespace
* // Key of the metafield
* rating: {
* key: 'rating',
* value: 4,

View File

@ -156,7 +156,7 @@ export function normalizeMetafields(
type,
namespace,
value,
html: getMetafieldValue(type, value, locale),
valueHtml: getMetafieldValue(type, value, locale),
}
if (!output[namespace]) {

View File

@ -0,0 +1,23 @@
import { ProductCustomField } from '@commerce/types/product'
interface Props {
customFields: ProductCustomField[]
}
const ProductCustomFields: React.FC<Props> = ({ customFields }) => {
return (
<>
{customFields.map((field) => (
<div
key={field.id}
className="flex gap-2 border-b py-3 border-accent-2 border-dashed last:border-b-0"
>
<strong className="leading-7">{field.name}:</strong>
<span>{field.value}</span>
</div>
))}
</>
)
}
export default ProductCustomFields

View File

@ -0,0 +1 @@
export { default as ProductCustomFields } from './ProductCustomFields'

View File

@ -0,0 +1,29 @@
import type { FC } from 'react'
import type { ProductMetafields as IProductMetafields } from '@commerce/types/product'
import Text from '@components/ui/Text'
interface Props {
metafields: IProductMetafields
/**
* The namespace of the metafields to display.
*/
namespace: string
}
const ProductMetafields: FC<Props> = ({ metafields, namespace }) => {
return (
<>
{Object.values(metafields[namespace] ?? {}).map((field) => (
<div
key={field.key}
className="flex gap-2 border-b py-3 border-accent-2 border-dashed last:border-b-0"
>
<strong className="leading-7">{field.name}:</strong>
<Text html={field.valueHtml || field.value} className="!mx-0" />
</div>
))}
</>
)
}
export default ProductMetafields

View File

@ -0,0 +1 @@
export { default as ProductMetafields } from './ProductMetafields'

View File

@ -10,6 +10,8 @@ import {
SelectedOptions,
} from '../helpers'
import ErrorMessage from '@components/ui/ErrorMessage'
import { ProductCustomFields } from '../ProductCustomFields'
import { ProductMetafields } from '../ProductMetafields'
interface ProductSidebarProps {
product: Product
@ -94,32 +96,25 @@ const ProductSidebar: FC<ProductSidebarProps> = ({ product, className }) => {
This is a limited edition production run. Printing starts when the
drop ends.
</Collapse>
<Collapse title="Details">
This is a limited edition production run. Printing starts when the
drop ends. Reminder: Bad Boys For Life. Shipping may take 10+ days due
to COVID-19.
</Collapse>
{(product.customFields || product.metafields) && (
<Collapse title="Specifications">
{product.customFields?.map((field) => (
<div
key={field.id}
className="flex gap-2 border-b py-3 border-accent-2 border-dashed last:border-b-0"
>
<strong className="leading-7">{field.name}:</strong>
<span>{field.value}</span>
</div>
))}
{Object.values(product.metafields?.my_fields ?? {}).map((field) => (
<div
key={field.key}
className="flex gap-2 border-b py-3 border-accent-2 border-dashed last:border-b-0"
>
<strong className="leading-7">{field.name}:</strong>
<Text html={field.valueHtml || field.value} className="!mx-0" />
</div>
))}
{product.customFields && product.customFields?.length > 0 && (
<Collapse title="Specifications">
<ProductCustomFields customFields={product.customFields} />
</Collapse>
)}
{product.metafields?.my_fields && (
<Collapse title="Specifications">
<ProductMetafields
metafields={product.metafields}
namespace="my_fields"
/>
</Collapse>
)}
</div>

View File

@ -13,11 +13,10 @@ import { ProductView } from '@components/product'
// Used by the Shopify Example
const withMetafields = [
{ namespace: 'reviews', key: 'rating' },
{ namespace: 'descriptors', key: 'care_guide' },
{ namespace: 'my_fields', key: 'weight' },
{ namespace: 'reviews', key: 'count' },
{ namespace: 'my_fields', key: 'width' },
{ namespace: 'my_fields', key: 'weight' },
{ namespace: 'my_fields', key: 'length' },
{ namespace: 'my_fields', key: 'manufacturer_url' },
]
export async function getStaticProps({