ProductEdit.js
13.5 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import TagsInput from 'react-tagsinput'
import { withRouter } from 'react-router';
import {
Input,
Label,
Form,
FormGroup,
Col,
Button,
ButtonToolbar,
Dropdown,
DropdownItem,
DropdownMenu,
DropdownToggle,
Popover,
PopoverHeader,
PopoverBody
} from "reactstrap";
import {
loadProductRequest,
receiveProduct,
updateProduct,
updateProductRequest,
createProductRequest,
deleteProductRequest,
getProductsImagesRequest
} from '../../../../../actions/products';
import Widget from '../../../../../components/Widget';
import Loader from '../../../../../components/Loader';
import s from './ProductEdit.module.scss';
class ProductEdit extends React.Component {
static propTypes = {
products: PropTypes.array,
images: PropTypes.array,
dispatch: PropTypes.func.isRequired,
};
static defaultProps = {
products: [],
images: []
};
constructor(props) {
super(props);
this.updateProductRequest = this.updateProductRequest.bind(this);
this.createProductRequest = this.createProductRequest.bind(this);
this.deleteProductRequest = this.deleteProductRequest.bind(this);
this.toggleDropdown = this.toggleDropdown.bind(this);
this.state = {
dropdownOpen: false,
popover: false
};
this.description_1 = React.createRef();
this.description_2 = React.createRef();
let newProduct = {
id: -1,
price: 0.01,
rating: 5,
technology: []
};
let product = this.findProduct(this.getId());
if (this.getId() > -1) {
if (!product) {
this.props.dispatch(loadProductRequest(this.getId()));
}
} else {
if (!product) {
this.props.dispatch(receiveProduct(newProduct));
}
}
this.props.dispatch(getProductsImagesRequest(newProduct));
}
findProduct(id) {
const {products} = this.props;
return products.find(p => p.id === id);
}
getId() {
const {match} = this.props;
return parseInt(match.params.id) || -1;
}
updateProductRequest() {
this.props.dispatch(updateProductRequest(this.findProduct(this.getId())));
}
createProductRequest() {
this.props.dispatch(createProductRequest({
product: this.findProduct(this.getId()),
history: this.props.history
}));
}
deleteProductRequest() {
debugger;
this.props.dispatch(deleteProductRequest({
id: this.getId(),
history: this.props.history
}));
}
getImage() {
let product = this.findProduct(this.getId());
return product ? product.img : null;
}
updateProduct(value, key) {
let product = this.findProduct(this.getId());
product[key] = value;
this.props.dispatch(updateProduct(product));
}
goBack() {
this.props.history.push('/app/ecommerce/management');
}
toggleDropdown() {
this.setState({
dropdownOpen: !this.state.dropdownOpen
})
}
togglePopover() {
this.setState({
popover: !this.state.popover
});
}
componentDidUpdate() {
let product = this.findProduct(this.getId()) || {
technology: []
};
if (this.description_1.current) {
this.description_1.current.value = product.description_1 || "";
}
if (this.description_2.current) {
this.description_2.current.value = product.description_2 || "";
}
}
render() {
const isNew = this.getId() === -1;
let image = this.getImage();
let product = this.findProduct(this.getId()) || {
technology: []
};
return (
<div>
<h2 className="page-title">Product - <span className="fw-semi-bold">Detail</span></h2>
<Widget title={(isNew ? "New" : "Edit") + " product"} collapse close>
{!isNew && this.props.isReceiving ? <Loader size={40}/> :
<Form>
<FormGroup row>
<Label md={2} for="productImage">Image</Label>
<Col md={5}>
<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggleDropdown}
id="productImage">
<DropdownToggle caret color="info">
{image && <img className={s.productImage} alt="img" src={image}/>}
</DropdownToggle>
<DropdownMenu>
{this.props.images.map(img => (
<DropdownItem key={img} onClick={() => this.updateProduct(img, 'img')}>
<img className={s.productImage} alt={img} src={img}/>
</DropdownItem>
))}
</DropdownMenu>
</Dropdown>
</Col>
</FormGroup>
<FormGroup row>
<Label md={2} for="productTitle">Title</Label>
<Col md={5}>
<Input id="productTitle" type="text" defaultValue={product.title}
onChange={(event) => this.updateProduct(event.target.value, 'title')}/>
</Col>
</FormGroup>
<FormGroup row>
<Label md={2} for="productSubtitle">Subtitle</Label>
<Col md={5}>
<Input id="productSubtitle" type="text" defaultValue={product.subtitle}
onChange={(event) => this.updateProduct(event.target.value, 'subtitle')}/>
</Col>
</FormGroup>
<FormGroup row>
<Label md={2} for="productPrice">Price</Label>
<Col md={2}>
<Input id="productPrice" type="number" step={0.01} min={0.01}
defaultValue={product.price}
onChange={(event) => this.updateProduct(event.target.value, 'price')}/>
</Col>
</FormGroup>
<FormGroup row>
<Label md={2} for="productDiscount">Discount</Label>
<Col md={2}>
<Input id="productDiscount" type="number" step={1} min={0} max={100}
defaultValue={product.discount || 0}
onChange={(event) => this.updateProduct(event.target.value, 'discount')}/>
</Col>
</FormGroup>
<FormGroup row>
<Label md={2} for="productDescription_1">Description 1</Label>
<Col md={5}>
<textarea rows={3} className="form-control" id="productDescription_1"
ref={this.description_1}
onChange={(event) => this.updateProduct(event.target.value, 'description_1')}/>
</Col>
</FormGroup>
<FormGroup row>
<Label md={2} for="productDescription_2">Description 2</Label>
<Col md={5}>
<textarea rows={3} className="form-control" id="productDescription_2"
ref={this.description_2}
onChange={(event) => this.updateProduct(event.target.value, 'description_2')}/>
</Col>
</FormGroup>
<FormGroup row>
<Label md={2} for="productCode">Code</Label>
<Col md={2}>
<Input id="productCode" type="text"
defaultValue={product.code}
onChange={(event) => this.updateProduct(event.target.value, 'code')}/>
</Col>
</FormGroup>
<FormGroup row>
<Label md={2} for="productHashtag">Hashtag</Label>
<Col md={5}>
<Input id="productHashtag" type="text"
defaultValue={product.hashtag}
onChange={(event) => this.updateProduct(event.target.value, 'hashtag')}/>
</Col>
</FormGroup>
<FormGroup row>
<Label md={2} for="productTechnology">Technology</Label>
<Col md={5}>
<TagsInput className="react-tagsinput form-control" id="productTechnology"
value={product.technology}
onChange={(tags) => this.updateProduct(tags, 'technology')}/>
</Col>
</FormGroup>
<FormGroup row>
<Label md={2} for="productRating">Rating</Label>
<Col md={2}>
<Input id="productRating" step={0.1} min={0} max={5} type="number"
defaultValue={product.rating}
onChange={(event) => this.updateProduct(event.target.value, 'rating')}/>
</Col>
</FormGroup>
<ButtonToolbar>
<Button color="success"
onClick={!isNew ? this.updateProductRequest : this.createProductRequest}>
{this.props.isUpdating ? <Loader/> : 'Save'}
</Button>
<Button color="default" onClick={() => {
this.goBack()
}}>Back</Button>
{!isNew && (
<span>
<Button id="deleteProductPopover" color="danger">
{this.props.isDeleting ? <Loader/> : 'Delete'}
</Button>
<Popover className="popover-danger" target="deleteProductPopover"
placement="top"
isOpen={this.state.popover}
toggle={() => {
this.togglePopover()
}}
>
<PopoverHeader className="px-5">Are you sure?</PopoverHeader>
<PopoverBody className="px-5 d-flex justify-content-center">
<ButtonToolbar>
<Button color="success" size="xs" onClick={this.deleteProductRequest}>
Yes
</Button>
<Button color="danger" size="xs" onClick={() => {
this.togglePopover()
}}>
No
</Button>
</ButtonToolbar>
</PopoverBody>
</Popover>
</span>
)}
</ButtonToolbar>
</Form>
}
</Widget>
</div>
);
}
}
function mapStateToProps(state) {
return {
products: state.products.data,
images: state.products.images,
isReceiving: state.products.isReceiving,
isUpdating: state.products.isUpdating,
isDeleting: state.products.isDeleting,
};
}
export default withRouter(connect(mapStateToProps)(ProductEdit));