From 8f82f6299eb1cb7a832ea18f5aed8300c141bd5a Mon Sep 17 00:00:00 2001 From: Chloe Date: Wed, 12 Jun 2024 19:14:57 +0700 Subject: [PATCH] feat: add more details to product tile Signed-off-by: Chloe --- app/product/[handle]/page.tsx | 6 +- components/grid/three-items.tsx | 6 +- components/grid/tile.tsx | 85 ++++++++++++++-------- components/layout/products-list/index.tsx | 6 +- lib/shopify/fragments/product.ts | 6 ++ lib/shopify/index.ts | 3 +- lib/shopify/types.ts | 11 ++- lib/utils.ts | 2 +- public/icons/cylinder.png | Bin 0 -> 509 bytes public/icons/fuel.png | Bin 0 -> 584 bytes 10 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 public/icons/cylinder.png create mode 100644 public/icons/fuel.png diff --git a/app/product/[handle]/page.tsx b/app/product/[handle]/page.tsx index 94a8d74f0..8a9ef69b5 100644 --- a/app/product/[handle]/page.tsx +++ b/app/product/[handle]/page.tsx @@ -132,11 +132,7 @@ async function RelatedProducts({ id }: { id: string }) { > diff --git a/components/grid/tile.tsx b/components/grid/tile.tsx index 08d5ff34e..a8abf656f 100644 --- a/components/grid/tile.tsx +++ b/components/grid/tile.tsx @@ -1,25 +1,23 @@ import { ArrowRightIcon, PhotoIcon } from '@heroicons/react/24/solid'; import clsx from 'clsx'; import Price from 'components/price'; +import { Product } from 'lib/shopify/types'; import Image from 'next/image'; import Link from 'next/link'; export function GridTileImage({ active, - label, + product, href, - place = 'grid', ...props }: { active?: boolean; - label?: { - title: string; - amount: string; - currencyCode: string; - }; - place?: 'grid' | 'gallery'; + product: Product; href: string; } & React.ComponentProps) { + const metafieldKeys = ['engineCylinders', 'fuelType'] as Partial[]; + const shouldShowDescription = metafieldKeys.some((key) => product[key]); + return (
@@ -43,30 +41,57 @@ export function GridTileImage({ )}
-
- {label && ( -

{label.title}

- )} - {label && ( -
- -
- )} +

+ {product.title} +

+
+
+ {shouldShowDescription && ( +
+ {product.engineCylinders?.length ? ( +
+ Cylinder icon + {`${product.engineCylinders[0]} Cylinder`} +
+ ) : null} + {product.fuelType ? ( +
+ Fuel icon + {product.fuelType} +
+ ) : null} +
+ )} +
+
- {place === 'grid' && ( - - More details - - - )} + + + More details + + ); } diff --git a/components/layout/products-list/index.tsx b/components/layout/products-list/index.tsx index b17d2ce13..1eb6fae6a 100644 --- a/components/layout/products-list/index.tsx +++ b/components/layout/products-list/index.tsx @@ -70,11 +70,7 @@ const ProductsList = ({ > (product.engineCylinders), + fuelType: product.fuelType?.value || null, images: reshapeImages(images, product.title), variants: reshapeVariants(removeEdgesAndNodes(variants)) }; @@ -305,7 +307,6 @@ const reshapeProducts = (products: ShopifyProduct[]) => { for (const product of products) { if (product) { const reshapedProduct = reshapeProduct(product); - if (reshapedProduct) { reshapedProducts.push(reshapedProduct); } diff --git a/lib/shopify/types.ts b/lib/shopify/types.ts index d64376ba9..7443823c4 100644 --- a/lib/shopify/types.ts +++ b/lib/shopify/types.ts @@ -100,9 +100,14 @@ export type Metaobject = { [key: string]: string; }; -export type Product = Omit & { +export type Product = Omit< + ShopifyProduct, + 'variants' | 'images' | 'fuelType' | 'engineCylinders' +> & { variants: ProductVariant[]; images: Image[]; + fuelType: string | null; + engineCylinders: number[] | null; }; export type ProductOption = { @@ -128,6 +133,8 @@ export type ProductVariant = { mileage: number | null; estimatedDelivery: string | null; condition: string | null; + engineCylinders: string | null; + fuelType: string | null; }; export type ShopifyCartProductVariant = { @@ -205,6 +212,8 @@ export type ShopifyProduct = { handle: string; }[]; }; + engineCylinders: { value: string } | null; + fuelType: { value: string } | null; }; export type ShopifyCartOperation = { diff --git a/lib/utils.ts b/lib/utils.ts index 8d800e3f6..de885097e 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -51,7 +51,7 @@ export function normalizeUrl(domain: string, url: string) { export const parseMetaFieldValue = (field: { value: string } | null): T | null => { try { - return JSON.parse(field?.value || '{}'); + return field?.value ? JSON.parse(field.value) : null; } catch (error) { return null; } diff --git a/public/icons/cylinder.png b/public/icons/cylinder.png new file mode 100644 index 0000000000000000000000000000000000000000..0e7b1a279af5f54229103962749064e33afa88a4 GIT binary patch literal 509 zcmVr}qd+ zir#3D6tX-DjL|K6i4aKeHZ9U#dPUD@g-3xB?La_k=$;0*X~*`6W?bhLV1BXP2#CAm z7FX~&-GCXBu%N+R2uMmhe)0&er5WvBW?Tx=v8CF<#WKM{1DqNA2KUnkJz0FE?>@mc z%lKmy^BTBBduTfyhZ)DN2FuE5L`W?93A2o3SxxD-9M5RcNWnsuzUX#AyS;_^=MxU; zIwDX>o8Y`j_XM4 literal 0 HcmV?d00001 diff --git a/public/icons/fuel.png b/public/icons/fuel.png new file mode 100644 index 0000000000000000000000000000000000000000..14c9e56188cd646c618a6907829e0918c84788d1 GIT binary patch literal 584 zcmV-O0=NB%P)gBB113!p2R!8MZ`!3kfe6ldLmX_j~RUIU02@}-u&-2k~XOExuX zDyzUC?~gz~D2X>3uo+}C-h*cLXd6s|cZUvz!5ABlu&$c)f*KGDh_lKWSHPwr$D%>0 ztxk=;n4_vw#_5Kaql~8cS1M%uOfX$%IixNogH@WS;Xi=~2M(CKBrY-;+=4dID=B8k zvA__R7Z(Lwo^6r1f{an|p>&UVt>9Ah)QpP{c`@h%YoI{%*`!v^jCfQsgAf>Hxf7I$ zp8For`{2(8VbQyC9xSr)Zay246d2SqF4*dJ87m|O1{sV&TU`$0o20;CnQ_2YH_Uh_ z9u!FOTpf1%qQ8YYT`iW?UYHOM^0G`jz@F$86&AhA8^q&p(msOt4h<&cSfWoxb;4$W zQ-^l)8P#{Wv9}lhqp6c|Resj?nfyK_-J~{O5y%4}EaNZ$0000