Guest User

Untitled

a guest
Feb 11th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import { connect } from 'react-redux'
  3. import autobind from 'autobind-decorator'
  4. import styled from 'styled-components'
  5. import { compose } from 'ramda'
  6. import { withFormik, Field } from 'formik'
  7. import Yup from 'yup'
  8. import { login } from '_client/actions/CashierActions'
  9. import Box from '_client/components/elements/Box'
  10. import App from '_client/components/elements/App'
  11. import Heading from '_client/components/elements/Heading'
  12. import PrimaryButton from '_client/components/composites/PrimaryButton'
  13. import { FormikCustomInput } from '_client/components/elements/InputField'
  14. import BackgroundSVG from '_client/assets/svg/main-background.svg'
  15.  
  16. const Form = styled.form`
  17. display: flex;
  18. flex-direction: column;
  19. `
  20.  
  21. const loginEnhancer = withFormik({
  22. displayName: 'LoginForm',
  23. mapPropsToValues: props => ({
  24. username: '',
  25. password: ''
  26. }),
  27. validationSchema: Yup.object().shape({
  28. username: Yup.string().required('Username is required'),
  29. password: Yup.string().required('Password is required')
  30. }),
  31. handleSubmit: (
  32. values,
  33. {
  34. props,
  35. setSubmitting,
  36. setErrors
  37. }
  38. ) =>
  39. props.login(values)
  40. .catch(({ message }) => {
  41. setErrors({ password: message })
  42. setSubmitting(false)
  43. })
  44. })
  45.  
  46. const Login = props => {
  47. const {
  48. values,
  49. touched,
  50. errors,
  51. dirty,
  52. isSubmitting,
  53. isValid,
  54. handleChange,
  55. handleBlur,
  56. handleSubmit,
  57. handleReset
  58. } = props
  59.  
  60. return (
  61. <App
  62. full
  63. orientation="portrait"
  64. background={`url(${BackgroundSVG})`}
  65. p={2}
  66. justify="center"
  67. >
  68. <Box>
  69. <Heading
  70. fontWeight={400}
  71. mb="0.2em"
  72. align="center"
  73. >
  74. {'Welcome'}
  75. </Heading>
  76. <Heading
  77. level={2}
  78. fontWeight={400}
  79. fontSize={16}
  80. mb="2em"
  81. align="center"
  82. >
  83. {`Please sign in`}
  84. </Heading>
  85. <Form onSubmit={handleSubmit}>
  86. <Box mb={2}>
  87. <Field
  88. autoFocus
  89. name="username"
  90. component={FormikCustomInput}
  91. placeholder="ID"
  92. value={values.username}
  93. onChange={handleChange}
  94. onBlur={handleBlur}
  95. />
  96. </Box>
  97. <Box mb={2}>
  98. <Field
  99. name="password"
  100. placeholder="Password"
  101. type="password"
  102. component={FormikCustomInput}
  103. value={values.password}
  104. onChange={handleChange}
  105. onBlur={handleBlur}
  106. />
  107. </Box>
  108. <PrimaryButton
  109. label="Login"
  110. type="submit"
  111. disabled={!isValid || isSubmitting}
  112. />
  113. </Form>
  114. </Box>
  115. </App>
  116. )
  117. }
  118.  
  119. export default connect(
  120. null,
  121. dispatch => ({
  122. login: compose(dispatch, login)
  123. })
  124. )(loginEnhancer(Login))
Add Comment
Please, Sign In to add comment