Advertisement
Venelin

step2 final

Dec 7th, 2020
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useState, useEffect} from 'react'
  2. import { useForm } from "react-hook-form";
  3. import { useData, useProduct, productsState } from '../../utils/DataProvider'
  4. import Display from './Display';
  5. import _ from 'lodash';
  6.  
  7. const Step2 = (props) => {
  8.   const { allProducts } = props;
  9.   const [state, setState] = useData()
  10.   const [products, setProducts] = useProduct();
  11.  
  12.   const currentProductSelection = products[state.activeProductIndex];
  13.   const currentProduct = !!currentProductSelection.productName ?
  14.     _.find(allProducts, {"Product": currentProductSelection.productName}) :
  15.     allProducts[0];
  16.  
  17.   const types = ["Indoor", "Outdoor"];
  18.   const [productType, setProductType] = useState(currentProduct["Indoor / Outdoor"]);
  19.  
  20.   const {handleSubmit, register } = useForm();
  21.  
  22.   const onSubmit = data => {
  23.     handleFormControlChange(false, 'productName', data.productName)
  24.     console.log(data);
  25.     setState(
  26.       state => ({
  27.         ...state,
  28.         step: 3
  29.       })
  30.     );
  31.   }
  32.  
  33.   const handleFormControlChange = (e, key, value) => {
  34.     setProducts(products => {
  35.       let activeProduct = products[state.activeProductIndex];
  36.       Object.assign( activeProduct, productsState);
  37.       activeProduct[key] = value;
  38.       return [...products];
  39.     });
  40.   }
  41.  
  42.   return (
  43.     <form className="form-root" onSubmit={handleSubmit(onSubmit)}>
  44.       <div className="form form--step2">
  45.  
  46.         <Display data={currentProduct.image} />
  47.         <div className="form__item">
  48.           <label htmlFor="type" className="form__label">Type</label>
  49.           <select
  50.               className="ui-select"
  51.               name="type"
  52.               ref={register}
  53.               defaultValue={productType.toLowerCase()}
  54.               onChange={(e) => {
  55.                 setProductType(e.target.value);
  56.                 if (!currentProduct["Product"]) {
  57.                   handleFormControlChange(e, "productName", allProducts.filter( p => p["Indoor / Outdoor"].toLowerCase() === productType.toLowerCase())[0]["Product"])
  58.                 }
  59.                 console.log('product type should set to:', e.target.value);
  60.               }}
  61.               >
  62.             {types.map(value => (
  63.               <option key={value} value={value.toLowerCase()}>
  64.                 {value}
  65.               </option>
  66.             ))}
  67.           </select>
  68.         </div>
  69.         <div className="form__item">
  70.           <label htmlFor="productName" className="form__label">Product</label>
  71.           <select
  72.               className="ui-select"
  73.               name="productName"
  74.               ref={register}
  75.               defaultValue={currentProduct["Product"]}
  76.               onChange={(e) => {
  77.                 handleFormControlChange(e, 'productName', e.target.value);
  78.               }}
  79.             >
  80.             {allProducts.filter( p => p["Indoor / Outdoor"].toLowerCase() === productType.toLowerCase() ).map(item => (
  81.               <option selected={currentProduct["Product"] === item.Product} key={item.ID} value={item.name} id={item.id}>
  82.                 {item.Product}
  83.               </option>
  84.             ))}
  85.           </select>
  86.         </div>
  87.         <div className="form__item">
  88.           <div className="desc-box">
  89.             <span className="desc-box__label">Description</span>
  90.             <span className="desc-box__content">{currentProduct["Description"]}</span>
  91.           </div>
  92.         </div>
  93.       </div>
  94.       <div className="button-nav">
  95.         <div>
  96.         <button className="btn" onClick={() => setState(state => ({...state,step: 1}))}>Previous</button>
  97.         </div>
  98.         <div>
  99.           <button className="btn btn--active" type="submit">Next</button>
  100.         </div>
  101.       </div>
  102.     </form>
  103.   )
  104. }
  105. export default Step2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement