1
+ import { useState , useEffect } from 'react' ;
2
+ import { useDispatch , useSelector } from 'react-redux' ;
3
+ import { Link } from 'react-router-dom' ;
4
+ import Header from '../../Header' ;
5
+ import Sidebar from '../../Sidebar' ;
6
+ import Footer from '../../Footer' ;
7
+ import { editProduct } from '../../../redux/actions/ProductActions' ;
8
+ import { updateProduct } from '../../../redux/actions/ProductActions' ;
9
+ import { PRODUCT_UPDATE_RESET } from "../../../redux/constants/ProductConstants" ;
10
+ import { toast } from "react-toastify" ;
11
+ import { ToastObjects } from '../../../redux/actions/toastObject' ;
12
+
13
+ const EditProduct = ( { match} ) => {
14
+ const productId = match . params . id ;
15
+ const [ submitted , setSubmitted ] = useState ( false ) ;
16
+ const dispatch = useDispatch ( ) ;
17
+
18
+ const productEdit = useSelector ( ( state ) => state . productEdit ) ;
19
+ const { loading, error, product } = productEdit ;
20
+
21
+ const productUpdate = useSelector ( ( state ) => state . productUpdate ) ;
22
+ const {
23
+ loading : loadingUpdate ,
24
+ error : errorUpdate ,
25
+ success : successUpdate ,
26
+ } = productUpdate ;
27
+
28
+ const [ formState , setFormState ] = useState ( {
29
+ values :{ }
30
+ } ) ;
31
+
32
+ useEffect ( ( ) => {
33
+ setFormState ( { values :{ } } )
34
+ if ( successUpdate ) {
35
+ dispatch ( { type : PRODUCT_UPDATE_RESET } ) ;
36
+ toast . success ( "Product updated successfully" , ToastObjects ) ;
37
+ } else {
38
+ if ( ! product . title || product . _id !== productId ) {
39
+ dispatch ( editProduct ( productId ) ) ;
40
+ } else {
41
+ setFormState ( { values :product } )
42
+ }
43
+ }
44
+
45
+ } , [ product , dispatch , productId , successUpdate ] ) ;
46
+
47
+
48
+ const handleChange = ( event ) => {
49
+ setFormState ( formState => ( {
50
+ ...formState ,
51
+ values :{
52
+ ...formState . values ,
53
+ [ event . target . name ] :
54
+ event . target . type === 'checkbox'
55
+ ? event . target . checked
56
+ : event . target . value
57
+ }
58
+
59
+ } ) ) ;
60
+ }
61
+
62
+ const handleSubmit = ( e ) => {
63
+ e . preventDefault ( ) ;
64
+ setSubmitted ( true ) ;
65
+ const { title, description, image } = formState . values ;
66
+ if ( title && description && image ) {
67
+ dispatch ( updateProduct ( formState . values ) ) ;
68
+ setSubmitted ( false ) ;
69
+ }
70
+ }
71
+
72
+ return (
73
+ < >
74
+ < div className = "container-scroller" >
75
+ < Header />
76
+ < div className = "container-fluid page-body-wrapper" >
77
+ < Sidebar />
78
+ < div className = "main-panel" >
79
+ < div className = "content-wrapper" >
80
+ < div className = "row" >
81
+ < div className = "col-12 grid-margin" >
82
+ < div className = "card" >
83
+ < div className = "card-body" >
84
+ < h4 className = "card-title" > Edit Product</ h4 >
85
+ < form className = "form-sample" onSubmit = { handleSubmit } >
86
+ < p className = "card-description" >
87
+ </ p >
88
+ < div className = "row" >
89
+ < div className = "col-md-6" >
90
+ < div className = "form-group row" >
91
+ < label className = "col-sm-3 col-form-label" > Title</ label >
92
+ < div className = "col-sm-9" >
93
+ < input type = "text" className = { 'form-control form-control-lg' + ( submitted && ! formState . values . title ? ' is-invalid' : '' ) }
94
+ name = "title"
95
+ onChange = { handleChange }
96
+ value = { formState . values . title || '' }
97
+ />
98
+ { submitted && ! formState . values . title &&
99
+ < div className = "inline-errormsg" > Title is required</ div >
100
+ }
101
+ </ div >
102
+ </ div >
103
+ </ div >
104
+ </ div >
105
+ < div className = "row" >
106
+ < div className = "col-md-6" >
107
+ < div className = "form-group row" >
108
+ < label className = "col-sm-3 col-form-label" > Description</ label >
109
+ < div className = "col-sm-9" >
110
+ < textarea rows = { 5 } cols = { 5 } className = { 'form-control form-control-lg' + ( submitted && ! formState . values . title ? ' is-invalid' : '' ) }
111
+ name = "description"
112
+ onChange = { handleChange }
113
+ value = { formState . values . description || '' }
114
+ />
115
+ { submitted && ! formState . values . description &&
116
+ < div className = "inline-errormsg" > Description is required</ div >
117
+ }
118
+ </ div >
119
+ </ div >
120
+ </ div >
121
+ </ div >
122
+ < div className = "row" >
123
+ < div className = "col-md-6" >
124
+ < div className = "form-group row" >
125
+ < label className = "col-sm-3 col-form-label" > Image URL</ label >
126
+ < div className = "col-sm-9" >
127
+ < input type = "text" className = { 'form-control form-control-lg' + ( submitted && ! formState . values . image ? ' is-invalid' : '' ) }
128
+ name = "image"
129
+ onChange = { handleChange }
130
+ value = { formState . values . image || '' }
131
+ />
132
+ { submitted && ! formState . values . image &&
133
+ < div className = "inline-errormsg" > Image is required</ div >
134
+ }
135
+ </ div >
136
+ </ div >
137
+ </ div >
138
+ </ div >
139
+ < div className = "row" >
140
+ < div className = "col-md-6" >
141
+ < div className = "form-group row" >
142
+ < label className = "col-sm-3 col-form-label" > Size</ label >
143
+ < div className = "col-sm-9" >
144
+ < input type = "text" className = "form-control form-control-lg"
145
+ name = "size"
146
+ onChange = { handleChange }
147
+ value = { formState . values . size || '' }
148
+ />
149
+ </ div >
150
+ </ div >
151
+ </ div >
152
+ </ div >
153
+ < div className = "row" >
154
+ < div className = "col-md-6" >
155
+ < div className = "form-group row" >
156
+ < label className = "col-sm-3 col-form-label" > Color</ label >
157
+ < div className = "col-sm-9" >
158
+ < input type = "text" className = "form-control form-control-lg"
159
+ name = "color"
160
+ onChange = { handleChange }
161
+ value = { formState . values . color || '' }
162
+ />
163
+ </ div >
164
+ </ div >
165
+ </ div >
166
+ </ div >
167
+ < div className = "row" >
168
+ < div className = "col-md-6" >
169
+ < div className = "form-group row" >
170
+ < label className = "col-sm-3 col-form-label" > Price</ label >
171
+ < div className = "col-sm-9" >
172
+ < input type = "number" className = "form-control form-control-lg"
173
+ name = "price"
174
+ onChange = { handleChange }
175
+ value = { formState . values . price || '' }
176
+ />
177
+ </ div >
178
+ </ div >
179
+ </ div >
180
+ </ div >
181
+
182
+ < div className = "row" >
183
+ < div className = "col-md-6" >
184
+ < div className = "form-group row" >
185
+ < label className = "col-sm-3 col-form-label" > Category</ label >
186
+ < div className = "col-sm-9" >
187
+ < select className = "form-control" name = "category" multiple >
188
+ < option value = "man" > Man</ option >
189
+ < option value = "woman" > Woman</ option >
190
+ </ select >
191
+ </ div >
192
+ </ div >
193
+ </ div >
194
+ </ div >
195
+ < div className = "row" >
196
+ < div className = "col-md-6" >
197
+ < div className = "form-group row" >
198
+ < label className = "col-sm-3 col-form-label" > Stock</ label >
199
+ < div className = "col-sm-9" >
200
+ < input type = "text" className = { 'form-control form-control-lg' + ( submitted && ! formState . values . stock ? ' is-invalid' : '' ) }
201
+ name = "stock"
202
+ onChange = { handleChange }
203
+ value = { formState . values . stock || '' }
204
+ />
205
+ { submitted && ! formState . values . stock &&
206
+ < div className = "inline-errormsg" > Stock is required</ div >
207
+ }
208
+ </ div >
209
+ </ div >
210
+ </ div >
211
+ </ div >
212
+ < div className = "text-center" >
213
+ < button type = "submit" className = "btn btn-primary me-2" > Update</ button >
214
+ < Link to = "/products" > < button className = "btn btn-light" > Cancel</ button > </ Link >
215
+ </ div >
216
+ </ form >
217
+ </ div >
218
+ </ div >
219
+ </ div >
220
+ </ div >
221
+ </ div >
222
+ < Footer />
223
+ </ div >
224
+ </ div >
225
+ </ div >
226
+ </ >
227
+ )
228
+ }
229
+
230
+ export default EditProduct ;
0 commit comments