-
Notifications
You must be signed in to change notification settings - Fork 8
/
CheckoutCartListItem.js
61 lines (54 loc) · 1.72 KB
/
CheckoutCartListItem.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
import Link from 'next/link'
import styled from 'styled-components'
export default function CheckoutCartListItem({ cartListItem, currency, currencyRate }) {
const id = cartListItem.id
const attributes = cartListItem.attributes
const selectedAmount = cartListItem.selectedAmount
const available = attributes.available
const discount = attributes.discount.data
let discountMultiplier = discount !== null ? discount.attributes.discountMultiplier : 1
const isSelectedGreaterThanAvailable = available !== 0 && selectedAmount > available
// DRY JSX
const content = (
<>
<Link href={`/product-page/${id}`}>
<a className="product-link">{attributes.title}</a>
</Link>
<span className="selected-amount">
×{available !== 0 && selectedAmount > available ? 0 : selectedAmount}
</span>
<span className="price">
{currency}
{
available !== 0 && selectedAmount > available
? (0).toFixed(2)
: (((attributes.price * currencyRate) * discountMultiplier) * selectedAmount).toFixed(2)
}
</span>
</>
)
if (available <= 0) {
return (
<s key={id} className="checkout-cart-list-item">
{content}
<h6 className="out-of-stock-text text-danger">
<b>OUT OF STOCK!</b>
</h6>
</s>
)
} else {
return (
<ItemP
key={id}
isSelectedGreaterThanAvailable={isSelectedGreaterThanAvailable}
className="checkout-cart-list-item"
>
{content}
</ItemP>
)
}
}
const ItemP = styled.p`
color: ${props => props.isSelectedGreaterThanAvailable ? '#dc3545' : 'black'};
`