-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductAdd.jsx
231 lines (205 loc) · 8.87 KB
/
ProductAdd.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
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
import React, { useEffect, useState } from 'react';
import { Switch } from '@mantine/core';
import SelectOptions from '../components/SelectOptions';
import DeleteIcon from '../constants/icons/DeleteIcon';
import { getAllBrand } from '../services/brandService';
import { getAllCategories } from '../services/categoryService';
import { getAllColor } from '../services/colorService';
import { getAllUsingStatus } from '../services/usingStatusService';
import { Formik } from 'formik';
import { ProductAddShema } from '../constants/yupSchemas/ProudctAddSchema';
import { addProduct } from '../services/productService';
import ImageDragDrop from '../components/ImageDragDrop';
import { useAuth } from '../context/AuthProviderContext';
import useDisplaySuccessMess from '../hooks/useDisplaySuccessMes';
import useDisplayErrorMess from '../hooks/useDisplayErrorMess';
import LoadingCircleIcon from '../constants/icons/LoadingCircleIcons';
function ProductAdd() {
const { auth } = useAuth();
const [image, setImage] = useState([]);
const [brands, setBrands] = useState([]);
const [categories, setCategories] = useState([]);
const [colors, setColors] = useState([]);
const [usingStatus, setUsingStatus] = useState([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
getAllBrands();
getAllCategory();
getAllColors();
getAllUsingStatuses();
}, []);
const getAllBrands = async () => {
const res = await getAllBrand();
setBrands(res.data);
};
const getAllCategory = async () => {
const res = await getAllCategories();
setCategories(res.data);
};
const getAllColors = async () => {
const res = await getAllColor();
setColors(res.data);
};
const getAllUsingStatuses = async () => {
const res = await getAllUsingStatus();
setUsingStatus(res.data);
};
const handleFormSubmit = async (formikData, resetForm, setFieldValue) => {
setLoading(true);
let category = categories.find(item => item.name == formikData.category);
const formData = new FormData();
formData.append('files.image', image.file);
formData.append('data', JSON.stringify({
name: formikData.title,
description: formikData.description,
category: category.id,
brand: formikData.brand,
color: formikData.color,
status: formikData.usingStatus,
price: formikData.price,
isOfferable: formikData.offerOption,
users_permissions_user: auth.id
}));
const product = await addProduct(formData);
console.log(product);
if (product.status == 200) {
useDisplaySuccessMess('Ürün eklendi.');
}
else {
useDisplayErrorMess('Ürün ekleme başarısız.');
}
setLoading(false);
//resetin all formik data
resetForm();
setFieldValue('category', '');
setFieldValue('brand', '');
setFieldValue('category', '');
setFieldValue('color', '');
setFieldValue('usingStatus', '');
setFieldValue('offerOption', false);
setFieldValue('image', '');
setImage('');
};
return (
<div className='productAdd'>
<Formik
initialValues={{
title: '',
description: '',
category: '',
brand: '',
color: '',
usingStatus: '',
price: '',
offerOption: false,
image: '',
}}
validationSchema={ProductAddShema}
onSubmit={(auth, { resetForm, setFieldValue }) => {
handleFormSubmit(auth, resetForm, setFieldValue);
}}
>
{
({ values, errors, handleChange, handleSubmit, setFieldValue }) =>
<form className='containerForm' >
<div className="row-1">
<div className="productDetails">
<h1>Ürün Detayları</h1>
<div className='productDetailContainer' >
<div className='productName'>
<label>Ürün Adı</label>
<input className={`inputProductName ${errors.title ? 'errorInput' : ''}`} type="text" name='title' value={values.title} onChange={handleChange} placeholder='Örnek: Iphone 12 Pro Max' />
{errors.title && <span className='errorText'>{errors.title}</span>}
</div>
<div className='productDescription'>
<label>Açıklama</label>
<textarea className={`${errors.description ? 'errorInput' : ''}`} type="text" name='description' value={values.description} onChange={handleChange} placeholder='Ürün açıklaması girin' />
{errors.description && <span className='errorText'>{errors.description}</span>}
</div>
<div className="productProperties">
<div className='row'>
<div className='productCategory'>
<label htmlFor="">Kategori</label>
<SelectOptions
name='category' value={values.category} setFieldValue={setFieldValue}
error={errors.category}
data={categories.map(item => item.name)}
/>
{errors.category && <span className='errorText'>{errors.category}</span>}
</div>
<div className='productBrand'>
<label htmlFor="">Marka</label>
<SelectOptions
name='brand' value={values.brand} setFieldValue={setFieldValue}
error={errors.brand}
data={brands.map(item => item.name)} />
{errors.brand && <span className='errorText'>{errors.brand}</span>}
</div>
</div>
<div className='row'>
<div className='productColor'>
<label htmlFor="">Renk</label>
<SelectOptions
name='color' value={values.color} setFieldValue={setFieldValue}
error={errors.color}
data={colors.map(item => item.name)} />
{errors.color && <span className='errorText'>{errors.color}</span>}
</div>
<div className='productUsingStatus'>
<label htmlFor="">Kullanım Durumu</label>
<SelectOptions
name='usingStatus' value={values.usingStatus} setFieldValue={setFieldValue}
error={errors.usingStatus}
data={usingStatus.map(item => item.name)} />
{errors.usingStatus && <span className='errorText'>{errors.usingStatus}</span>}
</div>
</div>
</div>
<div className='productPrice'>
<label htmlFor="">Fiyat</label>
<div className={`${errors.price ? 'errorInput' : ''}`}>
<input type="number" name='price' value={values.price} onChange={handleChange} />
<span>TL</span>
</div>
{errors.price && <span className='errorText'>{errors.price}</span>}
</div>
<div className='productOfferOption'>
<label htmlFor="">Teklif Opsiyonu</label>
<Switch checked={values.offerOption} onChange={e => setFieldValue('offerOption', e.currentTarget.checked)} size="md" />
</div>
</div>
</div>
<div className="productImage">
<h1>Ürün Görseli</h1>
{
image.length === 0 &&
<>
<ImageDragDrop setFieldValue={setFieldValue} setImage={setImage} />
{errors.image && <span className='errorText'>{errors.image}</span>}
</>
}
{
image?.preview &&
(
<div className='image'>
<button onClick={() => { setImage([]); setFieldValue('image', ''); }}> <DeleteIcon /> </button>
<img src={image?.preview} alt="" />
</div>
)
}
</div>
</div>
<div className="row-2">
<button type='submit' className='submitButton' disabled={loading && true} onClick={handleSubmit}>
{
loading ? <LoadingCircleIcon size={30} color='white' /> : 'Kaydet'
}
</button>
</div>
</form>
}
</Formik>
</div >
);
}
export default ProductAdd;