Guest User

Untitled

a guest
Oct 1st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import { map } from 'ramda'
  2.  
  3. import withForm from 'hoc/withForm'
  4. import { isValidEmail } from 'utilities/validations'
  5.  
  6. const renderError = error => <div>{error}</div>
  7.  
  8. const renderButtonText = form => {
  9. if (form.submitting || form.validating) return '...'
  10. if (form.confirming) return 'Success!'
  11. return 'Submit'
  12. }
  13.  
  14. const RegistrationForm = ({ form }) => (
  15. <form onSubmit={form.submit}>
  16. <input
  17. type="email"
  18. value={form.fields.email.value}
  19. onChange={form.setField('email')}
  20. />
  21. {map(renderError, form.fields.email.errors)}
  22. <input
  23. type="password"
  24. value={form.fields.password.value}
  25. onChange={form.setField('password')}
  26. />
  27. {map(renderError, form.fields.password.errors)}
  28. <button>{renderButtonText(form)}</button>
  29. </form>
  30. )
  31.  
  32. export default withForm({
  33. submit: (form, { createUser }) => {
  34. return createUser({
  35. email: form.fields.email.value,
  36. password: form.fields.password.value
  37. })
  38. },
  39. confirmationDuration: 1000,
  40. fields: {
  41. email: {
  42. getErrors: async (email, { getExistingUser, isValidEmail }) => {
  43. if (!isValidEmail(email)) return ['Please enter a valid email']
  44. const user = await getExistingUser({ email })
  45. if (user) return ['A user with that email already exists']
  46. return []
  47. }
  48. },
  49. password: {
  50. getErrors: password => {
  51. if (password.length < 6) {
  52. return ['Please enter a password at least 6 characters long']
  53. }
  54. return []
  55. }
  56. }
  57. }
  58. })(RegistrationForm)
Add Comment
Please, Sign In to add comment