Guest User

Untitled

a guest
Oct 17th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. import React from 'react'
  2. import { withRouter, Link } from 'react-router-dom'
  3. import { graphql, compose } from 'react-apollo'
  4. import { withFormik } from 'formik'
  5. import Yup from 'yup'
  6.  
  7. import FormWideError from '../elements/form/FormWideError'
  8. import TextInput from '../elements/form/TextInput'
  9. import Button from '../elements/form/Button'
  10. import { H2 } from '../elements/text/Headings'
  11. import Wrapper from '../elements/layout/Wrapper'
  12.  
  13. import CurrentUser from '../queries/CurrentUser'
  14. import signinUser from '../mutations/signinUser'
  15.  
  16. const handleSubmit = (payload, { props, setSubmitting, setErrors }) => {
  17. const {email, password} = payload
  18. props.signinUser({ variables: { email, password } })
  19. .then((response) => {
  20. window.localStorage.setItem('graphcoolToken', response.data.signinUser.token)
  21. props.data.refetch()
  22. props.history.push('/')
  23. }, (e) => {
  24. const errors = e.graphQLErrors.map(error => error.message)
  25. console.log(errors)
  26. setSubmitting(false)
  27. setErrors({ email: ' ', password: ' ', form: errors })
  28. })
  29. }
  30.  
  31. const LoginForm = ({
  32. handleSubmit,
  33. errors,
  34. touched,
  35. values,
  36. handleChange,
  37. handleBlur,
  38. isSubmitting
  39. }) =>
  40. <Wrapper>
  41. <H2>Sign In</H2>
  42. <FormWideError error={errors.form} />
  43. <form onSubmit={handleSubmit}>
  44. <TextInput
  45. id='email'
  46. type='email'
  47. label='Email'
  48. placeholder='you@yourdomain.com'
  49. error={errors.email && touched.email && errors.email}
  50. value={values.email}
  51. onChange={handleChange}
  52. onBlur={handleBlur}
  53. />
  54. <TextInput
  55. id='password'
  56. type='password'
  57. label='Password'
  58. placeholder=''
  59. error={errors.password && touched.password && errors.password}
  60. value={values.password}
  61. onChange={handleChange}
  62. onBlur={handleBlur}
  63. />
  64. <Button
  65. primary
  66. type='submit'
  67. disabled={
  68. isSubmitting ||
  69. !!(errors.email && touched.email) ||
  70. !!(errors.password && touched.password)
  71. }
  72. >
  73. Sign In
  74. </Button>
  75. </form>
  76. </Wrapper>
  77.  
  78. const LoginFormWithGraphQL = compose(
  79. graphql(signinUser, {name: 'signinUser'}),
  80. graphql(CurrentUser, { options: { fetchPolicy: 'network-only' } }),
  81. withFormik({
  82. validationSchema: Yup.object().shape({
  83. email: Yup.string()
  84. .email('Invalid email address')
  85. .required('Email is required'),
  86. password: Yup.string()
  87. .required('Password is required')
  88. }),
  89. mapPropsToValues: ({ variables }) => ({
  90. ...variables
  91. }),
  92. handleSubmit: handleSubmit,
  93. displayName: 'Login'
  94. })
  95. )(LoginForm)
  96.  
  97. const LoginFormWithRouter = withRouter(LoginFormWithGraphQL)
  98.  
  99. class Login extends React.Component {
  100. componentWillUpdate (nextProps) {
  101. if (!this.props.data.user && nextProps.data.user) {
  102. this.props.history.push('/dashboard')
  103. }
  104. }
  105.  
  106. render () {
  107. if (this.props.data.loading) {
  108. return (<div></div>)
  109. }
  110.  
  111. return (
  112. <div>
  113. <LoginFormWithRouter variables={{ email: '', password: '' }} />
  114. <p>Need an account? <Link to="/signup">Create one now</Link></p>
  115. </div>
  116. )
  117. }
  118.  
  119. }
  120.  
  121. // Set network fetch policy to network only for security reasons
  122. export default graphql(CurrentUser, { options: { fetchPolicy: 'network-only' } })(withRouter(Login))
Add Comment
Please, Sign In to add comment