Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import { Field, reduxForm } from 'redux-form'
  3.  
  4. import { StepOne, StepTwo, StepThree, StepFour } from '../steps'
  5.  
  6.  
  7. export const steps =
  8.     [
  9.       {name: 'Data', component: <StepOne/>},
  10.       {name: 'Project', component: <StepTwo/>},
  11.       {name: 'Scope', component: <StepThree/>},
  12.       {name: 'Success', component: <StepFour/>}
  13.     ]
  14.  
  15. export class MultiStep extends React.Component {
  16.  
  17.  
  18.  
  19.           constructor(props) {
  20.             super(props);
  21.             this.state = {
  22.               showNextBtn: 'showNextBtn' + ' hero_button',
  23.               compState: 0,
  24.               navState: this.getNavStates(0, this.props.steps.length)
  25.             };
  26.  
  27.             this.handleOnClick = this.handleOnClick.bind(this);
  28.             this.handleKeyDown = this.handleKeyDown.bind(this);
  29.             this.next = this.next.bind(this);
  30.           }
  31.  
  32.           getNavStates(indx, length) {
  33.             let styles = [];
  34.             for (let i=0; i<length; i++) {
  35.               if(i < indx) {
  36.                 styles.push('done')
  37.               }
  38.               else if(i === indx) {
  39.                 styles.push('doing')
  40.               }
  41.               else {
  42.                 styles.push('todo')
  43.               }
  44.             }
  45.             return { current: indx, styles: styles }
  46.           }
  47.           checkNavState(currentStep){
  48.  
  49.             if(currentStep > 0 && currentStep < this.props.steps.length - 1){
  50.               this.setState({
  51.               })
  52.             }
  53.             else if(currentStep === 0) {
  54.               this.setState({
  55.                 showNextBtn: true
  56.               })
  57.             }
  58.             else if (this.state.compState = 3) {
  59.                 this.setState(
  60.                     {showNextBtn : 'showNextBtn ' + ' hide'}
  61.               )
  62.             }
  63.  
  64.           }
  65.           setNavState(next) {
  66.  
  67.  
  68.  
  69.               this.setState({navState: this.getNavStates(next, this.props.steps.length)})
  70.               if (next < this.props.steps.length) {
  71.                 this.setState({compState: next})
  72.               }
  73.  
  74.               this.checkNavState(next);
  75.             }
  76.  
  77.  
  78.  
  79.           handleKeyDown(evt) {
  80.             if (evt.which === 13) {
  81.               <Component onSubmit={this.next}/>
  82.             }
  83.           }
  84.           handleOnClick(evt) {
  85.             if (evt.currentTarget.value === (this.props.steps.length - 1) &&
  86.               this.state.compState === (this.props.steps.length - 1)) {
  87.               this.setNavState(this.props.steps.length)
  88.             }
  89.             else {
  90.               this.setNavState(evt.currentTarget.value)
  91.             }
  92.           }
  93.           next() {
  94.             this.setNavState(this.state.compState + 1)
  95.           }
  96.  
  97.  
  98.           getClassName(className, i){
  99.             return className + "-" + this.state.navState.styles[i];
  100.           }
  101.           renderSteps() {
  102.  
  103.               return this.props.steps.map((s, i)=> (
  104.  
  105.                 <li className={this.getClassName("progtrckr", i)} onClick={this.handleOnClick} key={i} value={i}>
  106.                   <em>{i+1}</em>
  107.                   <span>{this.props.steps[i].name}</span>
  108.                 </li>
  109.               ));
  110.             }
  111.           render() {
  112.  
  113.               const Component = this.props.steps[this.state.compState].component.type;
  114.  
  115.             return (
  116.               <div className="container" onKeyDown={this.handleKeyDown}>
  117.  
  118.                 <ol className="progtrckr">
  119.                   {this.renderSteps()}
  120.                 </ol>
  121.                 <Component onSubmit={this.next}/>
  122.               </div>
  123.             );
  124.           }
  125.         }
  126.  
  127.  
  128. export var submit =(values) =>{
  129.  
  130.           let isError =false;
  131.  
  132.           if (isError) {
  133.               throw new SumissionError(error);
  134.           } else{
  135.             return submitToServer(values)
  136.               .then(data =>{
  137.                   if (data.errors)  {
  138.                     console.log(data.errors);
  139.                       throw new SubmissionError(data.errors);
  140.                   } else{
  141.                       console.log(values)
  142.                       console.log('server added data to database');
  143.                   }
  144.               });
  145.           }
  146.           }
  147.  
  148.  
  149.  
  150. export async function submitToServer(data){
  151.  
  152.   try{
  153.       let response = await fetch('http://localhost:5000/stepone', {
  154.           method: 'POST',
  155.           headers: {
  156.             'Content-type' :  'application/json',
  157.           },
  158.           body: JSON.stringify(data),
  159.         });
  160.         let responseJson = await response.json();
  161.         return responseJson;
  162.   }   catch (error) {
  163.           console.error(error);
  164.   }
  165. }
  166. const validate = values => {
  167.  
  168.     const errors = {}
  169.  
  170.     if (!values.username ) {
  171.     errors.username =<div className ="error"> 'Required'</div>
  172.   } else if (values.username.length > 7) {
  173.     errors.username = <div className ="error">'Should be 7 characters or less'</div>
  174.   }
  175.  
  176.     if (!values.email) {
  177.       errors.email = <div className= "error">You should write your mail</div>
  178.     } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) {
  179.       errors.email = <div className ="error">Lets write in perfect scheme</div>
  180.     }
  181.     return errors
  182.   }
  183.  
  184.   const warn = values => {
  185.     const warnings = {}
  186.     return warnings
  187.   }
  188.  
  189.  
  190.  
  191.   const renderFieldOne = ({ input, label, meta: { touched, error, warning }, custom, ...inputProps }) => (
  192.     <div>
  193.       <div className ="group">
  194.         <input className="text" {...input} {...inputProps} />
  195.         <label>{label}</label>
  196.         <span className="highlight"></span>
  197.         <span className="bar"></span>
  198.         {touched && ((error && <span>{error}</span>) || (warning && <span>{warning}</span>))}
  199.       </div>
  200.     </div>
  201.   )
  202.  
  203. let StepOneFormValidation = (props) => {
  204.   const { handleSubmit, submitting, } = props
  205.  
  206.  
  207.   return (
  208.  
  209.  
  210.         <form onSubmit={()=>{handleSubmit(); handleSubmit(submit) }}>
  211.  
  212.       <Field name="name"
  213.         type="text"
  214.         component={renderFieldOne}
  215.         label="Username"
  216.           />
  217.  
  218.       <Field name="email"
  219.        type="email"
  220.         component={renderFieldOne}
  221.         label="Email"
  222.          />
  223.  
  224.       <div>
  225.         <button
  226.         className ="hero_button"
  227.          type="submit"
  228.           disabled={submitting}>
  229.           Submit</button>
  230.       </div>
  231.  
  232.     </form>
  233.   )
  234. }
  235.  
  236.  
  237. export default reduxForm({
  238.   form: 'syncValidation',           // a unique identifier for this form
  239.   validate,                                  // <--- validation function given to redux-form
  240.   warn                                        // <--- warning function given to redux-form
  241. })(StepOneFormValidation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement