Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // FORM TAST
  2. // ######### Imports ####################
  3. import React, { useState } from 'react';
  4. import styled from 'styled-components';
  5. import { find, get, filter, map } from 'lodash';
  6. import Api from '../../core/api';
  7.  
  8.  
  9. // ######### Components ####################
  10.  
  11. const Input = (props) => {
  12.   const [state, setState] = useState({
  13.     showContent: false,
  14.   });
  15.  
  16.   let { type, ...rest } = props;
  17.  
  18.   if (type === 'password' && state.showContent) {
  19.     type = 'text';
  20.   }
  21.  
  22.   return (
  23.     <div>
  24.       <input {...rest} type={type} />
  25.       {props.type === 'password' && <input type="checkbox" onChange={() => setState({ showContent: !state.showContent })} />}
  26.     </div>
  27.   );
  28. };
  29. // ######### Composing view ####################
  30.  
  31. const superDatabase = [
  32.  
  33. ];
  34.  
  35. class App extends React.Component {
  36.   constructor(props) {
  37.     super(props);
  38.  
  39.     this.state = {
  40.       password: '',
  41.       username: '',
  42.     };
  43.  
  44.     this.onSubmit = this.onSubmit.bind(this);
  45.     this.onChangePassword = this.onChangePassword.bind(this);
  46.     this.onChangeUsername = this.onChangeUsername.bind(this);
  47.   }
  48.  
  49.   onChangePassword(e) {
  50.     this.setState({
  51.       password: e.target.value,
  52.     });
  53.   }
  54.  
  55.   onChangeUsername(e) {
  56.     this.setState({
  57.       username: e.target.value,
  58.     });
  59.   }
  60.  
  61.   onSubmit(e) {
  62.     e.preventDefault();
  63.  
  64.     let correctPassword = false;
  65.     const regW = new RegExp(/\w/);
  66.     const regD = new RegExp(/\d/);
  67.  
  68.     if (this.state.password.length >= 8 && regW.test(this.state.password) && regD.test(this.state.password)) {
  69.       correctPassword = true;
  70.     } else {
  71.       alert('wrong password. cancelled');
  72.     }
  73.  
  74.     if (correctPassword) {
  75.       const exist = find(superDatabase, { username: this.state.username });
  76.  
  77.       if (!exist) {
  78.         superDatabase.push(this.state);
  79.         this.setState({
  80.           password: '',
  81.           username: '',
  82.         });
  83.         alert('added.');
  84.       } else {
  85.         alert('exist. cancelled');
  86.       }
  87.  
  88.       console.log('superDatabase', superDatabase);
  89.     }
  90.   }
  91.  
  92.   render() {
  93.     return (
  94.       <form onSubmit={this.onSubmit}>
  95.         <Input value={this.state.username} name="username" type="text" onChange={this.onChangeUsername} />
  96.         <Input value={this.state.password} name="password" type="password" onChange={this.onChangePassword} />
  97.         <button type="submit">Submit</button>
  98.       </form>
  99.     );
  100.   }
  101. }
  102.  
  103. // ######### Export ####################
  104. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement