Advertisement
Guest User

Untitled

a guest
Mar 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. import React, { useCallback, useState, useEffect } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { TextField, Button, Link as MuiLink } from '@material-ui/core';
  4. import { withRouter, Link } from 'react-router-dom';
  5. import { isEmpty, isEmail } from 'validator';
  6. import { connect } from 'react-redux';
  7. import { useFormField } from 'app-hooks';
  8. import firebase from 'app-firebase';
  9. import { FormBox, ButtonWrapper } from '../shared';
  10.  
  11. const mapStateToProps = state => ({
  12. authUser: state.authUser,
  13. });
  14.  
  15. const LoginBox = ({ history, authUser }) => {
  16. const [
  17. { email, password },
  18. handleFieldChange,
  19. handleFieldBlur,
  20. handleFieldFocus,
  21. validateFieldAfterSubmit,
  22. ] = useFormField({
  23. email: {
  24. value: '',
  25. validationRules: [
  26. {
  27. method: isEmpty,
  28. validWhen: false,
  29. errorText: 'This field should not be left empty!',
  30. },
  31. {
  32. method: isEmail,
  33. errorText: 'This field should contain an email!',
  34. },
  35. ],
  36. },
  37. password: {
  38. value: '',
  39. validationRules: [
  40. {
  41. method: isEmpty,
  42. validWhen: false,
  43. errorText: 'This field should not be left empty!',
  44. },
  45. ],
  46. },
  47. });
  48. const [errorText, setErrorText] = useState('');
  49. const [isLoading, setIsLoading] = useState(false);
  50. const handleFormSubmit = useCallback(
  51. async e => {
  52. e.preventDefault();
  53. setErrorText('');
  54.  
  55. if (!validateFieldAfterSubmit('email', email.value)) return;
  56.  
  57. if (!validateFieldAfterSubmit('password', password.value)) return;
  58.  
  59. setIsLoading(true);
  60.  
  61. try {
  62. await firebase.auth().signInWithEmailAndPassword(email.value, password.value);
  63. } catch (err) {
  64. setIsLoading(false);
  65. setErrorText(err.message);
  66. }
  67. },
  68. [validateFieldAfterSubmit, email.value, password.value],
  69. );
  70.  
  71. useEffect(() => {
  72. if (authUser) history.push('/dashboard');
  73. }, [authUser, history]);
  74.  
  75. return (
  76. <FormBox
  77. title="Login"
  78. formLink={
  79. <MuiLink component={Link} to="/register">
  80. Haven&#39;t got an account yet? Register!
  81. </MuiLink>
  82. }
  83. isLoading={isLoading}
  84. onSubmit={handleFormSubmit}
  85. errorText={errorText}
  86. >
  87. <TextField
  88. label="Email"
  89. margin="dense"
  90. variant="outlined"
  91. name="email"
  92. value={email.value}
  93. onChange={handleFieldChange}
  94. onBlur={handleFieldBlur}
  95. onFocus={handleFieldFocus}
  96. fullWidth
  97. helperText={email.errorText}
  98. error={!email.isValid}
  99. />
  100. <TextField
  101. label="Password"
  102. margin="dense"
  103. variant="outlined"
  104. name="password"
  105. type="password"
  106. value={password.value}
  107. onChange={handleFieldChange}
  108. onBlur={handleFieldBlur}
  109. onFocus={handleFieldFocus}
  110. fullWidth
  111. helperText={password.errorText}
  112. error={!password.isValid}
  113. />
  114. <ButtonWrapper dense>
  115. <Button variant="contained" type="submit" color="primary">
  116. Submit
  117. </Button>
  118. </ButtonWrapper>
  119. </FormBox>
  120. );
  121. };
  122.  
  123. LoginBox.propTypes = {
  124. /**
  125. * The authenticated user
  126. */
  127. authUser: PropTypes.object,
  128.  
  129. /**
  130. * @ignore
  131. */
  132. history: PropTypes.object.isRequired,
  133. };
  134.  
  135. export default withRouter(connect(mapStateToProps)(LoginBox));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement