-
Notifications
You must be signed in to change notification settings - Fork 9
/
ProductListItem.js
169 lines (155 loc) · 5.01 KB
/
ProductListItem.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
import { useState, useEffect, useContext } from 'react'
import Link from 'next/link'
import CurrencyContext from '../context/currencyContext'
import styled from 'styled-components'
export default function ProductListItem({ item }) {
const id = item.id
const attributes = item.attributes
const title = attributes.title
const price = attributes.price
const imgsArr = attributes.image.data
const img = imgsArr.filter(i => i.attributes.name === "01.jpg")
const imgUrl = img[0].attributes.url
let discountAttributes
let discountPercent
let discountMultiplier
if (attributes.discount.data !== null) {
discountAttributes = attributes.discount.data.attributes
discountPercent = discountAttributes.discountPercent
discountMultiplier = discountAttributes.discountMultiplier
}
const { isCurrencySet, currency, currencyRate } = useContext(CurrencyContext)
const [ isItemInCart, setIsItemInCart ] = useState(false)
const [ quantity, setQuantity ] = useState(0)
const setItemAmountInCart = () => {
if (localStorage.getItem('cartList') !== null) {
const cartList = JSON.parse(localStorage.cartList)
const item = cartList.find(cartListItem => cartListItem.id === id)
if (item !== undefined) {
setIsItemInCart(true)
setQuantity(item.selectedAmount)
}
}
}
useEffect(() => {
setItemAmountInCart()
}, [])
return (
<DivProductListItem>
<Link href={`/product-page/${id}`}>
<a className="link">
<img alt={title} src={imgUrl} width={200} height={200} className="image" />
<br />
<div className="title">{title}</div>
<br />
{
isCurrencySet
? (
<div className="price-and-in-cart-wrapper">
{
attributes.discount.data === null
? (
<div className="price">
<span className="d-flex no-wrap">
<span>{currency}</span>
<span>{(parseFloat(price * currencyRate)).toFixed(2)}</span>
</span>
</div>
) : (
<div className="price d-flex">
<h5 className="discount-text text-success">
{discountPercent} OFF!
</h5>
<span className="d-flex flex-column">
<s className="d-flex no-wrap">
<span>{currency}</span>
<span>{(parseFloat(price * currencyRate)).toFixed(2)}</span>
</s>
<span className="d-flex no-wrap text-danger">
<span>{currency}</span>
<span>{(parseFloat((price * currencyRate) * discountMultiplier)).toFixed(2)}</span>
</span>
</span>
</div>
)
}
{
attributes.available <= 0
? (
<div className="out-of-stock">
<h4 className="out-of-stock-text text-danger">
OUT OF STOCK!
</h4>
</div>
) : attributes.available > 0 && isItemInCart ? (
<div className="in-cart">In cart: {quantity}</div>
) : null
}
</div>
) : (
<div className="loader"></div>
)
}
</a>
</Link>
</DivProductListItem>
)
}
const DivProductListItem = styled.div`
height: 427px;
width: 290px;
background: #f8f9fa;
border-radius: 10px;
&:hover {
box-shadow: .1rem .1rem 1rem .1rem rgba(0,0,0,.3);
text-decoration: none;
}
> .link {
height: 100%;
display: flex;
flex-direction: column;
padding: 1.5em;
color: #343a40;
&:hover {
text-decoration: none;
}
> .image {
align-self: center;
}
> .title {
font-size: 1.5rem;
}
> .price-and-in-cart-wrapper {
display: flex;
justify-content: space-between;
> .price {
font-size: 1.5rem;
position: relative;
> .discount-text {
position: absolute;
top: -12px;
right: -35px;
transform: rotate(24deg);
}
}
> .out-of-stock {
position: relative;
align-self: flex-start;
> .out-of-stock-text {
position: absolute;
top: 6px;
right: 7px;
transform: rotate(24deg);
}
}
> .in-cart {
align-self: flex-start;
background: #dc3545;
color: white;
margin: 0 1.5em 0 1.5em;
padding: .3em .5em .2em .5em;
border-radius: 5px;
}
}
}
`