Guest User

Untitled

a guest
Nov 22nd, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import * as Yup from 'yup';
  5. import {
  6. Formik,
  7. } from 'formik';
  8.  
  9. import { submitFormAction } from '../accountsReducer';
  10. import Form from '../../_components/Form';
  11. import TextField from '../../_components/TextField';
  12. import Button from '../../_components/Button';
  13.  
  14. export const formValidationSchema = Yup.object().shape({
  15. username: Yup.string()
  16. .email('Username must be a valid email address')
  17. .trim()
  18. .required('Username is a required field'),
  19. password: Yup.string()
  20. .trim()
  21. .required('Password is a required field'),
  22. });
  23.  
  24. export const LoginFormComponent = ({ handleSubmit, handleChange, handleBlur }) => (
  25. <Form handleSubmit={handleSubmit}>
  26. <TextField
  27. name="username"
  28. label="Username:"
  29. handleChange={handleChange}
  30. handleBlur={handleBlur}
  31. />
  32.  
  33. <TextField
  34. name="password"
  35. label="Password:"
  36. handleChange={handleChange}
  37. handleBlur={handleBlur}
  38. />
  39.  
  40. <Button type="submit">Login</Button>
  41. </Form>
  42. );
  43.  
  44. LoginFormComponent.propTypes = {
  45. handleSubmit: PropTypes.func.isRequired,
  46. handleChange: PropTypes.func.isRequired,
  47. handleBlur: PropTypes.func.isRequired,
  48. };
  49.  
  50. /**
  51. * Submit credentials to login to application
  52. */
  53. export const LoginPageComponent = ({ submitLoginForm }) => {
  54. const onSubmit = values => submitLoginForm(values);
  55.  
  56. return (
  57. <>
  58. <h1>Login</h1>
  59. <Formik
  60. onSubmit={onSubmit}
  61. validationSchema={formValidationSchema}
  62. render={LoginFormComponent}
  63. />
  64. </>
  65. );
  66. };
  67.  
  68. LoginPageComponent.propTypes = {
  69. submitLoginForm: PropTypes.func.isRequired,
  70. };
  71.  
  72. export default connect(
  73. state => ({ accounts: state.accounts }),
  74. {
  75. submitLoginForm: submitFormAction,
  76. },
  77. )(LoginPageComponent);
Add Comment
Please, Sign In to add comment