Advertisement
Fahim_7861

Untitled

May 31st, 2021
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. import React from 'react';
  2. import { useEffect } from 'react';
  3. import { useState } from 'react';
  4. import { useDispatch, useSelector } from 'react-redux';
  5. import { isLoading } from '../../../../constants/isLoading';
  6. import { categoryActions } from '../../../../redux/actions/categoryActions';
  7. import Sulov from '../../../../Sulov';
  8. const axios = require('axios');
  9. const ProductAdd = () => {
  10.  
  11. const [info, setInfo] = useState({});
  12. const [file, setFile] = useState([]);
  13.  
  14.  
  15.  
  16. const dispatch = useDispatch( );
  17.  
  18. const initData = useSelector(state => state.category);
  19.  
  20.  
  21. const {category} = initData;
  22.  
  23. console.log(initData)
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. useEffect(() => {
  32.  
  33. dispatch(categoryActions());
  34.  
  35.  
  36.  
  37. }, []);
  38.  
  39.  
  40. const handleFileChange = (e) => {
  41. const newFile = e.target.files;
  42.  
  43.  
  44.  
  45. setFile(newFile);
  46.  
  47. console.log(newFile)
  48.  
  49. }
  50.  
  51.  
  52.  
  53. const handleBlur = e => {
  54.  
  55. const newInfo = { ...info };
  56. newInfo[e.target.name] = e.target.value;
  57. setInfo(newInfo);
  58. }
  59.  
  60. const handleSubmit = (e) => {
  61. e.preventDefault();
  62.  
  63. const formData = new FormData()
  64. // formData.append('file', file)
  65.  
  66. const newFile = Object.values(file);
  67.  
  68.  
  69. newFile.forEach(f=>
  70. {
  71. formData.append('file',f);
  72. })
  73.  
  74. formData.append('status', info.status);
  75.  
  76. formData.append('star', info.star);
  77.  
  78. formData.append('itemCount', info.itemCount);
  79.  
  80. formData.append('title', info.title);
  81. formData.append('price', info.price);
  82.  
  83. formData.append('categoryTitle',info.categoryTitle);
  84. formData.append('categoryId',info.categoryId);
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. fetch('http://localhost:5000/product/add', {
  95. method: 'POST',
  96. body: formData,
  97.  
  98. })
  99. .then(response => response.json())
  100. .then(data => {
  101. console.log(data)
  102.  
  103.  
  104. })
  105. .catch(error => {
  106. console.error(error)
  107. })
  108.  
  109.  
  110. }
  111.  
  112.  
  113. const onChangeValue = e => {
  114.  
  115. const data = category.filter(ct=>ct._id==e.target.value)[0];
  116.  
  117.  
  118. const newInfo = { ...info };
  119.  
  120. newInfo.categoryId = data._id;
  121. newInfo.categoryTitle = data.title;
  122.  
  123.  
  124.  
  125. setInfo(newInfo);
  126.  
  127.  
  128.  
  129.  
  130.  
  131. }
  132.  
  133. const onChangeStatus=(e)=>
  134. {
  135. const newInfo = { ...info };
  136.  
  137. newInfo.status = e.target.value;
  138.  
  139. setInfo(newInfo);
  140.  
  141.  
  142.  
  143. }
  144.  
  145.  
  146. return (
  147.  
  148. !isLoading(category)?<div>loading</div>:
  149. <div className='container'>
  150.  
  151. <form onSubmit={handleSubmit}>
  152.  
  153.  
  154.  
  155. <label htmlFor="exampleInputName1"><b>Title</b></label>
  156. <input onBlur={handleBlur} type="text" className="form-control" name='title'
  157. id="inputTitle" placeholder="title" required />
  158.  
  159. <label htmlFor="exampleInputName2"><b>Status</b></label>
  160.  
  161.  
  162.  
  163. <label htmlFor="exampleInputName3"><b>Star</b></label>
  164. <input onBlur={handleBlur} type="number" className="form-control" name='star'
  165. id="inputStar" placeholder="star" required />
  166.  
  167. <label htmlFor="exampleInputName4"><b>Price</b></label>
  168. <input onBlur={handleBlur} type="number" className="form-control" name='price'
  169. id="inputPrice" placeholder="price" required />
  170.  
  171. <label htmlFor="exampleInputName5"><b>Itme Count</b></label>
  172. <input onBlur={handleBlur} type="number" className="form-control" name='itemCount'
  173. id="inputItemCount" placeholder="itemCount" required />
  174.  
  175. <label htmlFor="exampleInputName5"><b>image</b></label>
  176. <input className="form-control" onChange={handleFileChange} type='file' multiple></input>
  177.  
  178.  
  179. <div >
  180.  
  181. {
  182.  
  183. category.map(ct=><div key={ct._id}><label for={ct.title}> <input onChange={onChangeValue} type="radio" value={ct._id} name="category" required/>
  184. <span style={{ marginLeft: '10px' }}>{ct.title}</span></label><br />
  185. </div>)
  186.  
  187. }
  188.  
  189.  
  190. </div>
  191.  
  192.  
  193. <label> <input onChange={onChangeStatus} type="radio" value='active' name="status" required/>
  194. <span >Active</span></label>
  195. <label> <input onChange={onChangeStatus} type="radio" value='inactive' name="status" required/>
  196. <span >Inactive</span></label>
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. <br />
  210.  
  211.  
  212.  
  213.  
  214. <input type="submit" value="Submit" />
  215.  
  216.  
  217.  
  218. </form>
  219.  
  220. </div>
  221. );
  222. };
  223.  
  224. export default ProductAdd;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement