forked from adobe/storefront-product-listing-page
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProductItem.tsx
156 lines (143 loc) · 5.46 KB
/
ProductItem.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2024 Adobe
All Rights Reserved.
NOTICE: Adobe permits you to use, modify, and distribute this file in
accordance with the terms of the Adobe license agreement accompanying
it.
*/
import { FunctionComponent } from 'preact';
import { useState } from 'preact/hooks';
import NoImage from '../../icons/NoImage.svg';
import {
Product,
ProductViewMedia,
RedirectRouteFunc,
RefinedProduct,
} from '../../types/interface';
import { SEARCH_UNIT_ID } from '../../utils/constants';
import { getProductImageURL } from '../../utils/getProductImage';
import { htmlStringDecode } from '../../utils/htmlStringDecode';
import { SwatchButtonGroup } from '../SwatchButtonGroup';
import ProductPrice from './ProductPrice';
export interface ProductProps {
item: Product;
currencySymbol: string;
currencyRate?: string;
setRoute?: RedirectRouteFunc | undefined;
refineProduct: (optionIds: string[], sku: string) => any;
}
export const ProductItem: FunctionComponent<ProductProps> = ({
item,
currencySymbol,
currencyRate,
setRoute,
refineProduct,
}: ProductProps) => {
const { product, productView } = item;
const [selectedSwatch, setSelectedSwatch] = useState('');
const [productImages, setImages] = useState<ProductViewMedia[] | null>();
const [refinedProduct, setRefinedProduct] = useState<RefinedProduct>();
const handleSelection = async (optionIds: string[], sku: string) => {
const data = await refineProduct(optionIds, sku);
setSelectedSwatch(optionIds[0]);
setImages(data.refineProduct.images);
setRefinedProduct(data);
};
const isSelected = (id: string) => {
const selected = selectedSwatch ? selectedSwatch === id : false;
return selected;
};
const productImage = getProductImageURL(
productImages ? productImages ?? [] : productView.images ?? []
); // get image for PLP
// will have to figure out discount logic for amount_off and percent_off still
const discount: boolean = refinedProduct
? refinedProduct.refineProduct?.priceRange?.minimum?.regular?.amount
?.value >
refinedProduct.refineProduct?.priceRange?.minimum?.final?.amount?.value
: productView?.priceRange?.minimum?.regular?.amount?.value >
productView?.priceRange?.minimum?.final?.amount?.value ||
productView?.price?.regular?.amount?.value >
productView?.price?.final?.amount?.value;
const isComplexProductView = productView?.__typename === 'ComplexProductView';
const isBundle = product?.__typename === 'BundleProduct';
const isGrouped = product?.__typename === 'GroupedProduct';
const isGiftCard = product?.__typename === 'GiftCardProduct';
const isConfigurable = product?.__typename === 'ConfigurableProduct';
const onProductClick = () => {
window.magentoStorefrontEvents?.publish.searchProductClick(
SEARCH_UNIT_ID,
productView?.sku
);
};
const productUrl = setRoute
? setRoute({ sku: productView?.sku })
: product?.canonical_url;
return (
<div className="ds-sdk-product-item group relative flex flex-col max-w-sm justify-between h-full">
<a
href={productUrl as string}
onClick={onProductClick}
className="!text-primary hover:no-underline hover:text-primary"
>
<div className="ds-sdk-product-item__main relative flex flex-col justify-between h-full">
<div className="ds-sdk-product-item__image relative w-full h-full rounded-md overflow-hidden">
{/*
NOTE:
we could use <picture> <source...
or srcset in <img /> for breakpoint based img file
in future for better performance
*/}
{productImage ? (
<div class="aspect-w-1 aspect-h-1 w-full overflow-hidden rounded-md bg-gray-200 lg:aspect-none">
<img
src={productImage}
alt={productView.name}
loading="eager"
className="max-h-[45rem] h-full w-full object-cover object-center lg:h-full lg:w-full"
/>
</div>
) : (
<NoImage
className={`max-h-[45rem] w-full object-cover object-center lg:w-full`}
/>
)}
</div>
<div className="flex flex-col">
<div className="ds-sdk-product-item__product-name mt-md text-sm text-primary">
{htmlStringDecode(productView.name)}
</div>
<ProductPrice
item={refinedProduct ?? item}
isBundle={isBundle}
isGrouped={isGrouped}
isGiftCard={isGiftCard}
isConfigurable={isConfigurable}
isComplexProductView={isComplexProductView}
discount={discount}
currencySymbol={currencySymbol}
currencyRate={currencyRate}
/>
</div>
</div>
</a>
<div className="ds-sdk-product-item__product-swatch flex flex-row mt-sm text-sm text-primary pb-6">
{productView?.options?.map(
(swatches) =>
swatches.id == 'color' && (
<SwatchButtonGroup
key={productView?.sku}
isSelected={isSelected}
swatches={swatches.values ?? []}
showMore={onProductClick}
productUrl={productUrl as string}
onClick={handleSelection}
sku={productView?.sku}
/>
)
)}
</div>
</div>
);
};
export default ProductItem;