-
Notifications
You must be signed in to change notification settings - Fork 51
/
ProductImage.js
193 lines (176 loc) · 4.95 KB
/
ProductImage.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import React, { useContext, useState } from 'react'
import { pathOr, compose, path } from 'ramda'
import PropTypes from 'prop-types'
import { CollectionBadges, DiscountBadge } from 'vtex.store-components'
import classNames from 'classnames'
import ProductSummaryContext from '../ProductSummaryContext'
import { productShape } from '../../utils/propTypes'
import productSummary from '../../productSummary.css'
const maybeBadge = ({ listPrice, price, label }) => shouldShow => component => {
if (shouldShow) {
return (
<DiscountBadge listPrice={listPrice} sellingPrice={price} label={label}>
{component}
</DiscountBadge>
)
}
return component
}
const maybeCollection = ({ productClusters }) => shouldShow => component => {
if (shouldShow && productClusters && productClusters.length > 0) {
const collections = productClusters.map(cl => cl.name)
return (
<CollectionBadges collectionBadgesText={collections}>
{component}
</CollectionBadges>
)
}
return component
}
export const ImagePlaceholder = () => (
<div className="relative">
<div
className={`${
productSummary.imagePlaceholder
} absolute w-100 h-100 contain bg-center`}
/>
<svg
width="100%"
height="100%"
viewBox="0 0 512 512"
fill="none"
xmlns="http://www.w3.org/2000/svg"
data-testid="image-placeholder"
>
<rect width="512" height="512" fill="#F2F2F2" />
<rect
x="183.857"
y="180.2"
width="144.286"
height="150.474"
stroke="#CACBCC"
strokeWidth="2"
/>
<path d="M183.78 303.688H328.214" stroke="#CACBCC" strokeWidth="2" />
<path
d="M205.082 279.563L223.599 240.507L242.116 260.035L269.892 220.979L306.926 279.563H205.082Z"
stroke="#CACBCC"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M252.225 213.939C252.225 219.822 247.66 224.52 242.114 224.52C236.569 224.52 232.004 219.822 232.004 213.939C232.004 208.057 236.569 203.359 242.114 203.359C247.66 203.359 252.225 208.057 252.225 213.939Z"
stroke="#CACBCC"
strokeWidth="2"
/>
</svg>
</div>
)
const ProductImageContent = ({
product,
showBadge,
badgeText,
showCollections,
displayMode,
onError,
}) => {
const {
productClusters,
productName: name,
sku: {
image: { imageUrl },
},
} = product
const imageContentClassName = classNames({
[productSummary.imageNormal]: displayMode !== 'inline',
[productSummary.imageInline]: displayMode === 'inline',
})
const commertialOffer = pathOr(
{},
['sku', 'seller', 'commertialOffer'],
product
)
const withBadge = maybeBadge({
listPrice: commertialOffer.ListPrice,
price: commertialOffer.Price,
label: badgeText,
})
const withCollection = maybeCollection({ productClusters })
const img = (
<img
className={imageContentClassName}
src={imageUrl}
alt={name}
onError={onError}
/>
)
return compose(
withBadge(showBadge),
withCollection(showCollections)
)(img)
}
const ProductImage = props => {
const { product } = useContext(ProductSummaryContext)
const [error, setError] = useState(false)
const imageClassName = classNames(productSummary.imageContainer, {
'db w-100 center': props.displayMode !== 'inline',
})
return (
<div className={imageClassName}>
{path(['sku', 'image', 'imageUrl'], product) && !error ? (
<ProductImageContent
{...props}
product={product}
onError={() => setError(true)}
/>
) : (
<ImagePlaceholder />
)}
</div>
)
}
ProductImage.propTypes = {
/** Set the discount badge's visibility */
showBadge: PropTypes.bool,
/** Text shown on badge */
badgeText: PropTypes.string,
/** Defines if the collection badges are shown */
showCollections: PropTypes.bool,
/** Display mode of the summary */
displayMode: PropTypes.oneOf(['normal', 'inline']),
}
ProductImage.defaultProps = {
showBadge: true,
showCollections: false,
displayMode: 'normal',
}
ProductImage.getSchema = () => {
return {
title: 'admin/editor.productSummary.title',
description: 'admin/editor.productSummary.description',
type: 'object',
properties: {
showBadge: {
type: 'boolean',
title: 'admin/editor.productSummary.showBadge.title',
default: ProductImage.defaultProps.showBadge,
isLayout: true,
},
showCollections: {
type: 'boolean',
title: 'admin/editor.productSummary.showCollections.title',
default: ProductImage.defaultProps.showCollections,
isLayout: true,
},
displayMode: {
title: 'admin/editor.productSummary.displayMode.title',
type: 'string',
enum: ['normal', 'inline'],
default: ProductImage.defaultProps.displayMode,
isLayout: true,
},
},
}
}
export default ProductImage