Guest User

Untitled

a guest
Nov 9th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. import React from 'react';
  2. import { Link, withRouter } from 'react-router-dom';
  3. import { bindActionCreators } from 'redux';
  4. import { Message, Container, Loader, Dimmer, Label, Image, Form, Grid } from 'semantic-ui-react';
  5. import { isEmail } from 'validator';
  6. import { find, map, isEmpty } from 'lodash';
  7. import { Connect } from '../../../../utils';
  8. import { login } from '../../../../redux/auth';
  9. import logo from '../../../../assets/logo.svg';
  10.  
  11. class AuthLogin extends React.Component {
  12. constructor(props) {
  13. super(props);
  14.  
  15. this.state = {
  16. email: '',
  17. password: '',
  18. errors: []
  19. };
  20. }
  21.  
  22. componentWillReceiveProps(nextProps) {
  23. const { currentUser, loginError } = this.props;
  24. const newUser = nextProps.currentUser;
  25. const newError = nextProps.loginError;
  26.  
  27. if (newUser && (newUser !== currentUser)) {
  28. this.props.history.push('/');
  29. } else if (newError !== loginError) {
  30. const errors = [];
  31. if (newError) {
  32. errors.push({ message: 'Incorrect username or password' });
  33. }
  34. this.setState({ errors });
  35. }
  36. }
  37.  
  38. validateFields = () => {
  39. const { email, password } = this.state;
  40. const errors = [];
  41. if (isEmpty(email)) {
  42. errors.push({ field: 'email', message: 'Email is required' });
  43. } else if (!isEmail(email)) {
  44. errors.push({ field: 'email', message: 'Please enter valid email'});
  45. }
  46. if (isEmpty(password)) {
  47. errors.push({ field: 'password', message: 'Password is required' });
  48. }
  49.  
  50. this.setState({ errors });
  51.  
  52. return isEmpty(errors);
  53. }
  54.  
  55. handleChange = (e, { name, value }) => this.setState({ [name]: value, errors: [] })
  56.  
  57. handleSubmit = () => {
  58. if (this.validateFields()) {
  59. const { email, password } = this.state;
  60. this.props.login(email, password);
  61. }
  62. }
  63.  
  64. render() {
  65. const { email, password, errors } = this.state;
  66. const { isLoading } = this.props;
  67.  
  68. return (
  69. <div className="page">
  70. <Container>
  71. {
  72. isLoading &&
  73. <Dimmer active inverted>
  74. <Loader size='medium' />
  75. </Dimmer>
  76. }
  77. <Grid centered>
  78. <Grid.Column>
  79. <Grid.Row>
  80. <Image className="m-t-4 m-b-4" src={logo} size="small" centered />
  81. <Label.Detail className="t-align-center m-b-1">Sign in to your account</Label.Detail>
  82. </Grid.Row>
  83. <Grid.Row>
  84. <Form
  85. onSubmit={this.handleSubmit}
  86. error={!isEmpty(errors)}
  87. noValidate
  88. >
  89. <Form.Input
  90. placeholder="Your email address"
  91. type="email"
  92. name="email"
  93. value={email}
  94. onChange={this.handleChange}
  95. error={!!find(errors, { field: 'email' })}
  96. />
  97. <Form.Input
  98. placeholder="Password"
  99. type="password"
  100. name="password"
  101. value={password}
  102. onChange={this.handleChange}
  103. error={!!find(errors, { field: 'password' })}
  104. />
  105. <Message error>
  106. {map(errors, err =>
  107. <Message.Item key={err.message}>{err.message}</Message.Item>
  108. )}
  109. </Message>
  110. <Form.Button
  111. className="m-t-4"
  112. content="Login"
  113. fluid
  114. />
  115. <Label.Detail className="t-align-center m-t-1">
  116. <Link to="/auth">Forgot password</Link>
  117. </Label.Detail>
  118. </Form>
  119. </Grid.Row>
  120. </Grid.Column>
  121. </Grid>
  122. </Container>
  123. </div>
  124. );
  125. }
  126. }
  127.  
  128. const mapStateToProps = state => ({
  129. isLoading: state.auth.isLoading,
  130. loginError: state.auth.error
  131. });
  132.  
  133. const mapDispatchToProps = dispatch => bindActionCreators({
  134. login
  135. }, dispatch);
  136.  
  137. export default withRouter(Connect(
  138. mapStateToProps,
  139. mapDispatchToProps
  140. )(AuthLogin));
Add Comment
Please, Sign In to add comment