-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModForm.js
369 lines (347 loc) · 11.5 KB
/
ModForm.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import React, { useState, useEffect } from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import * as actions from '../../actions';
import firebase from 'firebase/app';
import '../../firebase/config';
import { imagesRef } from '../../firebase/config';
import { v4 as uuidv4 } from 'uuid';
import { db } from '../../firebase/config';
import Dropdown from '../dropdown/Dropdown';
import NewBrand from './NewBrand';
const ModForm = props => {
useEffect(() => {
if (props.brands.length === 0) {
props.fetchBrands();
}
if (props.cars.length === 0) {
props.fetchCars();
}
}, []);
const [showErrMsg, setShowErrMsg] = useState(false);
const [imgError, setImgError] = useState(null);
const [linkErr, setlinkErr] = useState(null);
const [brandErr, setBrandErr] = useState(null);
const [modelErr, setModelErr] = useState(null);
const [yearErr, setYearErr] = useState(null);
const [transmissionErr, setTransmissionErr] = useState(null);
const [classErr, setClassErr] = useState(null);
const [newBrandLogoErr, setnewBrandLogoErr] = useState(null);
const [newBrandNameErr, setNewBrandNameErr] = useState(null);
const [modUrlWarning, setModUrlWarning] = useState(null);
const [imgFileName, setImgFileName] = useState(null);
const imgTypes = ['image/png', 'image/jpeg'];
// Validate form
useEffect(() => {
// Car image
props.form.imgURL === null
? setImgError('Please upload an image of the car')
: setImgError(null);
// Mod URL
props.form.modURL.length === 0
? setlinkErr('Please enter a link to the mod')
: setlinkErr(null);
props.cars.forEach(car => {
if (car.modURL === props.form.modURL) {
setModUrlWarning('Warning: There is already a mod with this link');
}
});
// Brand Dropdown
props.form.brand === null ? setBrandErr('Please select a brand') : setBrandErr(null);
// Car model
props.form.model.length === 0 ? setModelErr('Please enter the car model') : setModelErr(null);
// Year
props.form.year.length === 0
? setYearErr('Please enter the year of the car')
: setYearErr(null);
// Transmission Dropdown
props.form.transmission === null
? setTransmissionErr('Please select a transmission')
: setTransmissionErr(null);
// Car class
props.form.carClass === null ? setClassErr('Please select a car class') : setClassErr(null);
// Add a new brand - image
props.newBrandForm.imgURL === null && props.form.brand === 'New Brand'
? setnewBrandLogoErr('Please upload an image of the brand logo')
: setnewBrandLogoErr(null);
// Add a new brand - image
props.newBrandForm.brandName.length === 0 && props.form.brand === 'New Brand'
? setNewBrandNameErr('Please enter the brand name')
: setNewBrandNameErr(null);
}, [props.form, props.newBrandForm]);
// Uploads file to firebase when user drops file
const changeHandler = e => {
let selected = e.target.files[0];
setImgFileName(selected.name);
const imgId = uuidv4();
if (selected && imgTypes.includes(selected.type)) {
const ref = imagesRef.child(imgId);
const uploadTask = ref.put(selected);
uploadTask.on(
firebase.storage.TaskEvent.STATE_CHANGED,
snapshot => {
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED:
break;
case firebase.storage.TaskState.RUNNING:
break;
}
},
error => {
switch (error.code) {
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;
case 'storage/unknown':
// Unknown error occurred, inspect error.serverResponse
break;
}
},
() => {
// Upload completed successfully, now we can get the download URL
uploadTask.snapshot.ref.getDownloadURL().then(downloadURL => {
props.setFormImageURL(downloadURL);
});
}
);
setImgError(null);
} else {
props.setFormImageURL(null);
setImgError('Please select an image file (png or jpeg)');
}
};
const renderBrandOptions = () => {
return props.brands
.sort((a, b) => {
if (a.brandName > b.brandName) {
return 1;
} else if (a.brandName < b.brandName) {
return -1;
} else {
return 0;
}
})
.map(brand => {
return (
<div
key={brand.id}
className={`item ${props.form.brand === brand.brandName ? 'active selected' : ''}`}
data-value={brand.brandName}
onClick={() => props.setFormBrand(brand.brandName)}
children={brand.brandName}
/>
);
});
};
const renderTransmissionOptions = () => {
const shifterOptions = ['Manual', 'Paddle Shifter', 'Sequential'];
return shifterOptions.map(value => {
return (
<div
key={value}
className={`item ${props.form.transmission === value ? 'active selected' : ''}`}
data-value={value}
onClick={() => props.setFormTransmission(value)}
children={value}
/>
);
});
};
const renderClassOptions = () => {
const classOptions = ['Road', 'Race', 'Prototype', 'Open Wheel'];
return classOptions.map(className => {
return (
<div
key={className}
className={`item ${props.form.carClass === className ? 'active selected' : ''}`}
data-value={className}
onClick={() => props.setFormCarClass(className)}
children={className}
/>
);
});
};
const handleSubmit = e => {
e.preventDefault();
if (
linkErr ||
brandErr ||
imgError ||
modelErr ||
yearErr ||
transmissionErr ||
classErr ||
newBrandLogoErr ||
newBrandNameErr
) {
setShowErrMsg(true);
} else {
props.setFormCreatedBy(props.userId);
props.setFormDateAdded(new Date().toJSON());
props.setFormShowReview(true);
if (props.form.brand === 'New Brand') {
const brandId = uuidv4();
db.ref('brands/' + brandId).set({
id: brandId,
logoURl: props.newBrandForm.imgURL,
brandName: props.newBrandForm.brandName
});
props.setFormBrand(props.newBrandForm.brandName);
}
}
};
return (
<div className='container'>
<form className='form mod-form' onSubmit={handleSubmit} id='add-mod-form'>
<div className='fields'>
<div className='field'>
<label>Mod Image</label>
<label className='input-file btn-secondary' htmlFor='modImage'>
<div>
Upload Image
<img src='/img/icons/upload.svg' alt='Upload' />
</div>
</label>
<label htmlFor='files' id='files-label'>
{imgFileName ? imgFileName : 'Drag and drop image here'}
<input
type='file'
name='files'
id='files'
multiple='multiple'
onChange={changeHandler}
/>
</label>
<input type='file' name='modImage' id='modImage' onChange={changeHandler} />
{showErrMsg && imgError && <div className='negative message'>{imgError}</div>}
{props.form.imgURL && (
<div className='preview-img'>
<img src={props.form.imgURL} />
</div>
)}
</div>
</div>
<div className='field'>
<label>Mod Link</label>
<input
type='text'
placeholder='Enter mod link'
value={props.form.modURL}
onChange={e => props.setFormModLink(e.target.value)}
/>
{showErrMsg && linkErr && (
<div className='negative message'>
<p>{linkErr}</p>
</div>
)}
{modUrlWarning && <div className='yellow message'>{modUrlWarning}</div>}
</div>
<div className='field'>
<label>Brand</label>
<p className='info'>
* If the brand is not in the dropdown, select the "New Brand" option to add it to the
database.
</p>
<Dropdown inputName='Select a brand' selectedValue={props.form.brand}>
<div
className={`item ${props.form.brand === 'New Brand' ? 'active selected' : ''}`}
// className={`item`}
data-value='New Brand'
onClick={() => props.setFormBrand('New Brand')}
children={'New Brand'}
/>
{renderBrandOptions()}
</Dropdown>
{showErrMsg && brandErr && (
<div className='negative message'>
<p>{brandErr}</p>
</div>
)}
</div>
{props.form.brand === 'New Brand' && (
<NewBrand
showErrMsg={showErrMsg}
newBrandLogoErr={newBrandLogoErr}
newBrandNameErr={newBrandNameErr}
/>
)}
<div className='field'>
<label>Car Model</label>
<input
type='text'
placeholder='Enter Car Model Name'
value={props.form.model}
onChange={e => props.setFormModel(e.target.value)}
/>
{showErrMsg && modelErr && (
<div className='negative message'>
<p>{modelErr}</p>
</div>
)}
</div>
<div className='field'>
<label>Year</label>
<input
type='number'
placeholder='Enter Car Year'
value={props.form.year}
onChange={e => props.setFormYear(Number(e.target.value))}
/>
{showErrMsg && yearErr && (
<div className='negative message'>
<p>{yearErr}</p>
</div>
)}
</div>
<div className='field'>
<label>Transmission</label>
<Dropdown
inputName='Select transmission type'
selectedValue={props.form.transmission}
children={renderTransmissionOptions()}
/>
{showErrMsg && transmissionErr && (
<div className='negative message'>
<p>{transmissionErr}</p>
</div>
)}
</div>
<div className='field'>
<label>Car Class</label>
<Dropdown
inputName='Select car class'
selectedValue={props.form.carClass}
children={renderClassOptions()}
/>
{showErrMsg && classErr && (
<div className='negative message'>
<p>{classErr}</p>
</div>
)}
</div>
<div className='buttons-container'>
<Link to='/newmod' className='btn-negative' onClick={props.clearForm}>
Cancel
</Link>
<button type='submit' className='btn-primary'>
Next
<i className='arrow alternate circle right outline icon' style={{ opacity: '1' }}></i>
</button>
</div>
</form>
</div>
);
};
const mapStateToProps = state => {
return {
brands: Object.values(state.brands),
cars: Object.values(state.cars),
form: state.form,
userId: state.oAuth.userId,
newBrandForm: state.newBrandForm
};
};
export default connect(mapStateToProps, actions)(ModForm);