-
Notifications
You must be signed in to change notification settings - Fork 92
/
CartList.tsx
146 lines (130 loc) · 4.19 KB
/
CartList.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
import { TAttribute, TProduct, TStoreListItem } from '@cromwell/core';
import { getCStore, LoadBox as BaseLoadBox, useCart } from '@cromwell/core-frontend';
import React, { useEffect, useState } from 'react';
import { TBaseButton } from '../shared/Button';
import styles from './CartList.module.scss';
import { CartListItem, CartListItemProps } from './CartListItem';
import { useModuleState } from '../../helpers/state';
import { useStoreAttributes } from '../../helpers/useStoreAttributes';
import clsx from 'clsx';
export type CartListProps = {
classes?: Partial<
Record<
| 'root'
| 'list'
| 'listItem'
| 'cartHeader'
| 'cartTotal'
| 'cartTotalText'
| 'productList'
| 'imageBlock'
| 'imageLink'
| 'image'
| 'captionBlock'
| 'productName'
| 'priceBlock'
| 'oldPrice'
| 'price'
| 'attributesBlock'
| 'actionsBlock'
| 'attributeValue',
string
>
>;
elements?: {
Loadbox?: React.ComponentType;
Button?: TBaseButton;
DeleteIcon?: React.ComponentType;
ListItem?: React.ComponentType<CartListItemProps>;
HeaderActions?: React.ComponentType;
};
text?: {
total?: string;
};
/** All available attributes */
attributes?: TAttribute[];
/**
* Fires when clicked link for a product page.
*/
onProductClick?: (event: React.MouseEvent, product: TProduct) => void;
/**
* Pass custom cart to override retrieved cart from CStore
*/
cart?: TStoreListItem[];
/**
* Hide delete button for products from the list?
*/
hideDelete?: boolean;
/**
* Custom resolver of a link to product page
*/
getProductLink?: (product: TProduct) => string | undefined;
/**
* Position of cart header with sums (cart total)
*/
sumPosition?: 'top' | 'bottom' | 'none';
};
/**
* Displays product list of user cart. Use CStore API from `@cromwell/core-frontend`
* package to add products in the list
*/
export function CartList(props: CartListProps) {
const { elements, classes, text, sumPosition = 'top' } = props;
const { Loadbox = BaseLoadBox, ListItem = CartListItem, HeaderActions } = elements ?? {};
const cstore = getCStore();
const moduleState = useModuleState();
const attributes = useStoreAttributes(props.attributes);
const cart = useCart();
const [isLoading, setIsLoading] = useState<boolean>(false);
let viewCart = (props.cart ?? moduleState.paymentSession?.cart ?? cart) as TStoreListItem[] | undefined;
if (typeof viewCart === 'string') {
try {
viewCart = JSON.parse(viewCart);
} catch (error) {
console.error(error);
viewCart = undefined;
}
}
const cartInfo = cstore.getCartTotal(viewCart);
const cartTotal = cartInfo.total;
const cartTotalOldPrice = cartInfo.totalOld;
useEffect(() => {
/**
* Since getCart method wll retrieve products from local storage and
* after a while products can be modified at the server, we need to refresh cart first
*/
if (!props.cart) {
(async () => {
setIsLoading(true);
await cstore.updateCart(attributes || []);
setIsLoading(false);
})();
}
}, []);
const listJsx = (
<div className={clsx(styles.productList, classes?.productList)}>
{isLoading && <Loadbox />}
{!isLoading &&
viewCart?.map((item, i) => <ListItem item={item} key={item?.product?.id ?? i} cartProps={props} />)}
</div>
);
const headerJsx = (
<div className={clsx(styles.cartHeader, classes?.cartHeader)}>
<div className={clsx(styles.cartTotal, classes?.cartTotal)}>
<p className={clsx(styles.cartTotalText, classes?.cartTotalText)}>{text?.total ?? 'Total: '}</p>
{cartTotalOldPrice !== cartTotal && (
<p className={clsx(styles.oldPrice, classes?.oldPrice)}>{cstore.getPriceWithCurrency(cartTotalOldPrice)}</p>
)}
<span className={clsx(styles.price, classes?.price)}>{cstore.getPriceWithCurrency(cartTotal)}</span>
</div>
{HeaderActions && <HeaderActions />}
</div>
);
return (
<div className={clsx(styles.CartList, classes?.root)}>
{sumPosition === 'top' && headerJsx}
{listJsx}
{sumPosition === 'bottom' && headerJsx}
</div>
);
}