-
Notifications
You must be signed in to change notification settings - Fork 92
/
ProductActions.tsx
320 lines (295 loc) · 9.46 KB
/
ProductActions.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { TAttribute, TCromwellNotify, TProduct, TStoreListItem } from '@cromwell/core';
import { CContainer, getCStore, useForceUpdate } from '@cromwell/core-frontend';
import clsx from 'clsx';
import React, { useEffect, useState } from 'react';
import { getNotifier, NotifierActionOptions } from '../../helpers/notifier';
import { useModuleState } from '../../helpers/state';
import { useStoreAttributes } from '../../helpers/useStoreAttributes';
import {
AddShoppingCartIcon as BaseAddShoppingCartIcon,
FavoriteActiveIcon as BaseFavoriteIcon,
ShoppingCartIcon as BaseShoppingCartIcon,
} from '../icons';
import { TBaseAlert } from '../shared/Alert';
import { BaseButton, TBaseButton } from '../shared/Button';
import styles from './ProductActions.module.scss';
export type ProductActionsProps = {
classes?: Partial<Record<'root' | 'actionsCartBlock' | 'actionButton' | 'actionsWishlistBlock', string>>;
elements?: {
Button?: TBaseButton;
Alert?: TBaseAlert;
ShoppingCartIcon?: React.ComponentType;
AddShoppingCartIcon?: React.ComponentType;
FavoriteIcon?: React.ComponentType;
QuantityField?: React.ComponentType<{
value: number;
onChange: (value: number, event?: any) => any;
className?: string;
style?: React.CSSProperties;
}>;
};
text?: {
addedClickToOpenCart?: string;
productIsInCart?: string;
productIsInWishlist?: string;
pickFollowingAttributes?: string;
addedClickToOpenWishlist?: string;
addToCart?: string;
addToWishlist?: string;
outOfStock?: string;
onBackorder?: string;
updateQuantity?: string;
openCart?: string;
openWishlist?: string;
};
/** Product data. Required */
product: TProduct;
/**
* All available attributes in DB. If not passed will be fetched and cached automatically.
*/
attributes?: TAttribute[];
/**
* Action events
*/
onCartOpen?: () => any;
onWishlistOpen?: () => any;
/**
* Notifier tool. Will show notifications when user adds a product to the cart or
* wishlist. To disable notifications pass an empty object
*/
notifier?: TCromwellNotify<NotifierActionOptions>;
/**
* Notifier options
*/
notifierOptions?: NotifierActionOptions;
/**
* Override modified product by `onChange` of `ProductAttributes` component.
* (not recommended since modifications already stored in the `moduleState`)
*/
modifiedProduct?: TProduct | null;
/**
* Disable editing of inner blocks in Theme editor
*/
disableEdit?: boolean;
};
/**
* Displays actions (buttons) on product page such as: add to cart/wishlist,
* amount picker. Handles cart and shows notifications.
*/
export function ProductActions(props: ProductActionsProps) {
const moduleState = useModuleState();
const {
product,
onCartOpen,
modifiedProduct = (product?.id ? moduleState.products[product?.id]?.modifiedProduct : undefined) ?? product,
onWishlistOpen,
notifier = getNotifier(),
notifierOptions = {},
elements = {},
disableEdit,
classes,
text,
} = props;
// Custom / default elements
const {
Button = BaseButton,
AddShoppingCartIcon = BaseAddShoppingCartIcon,
ShoppingCartIcon = BaseShoppingCartIcon,
FavoriteIcon = BaseFavoriteIcon,
QuantityField = DefaultQuantityField,
} = elements;
const forceUpdate = useForceUpdate();
const attributes = useStoreAttributes(props.attributes);
const cstore = getCStore();
const [amount, setAmount] = useState(1);
const pickedAttributes = product?.id ? moduleState.products[product?.id]?.pickedAttributes : undefined;
const item: TStoreListItem = {
product: product ?? undefined,
pickedAttributes: pickedAttributes,
amount,
};
const inCart = cstore.isInCart(item);
const sameQntInCart = cstore.hasSameQntInCart(item);
const inWishlist = product && cstore.isInWishlist({ product });
// const inCompare = cstore.isInCompare({ product });
useEffect(() => {
const onCartUpdateId = cstore.onCartUpdate(() => {
forceUpdate();
});
const onUpdateId =
product?.id &&
moduleState.addOnProductUpdateListener(product.id, () => {
forceUpdate();
});
return () => {
cstore.removeOnCartUpdate(onCartUpdateId);
if (product?.id) {
delete moduleState.products[product?.id];
if (onUpdateId) moduleState.removeOnProductUpdateListener(product.id, onUpdateId);
}
};
}, []);
const handleAddToCart = () => {
if (inCart) {
if (sameQntInCart) {
onCartOpen?.();
} else {
cstore.updateQntInCart(item);
forceUpdate();
}
} else {
const result = cstore.addToCart(item, attributes);
if (result.success) {
notifier?.success?.(text?.addedClickToOpenCart ?? 'Added! Click here to open cart', {
onClick: () => {
onCartOpen?.();
},
...notifierOptions,
});
}
if (result.code === 1) {
notifier?.warning?.(text?.productIsInCart ?? 'Product is already in your cart!', {
...notifierOptions,
});
}
if (result.missingAttributes?.length) {
notifier?.error?.(
`${text?.pickFollowingAttributes ?? 'Please pick following attributes:'} ${result.missingAttributes
.map((attr) => attr.key)
.join(', ')}`,
{
...notifierOptions,
},
);
if (product?.id) moduleState.setCanValidate(product?.id, true);
}
forceUpdate();
}
};
const handleAddToWishlist = () => {
if (!product) return;
if (inWishlist) {
onWishlistOpen?.();
} else {
const hasBeenAdded = cstore.addToWishlist({ product });
if (hasBeenAdded) {
notifier?.success?.(text?.addedClickToOpenWishlist ?? 'Added! Click here to open wishlist', {
onClick: () => {
onWishlistOpen?.();
},
...notifierOptions,
});
} else {
notifier?.warning?.(text?.productIsInWishlist ?? 'Product is already in your wishlist!', {
...notifierOptions,
});
}
forceUpdate();
}
};
// const handleAddToCompare = () => {
// if (inCompare) {
// appState.isCompareOpen = true;
// } else {
// const hasBeenAdded = cstore.addToCompare({ product });
// if (hasBeenAdded) {
// toast.success("Added! Click here to compare", {
// position: toast.POSITION.TOP_RIGHT,
// onClick: () => {
// appState.isCompareOpen = true;
// }
// });
// } else {
// toast.warn("Product is already in your list!", {
// position: toast.POSITION.TOP_RIGHT
// });
// }
// forceUpdate();
// }
// }
const outOfStock = !!(
modifiedProduct?.stockStatus === 'Out of stock' ||
modifiedProduct?.stockStatus === 'On backorder' ||
(modifiedProduct?.manageStock && modifiedProduct?.stockAmount && modifiedProduct?.stockAmount < amount)
);
let cartButtonText = text?.addToCart ?? 'Add to cart';
if (inCart) {
if (sameQntInCart) {
cartButtonText = text?.openCart ?? 'Open cart';
} else {
cartButtonText = text?.updateQuantity ?? 'Update qty';
}
}
if (outOfStock) {
cartButtonText = text?.outOfStock ?? 'Out of stock';
}
return (
<CContainer
className={clsx(styles.ProductActions, classes?.root)}
id="ccom_product_actions"
editorHidden={disableEdit}
>
<CContainer
className={clsx(styles.actionsCartBlock, classes?.actionsCartBlock)}
id="ccom_product_actions_cart"
editorHidden={disableEdit}
>
<Button
onClick={handleAddToCart}
variant="contained"
color="primary"
size="large"
className={clsx(styles.actionButton, classes?.actionButton)}
disabled={outOfStock}
startIcon={inCart ? <ShoppingCartIcon /> : <AddShoppingCartIcon />}
>
{cartButtonText}
</Button>
<QuantityField
className={clsx(styles.actionButton, classes?.actionButton)}
value={amount}
onChange={(value) => {
const valNum = Number(value);
if (valNum && !isNaN(valNum)) setAmount(valNum);
}}
/>
</CContainer>
<CContainer
className={clsx(styles.actionsWishlistBlock, classes?.actionsWishlistBlock)}
id="ccom_product_actions_wishlist"
editorHidden={disableEdit}
>
<Button
onClick={handleAddToWishlist}
variant="outlined"
color="primary"
size="large"
className={clsx(styles.actionButton, classes?.actionButton)}
startIcon={<FavoriteIcon />}
>
{inWishlist ? text?.openWishlist ?? 'Open Wishlist' : text?.addToWishlist ?? 'Add to wishlist'}
</Button>
{/* <Button
onClick={handleAddToCompare}
variant="outlined"
color="primary"
size="large"
className={clsx(styles.actionButton, commonStyles.button)}
startIcon={<EqualizerIcon />}
>{inCompare ? 'Open comparison list' : 'Compare'}</Button> */}
</CContainer>
</CContainer>
);
}
/** @internal */
const DefaultQuantityField = (props) => {
return (
<input
value={props.value + ''}
type="number"
min="1"
style={{ maxWidth: '50px' }}
onChange={(event) => props.onChange(Number(event.target.value))}
/>
);
};