1
|
|
|
import React, { Fragment, useContext, useState } from 'react'; |
2
|
|
|
import PropTypes from 'prop-types'; |
3
|
|
|
import AppContext, { ProductContext } from '../../context/Context'; |
4
|
|
|
import { Card, CardBody, Col, Media, Row } from 'reactstrap'; |
5
|
|
|
import { Link } from 'react-router-dom'; |
6
|
|
|
import { calculateSale, isIterableArray } from '../../helpers/utils'; |
7
|
|
|
import FalconCardHeader from '../common/FalconCardHeader'; |
8
|
|
|
import ButtonIcon from '../common/ButtonIcon'; |
9
|
|
|
|
10
|
|
|
const FavouriteItem = ({ id }) => { |
11
|
|
|
const { currency } = useContext(AppContext); |
12
|
|
|
const { products, handleCartAction, favouriteItemsDispatch } = useContext(ProductContext); |
13
|
|
|
const [cartLoading, setCartLoading] = useState(false); |
14
|
|
|
|
15
|
|
|
const handleAddToCart = () => { |
16
|
|
|
setCartLoading(true); |
17
|
|
|
setTimeout(() => { |
18
|
|
|
handleCartAction({ id }); |
19
|
|
|
setCartLoading(false); |
20
|
|
|
}, 1000); |
21
|
|
|
}; |
22
|
|
|
|
23
|
|
|
const { title, files, price, sale } = products.find(product => product.id === id); |
24
|
|
|
|
25
|
|
|
return ( |
26
|
|
|
<Row noGutters className="align-items-center px-1 border-bottom border-200"> |
27
|
|
|
<Col xs={8} className="py-3 px-2 px-md-3"> |
28
|
|
|
<Media className="align-items-center"> |
29
|
|
|
<Link to={`/e-commerce/product-details/${id}`}> |
30
|
|
|
<img |
31
|
|
|
className="rounded mr-3 d-none d-md-block" |
32
|
|
|
src={files[0]['src'] || files[0]['base64']} |
33
|
|
|
alt="" |
34
|
|
|
width="60" |
35
|
|
|
/> |
36
|
|
|
</Link> |
37
|
|
|
<Media body> |
38
|
|
|
<h5 className="fs-0"> |
39
|
|
|
<Link className="text-900" to={`/e-commerce/product-details/${id}`}> |
40
|
|
|
{title} |
41
|
|
|
</Link> |
42
|
|
|
</h5> |
43
|
|
|
<div |
44
|
|
|
className="fs--2 fs-md--1 text-danger cursor-pointer" |
45
|
|
|
onClick={() => favouriteItemsDispatch({ type: 'REMOVE', id })} |
46
|
|
|
> |
47
|
|
|
Remove |
48
|
|
|
</div> |
49
|
|
|
</Media> |
50
|
|
|
</Media> |
51
|
|
|
</Col> |
52
|
|
|
<Col xs={4} className="p-3"> |
53
|
|
|
<Row className="align-items-center"> |
54
|
|
|
<Col md={4} className="d-flex justify-content-end justify-content-md-center px-2 px-md-3 order-1 order-md-0"> |
55
|
|
|
{currency} |
56
|
|
|
{calculateSale(price, sale)} |
57
|
|
|
</Col> |
58
|
|
|
<Col md={8} className="text-right pl-0 pr-2 pr-md-3 order-0 order-md-1 mb-2 mb-md-0 text-600"> |
59
|
|
|
{cartLoading ? ( |
60
|
|
|
<ButtonIcon |
61
|
|
|
color="outline-primary" |
62
|
|
|
size="sm" |
63
|
|
|
icon="circle-notch" |
64
|
|
|
iconClassName="fa-spin ml-2 d-none d-md-inline-block" |
65
|
|
|
style={{ cursor: 'progress' }} |
66
|
|
|
disabled |
67
|
|
|
> |
68
|
|
|
Processing |
69
|
|
|
</ButtonIcon> |
70
|
|
|
) : ( |
71
|
|
|
<ButtonIcon |
72
|
|
|
color="outline-primary" |
73
|
|
|
size="sm" |
74
|
|
|
icon="cart-plus" |
75
|
|
|
iconClassName="ml-2 d-none d-md-inline-block" |
76
|
|
|
onClick={handleAddToCart} |
77
|
|
|
> |
78
|
|
|
Add to Cart |
79
|
|
|
</ButtonIcon> |
80
|
|
|
)} |
81
|
|
|
</Col> |
82
|
|
|
</Row> |
83
|
|
|
</Col> |
84
|
|
|
</Row> |
85
|
|
|
); |
86
|
|
|
}; |
87
|
|
|
|
88
|
|
|
FavouriteItem.propTypes = { id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired }; |
89
|
|
|
|
90
|
|
|
const FavouriteItems = () => { |
91
|
|
|
const { favouriteItems } = useContext(ProductContext); |
92
|
|
|
|
93
|
|
|
return ( |
94
|
|
|
<Card> |
95
|
|
|
<FalconCardHeader |
96
|
|
|
title={`Favourites (${favouriteItems.length} Item${favouriteItems.length === 1 ? '' : 's'})`} |
97
|
|
|
light={false} |
98
|
|
|
> |
99
|
|
|
<ButtonIcon |
100
|
|
|
icon="chevron-left" |
101
|
|
|
color="primary" |
102
|
|
|
size="sm" |
103
|
|
|
className="border-300" |
104
|
|
|
tag={Link} |
105
|
|
|
to="/e-commerce/products/list" |
106
|
|
|
> |
107
|
|
|
Continue Shopping |
108
|
|
|
</ButtonIcon> |
109
|
|
|
</FalconCardHeader> |
110
|
|
|
<CardBody className="p-0"> |
111
|
|
|
{isIterableArray(favouriteItems) ? ( |
112
|
|
|
<Fragment> |
113
|
|
|
<Row noGutters className="bg-200 text-900 px-1 fs--1 font-weight-semi-bold"> |
114
|
|
|
<Col xs={9} md={8} className="p-2 px-md-3"> |
115
|
|
|
Name |
116
|
|
|
</Col> |
117
|
|
|
<Col xs={3} md={4} className="px-3"> |
118
|
|
|
<Row> |
119
|
|
|
<Col md={4} className="py-2 d-none d-md-block text-center"> |
120
|
|
|
Price |
121
|
|
|
</Col> |
122
|
|
|
<Col md={8} className="text-right p-2 px-md-3"> |
123
|
|
|
Cart |
124
|
|
|
</Col> |
125
|
|
|
</Row> |
126
|
|
|
</Col> |
127
|
|
|
</Row> |
128
|
|
|
{favouriteItems.map(favouriteItem => ( |
129
|
|
|
<FavouriteItem {...favouriteItem} key={favouriteItem.id} /> |
130
|
|
|
))} |
131
|
|
|
</Fragment> |
132
|
|
|
) : ( |
133
|
|
|
<p className="p-card mb-0 bg-light">0 items in your Favourite list. Go ahead and add something!</p> |
134
|
|
|
)} |
135
|
|
|
</CardBody> |
136
|
|
|
</Card> |
137
|
|
|
); |
138
|
|
|
}; |
139
|
|
|
|
140
|
|
|
export default FavouriteItems; |
141
|
|
|
|