File size: 3,649 Bytes
7e79d67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React, { Component } from 'react';
import {connect} from 'react-redux';
import {Field, reduxForm, formValueSelector} from 'redux-form/immutable';
import { push } from 'react-router-redux';
import {
  TextField
} from 'redux-form-material-ui';
import RaisedButton from 'material-ui/RaisedButton';
import CenteredSection from '../../containers/HomePage/CenteredSection';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import Notifications, {notify} from 'react-notify-toast';
import {GridList, GridTile} from 'material-ui/GridList';
import {removeExtraChar} from '../Global/GlobalFun';


const errors = {}

const required = value => (value == null ? 'Required' : undefined);
const email = value =>
  (value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)
    ? 'Invalid email'
    : undefined);

const validateLogin = values => {
  
  errors.password = required(values.password)
  errors.email = email(values.email || '')
  if (!values.email) {
    errors.email = 'Required'
  } else if (email(values.email)) {
    errors.email = 'Invalid Email'
  }
  return errors
}


class LoginForm extends Component {
  static propTypes = {
    LoginUser: React.PropTypes.func
  }

  submitLoginForm = async () => {
    await this.props.LoginUser({variables: {email: this.props.Email,
                    password: this.props.Password}
                 }).then((res)=>localStorage.setItem('token', res.data.signinUser.token)).then(()=> this.props.GoToHome()).then(()=>location.reload()).catch((res)=>alert(removeExtraChar(res)))
  }

  render() {
    const {handleSubmit, pristine, reset, submitting} = this.props;
    return (
      <CenteredSection>
      <Notifications />
       <form onSubmit={handleSubmit}>
      <GridList cols={1} cellHeight={110} padding={1}>
        <GridTile>
          <Field
            name="email"
            component={TextField}
            hintText="Email"
            floatingLabelText="Email"
            validate={[required, email]}
          />
        </GridTile>
      </GridList>
      <GridList cols={1} cellHeight={110} padding={1}>
        <GridTile>
          <Field
            name="password"
            component={TextField}
            hintText="Password"
            type="password"
            floatingLabelText="Password"
            validate={required}
          />
        </GridTile>
      </GridList>
      <GridList cols={1} cellHeight={110} padding={1}>
        <GridTile style={{textAlign: "center",paddingTop:"20px"}}>
          <RaisedButton style={{"marginRight":"20px"}} label="Submit" onKeyPress={event => {
              console.log("hello");
              if (event.key === "Enter") {
                this.submitLoginForm();
              }
            }} onClick={()=>this.submitLoginForm()} disabled={errors.email != null || errors.password != null} primary={true} />
        </GridTile>
      </GridList>
      </form>
      </CenteredSection>
    );
  }
}

const selector = formValueSelector('login_form');

LoginForm = reduxForm({
  form: 'login_form',
  validate: validateLogin
})(LoginForm);

LoginForm = connect(state => ({
  Email: selector(state, 'email'),
  Password: selector(state, 'password')
}))(LoginForm);

export function mapDispatchToProps(dispatch) {
  return {
    GoToHome: () => dispatch(push('/'))
  };
}


const addMutation = gql`
  mutation LoginUser ($email: String!, $password: String!) {
    signinUser(email: {
     email: $email, password: $password
  }) {
    token
  }
  }
`

const LoginMutation = graphql(addMutation, {name: 'LoginUser'})(LoginForm)

export default connect (null, mapDispatchToProps)(LoginMutation);