-
Notifications
You must be signed in to change notification settings - Fork 2
/
Selection.jsx
181 lines (163 loc) · 4.07 KB
/
Selection.jsx
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
170
171
172
173
174
175
176
177
178
179
180
181
import React from 'react';
import axios from 'axios';
import styled from 'styled-components';
import { Row } from '../shared/containers';
const { useState } = React;
const StyleSelect = styled.select`
appearance: none;
display: block;
padding-left: 20px;
padding-right: 10px;
margin-right: 15px;
margin-top: 12px;
font-family: inherit;
font-weight: bold;
background-color: white;
border-radius: 0%;
border-width: 2px;
height: 55px;
width: 150px;
&:hover {
background-color: rgb(220,220,220);
cursor: pointer;
}
&:focus {
border-radius: 0%;
border-width: 3px;
outline: 0;
}
`;
const QuantSelect = styled(StyleSelect)`
padding-left: 22px;
margin-right: 20px;
width: 55px;
`;
const SelectOption = styled.option`
text-align: left;
padding: 10px;
margin-top: 60px;
margin-bottom: 20px;
margin-right: 20px;
font-family: inherit;
font-weight: bold;
background-color: white;
border-radius: 0%;
border-width: 2px;
height: 60px;
width: 150px;
`;
const CartButton = styled.button`
display: flex;
justify-content: center;
align-items: center;
margin-top: 15px;
font-weight: bold;
font-family: inherit;
background-color: white;
height: 55px;
width: 220px;
line-height: 50px;
&:hover {
background-color: rgb(220,220,220);
cursor: pointer;
}
&:focus {
border-radius: 0%;
border-width: 2px;
outline: 0;
}
`;
const Alert = styled.h5`
line-height; 12px;
padding: 0;
margin: 0;
`;
function Selection({ style, sku, changeSku }) {
const skuTuples = Object.entries(style.skus);
const [currentQty, setCurrentQty] = useState();
const [hideLabel, setHideLabel] = useState(true);
const addToCartHandler = () => {
if (!sku) {
setHideLabel(false);
document.getElementById('size-select').focus();
} else {
let count = currentQty;
while (count > 0) {
axios.post('/cart', { sku_id: sku })
.catch((error) => console.error(error));
count -= 1;
}
}
};
const skuSizeOptions = skuTuples.map((skuTuple) => {
const code = skuTuple[0];
const { size, quantity } = skuTuple[1];
if (quantity > 0) {
return (
<SelectOption key={code} data-sku={code} id={size}>{size}</SelectOption>
);
}
return '';
});
let sizeOptions = (
<>
<SelectOption style={{ display: 'none' }}>SELECT SIZE 🡳</SelectOption>
{skuSizeOptions}
</>
);
let hideCart = false;
if (skuSizeOptions.length === 0) {
sizeOptions = <SelectOption style={{ display: 'none' }}>OUT OF STOCK</SelectOption>;
hideCart = true;
}
const buildQuantityList = (qty) => {
const list = [];
const max = qty > 15 ? 15 : qty;
for (let i = 1; i <= max; i += 1) {
list.push(<SelectOption key={i}>{i}</SelectOption>);
}
return list;
};
const quantity = sku ? style.skus[sku].quantity : 0;
const quantityOptions = sku ? buildQuantityList(quantity) : <SelectOption> - </SelectOption>;
return (
<div>
{hideLabel ? (
<Alert> </Alert>
) : (
<Alert>Please select a size</Alert>
)}
<Row>
<StyleSelect
name="size"
id="size-select"
style={hideLabel ? { marginTop: '17px' } : { marginTop: '0' }}
onChange={(e) => {
changeSku(document.getElementById(e.target.value).dataset.sku);
setHideLabel(true);
}}
>
{sizeOptions}
</StyleSelect>
<QuantSelect
disabled={!sku}
name="quantity"
style={hideLabel ? { marginTop: '17px' } : { marginTop: '0' }}
onChange={(e) => setCurrentQty(e.target.value)}
>
{!sku && <SelectOption style={{ display: 'none' }}> - </SelectOption>}
{quantityOptions}
</QuantSelect>
<CartButton
name="add"
type="button"
style={hideCart ? { display: 'none' } : { display: 'inherit' }}
onClick={() => addToCartHandler()}
>
ADD TO BAG
</CartButton>
</Row>
</div>
);
}
export default Selection;