-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathShoeCard.js
130 lines (115 loc) · 2.87 KB
/
ShoeCard.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
import React from 'react';
import styled from 'styled-components/macro';
import { COLORS, WEIGHTS } from '../../constants';
import { formatPrice, pluralize, isNewShoe } from '../../utils';
import Spacer from '../Spacer';
const ShoeCard = ({
slug,
name,
imageSrc,
price,
salePrice,
releaseDate,
numOfColors,
}) => {
// There are 3 variants possible, based on the props:
// - new-release
// - on-sale
// - default
//
// Any shoe released in the last month will be considered
// `new-release`. Any shoe with a `salePrice` will be
// on-sale. In theory, it is possible for a shoe to be
// both on-sale and new-release, but in this case, `on-sale`
// will triumph and be the variant used.
// prettier-ignore
const variant = typeof salePrice === 'number'
? 'on-sale'
: isNewShoe(releaseDate)
? 'new-release'
: 'default'
return (
<Link href={`/shoe/${slug}`}>
<Wrapper>
<ImageWrapper>
<Image alt="" src={imageSrc} />
{variant === 'on-sale' && <SaleFlag>Sale</SaleFlag>}
{variant === 'new-release' && (
<NewFlag>Just released!</NewFlag>
)}
</ImageWrapper>
<Spacer size={12} />
<Row>
<Name>{name}</Name>
<Price
style={{
'--color':
variant === 'on-sale' ? COLORS.gray[700] : undefined,
'--text-decoration':
variant === 'on-sale' ? 'line-through' : undefined,
}}
>
{formatPrice(price)}
</Price>
</Row>
<Row>
<ColorInfo>{pluralize('Color', numOfColors)}</ColorInfo>
{variant === 'on-sale' ? (
<SalePrice>{formatPrice(salePrice)}</SalePrice>
) : undefined}
</Row>
</Wrapper>
</Link>
);
};
const Link = styled.a`
text-decoration: none;
color: inherit;
`;
const Wrapper = styled.article``;
const ImageWrapper = styled.div`
position: relative;
`;
const Image = styled.img`
width: 100%;
`;
const Row = styled.div`
font-size: 1rem;
display: flex;
justify-content: space-between;
`;
const Name = styled.h3`
font-weight: ${WEIGHTS.medium};
color: ${COLORS.gray[900]};
`;
const Price = styled.span`
color: var(--color);
text-decoration: var(--text-decoration);
`;
const ColorInfo = styled.p`
color: ${COLORS.gray[700]};
`;
const SalePrice = styled.span`
font-weight: ${WEIGHTS.medium};
color: ${COLORS.primary};
`;
const Flag = styled.div`
position: absolute;
top: 12px;
right: -4px;
background: red;
height: 32px;
line-height: 32px;
padding: 0 10px;
font-size: ${14 / 18}rem;
font-weight: ${WEIGHTS.bold};
color: ${COLORS.white};
border-radius: 2px;
`;
const SaleFlag = styled(Flag)`
background-color: ${COLORS.primary};
`;
const NewFlag = styled(Flag)`
background-color: ${COLORS.secondary};
`;
export default ShoeCard;