Advertisement
ahmadandika

Daftar.jsx

Aug 18th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable no-restricted-syntax */
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { Field, reduxForm } from 'redux-form';
  5. import { registerUser } from '../../actions/auth.action';
  6. import {
  7.   renderField,
  8.   emailValid,
  9.   required,
  10.   renderInputGroup
  11. } from '../../components/Field';
  12. import PasswordField from '../../components/PasswordField';
  13. import ButtonLoader from '../../components/ButtonLoader';
  14.  
  15. const validate = values => {
  16.   const errors = {};
  17.   if (values.password) {
  18.     if (!values.password_confirmation) {
  19.       errors.password_confirmation = 'Required';
  20.     } else if (values.password_confirmation !== values.password) {
  21.       errors.password_confirmation = 'Password mismatched';
  22.     }
  23.   }
  24.  
  25.   return errors;
  26. };
  27.  
  28. class Daftar extends Component {
  29.   state = {};
  30.  
  31.   onSubmit = value => {
  32.     const formData = new FormData();
  33.     const formKey = [];
  34.     const formPhone = ['phone'];
  35.     for (const key in value) {
  36.       if (formKey.includes(key)) {
  37.         formData.append(key, value[key].value);
  38.       } else if (formPhone.includes(key)) {
  39.         formData.append(key, `+62${value[key]}`);
  40.       } else {
  41.         formData.append(key, value[key]);
  42.       }
  43.     }
  44.     formData.append('password_confirmation', value.password);
  45.  
  46.     const { registerUser } = this.props;
  47.    
  48.     registerUser(formData);
  49.   };
  50.  
  51.   render() {
  52.     const { handleSubmit, auth } = this.props;
  53.     return (
  54.       <div className="container-fluid register-container">
  55.         <div className="height-top-container" />
  56.         <div className="row">
  57.           <div className="col-12 d-flex flex-column align-items-center">
  58.             <h2>Daftar akun</h2>
  59.             <h5 className="text-center">
  60.               Ayo daftarkan akunmu untuk melihat produk keuangan dengan lebih
  61.               lengkap
  62.             </h5>
  63.             <form onSubmit={handleSubmit(this.onSubmit)}>
  64.               <Field
  65.                 name="name"
  66.                 type="text"
  67.                 component={renderField}
  68.                 placeholder="Nama"
  69.                 validate={[required]}
  70.               />
  71.               <Field
  72.                 name="email"
  73.                 type="email"
  74.                 component={renderField}
  75.                 placeholder="Email"
  76.                 validate={[required, emailValid]}
  77.               />
  78.               <Field
  79.                 name="phone"
  80.                 type="number"
  81.                 prefix="+62"
  82.                 component={renderInputGroup}
  83.                 placeholder="No. HP"
  84.                 validate={[required]}
  85.               />
  86.               <Field
  87.                 name="password"
  88.                 type="password"
  89.                 component={PasswordField}
  90.                 placeholder="Password"
  91.                 validate={[required]}
  92.               />
  93.               <Field
  94.                 name="referalCode"
  95.                 type="number"
  96.                 component={renderField}
  97.                 placeholder="Kode Referal"
  98.               />
  99.               <div className="d-flex justify-content-center align-items-center mt-3 mb-3">
  100.                 <ButtonLoader
  101.                   type="submit"
  102.                   className="button-login-akun"
  103.                   loader={auth.isLoading}
  104.                 >
  105.                   DAFTAR
  106.                 </ButtonLoader>
  107.               </div>
  108.               {auth.errorMessage && (
  109.                 <div className="col-lg-12 mt-2 text-center">
  110.                   <div className="alert alert-danger" role="alert">
  111.                     {auth.errorMessage}
  112.                   </div>
  113.                 </div>
  114.               )}
  115.             </form>
  116.           </div>
  117.         </div>
  118.       </div>
  119.     );
  120.   }
  121. }
  122.  
  123. const mapStateToProps = state => ({
  124.   auth: state.auth
  125. });
  126.  
  127. export default reduxForm({
  128.   form: 'registerForm', // a unique identifier for this form
  129.   validate
  130. })(
  131.   connect(
  132.     mapStateToProps,
  133.     { registerUser }
  134.   )(Daftar)
  135. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement