Guest User

Untitled

a guest
Mar 5th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import { createActions, handleActions } from 'redux-actions';
  2.  
  3. import { getSignupFormFieldErrors } from './model';
  4.  
  5. export const defaultState = {
  6. inputs: {
  7. username: '',
  8. email: '',
  9. password: '',
  10. passwordConfirmation: '',
  11. },
  12. errors: {
  13. username: null,
  14. email: null,
  15. password: null,
  16. passwordConfirmation: null,
  17. },
  18. isValid: false,
  19. submitted: false,
  20. };
  21.  
  22. export const { inputChange, validateInputs, submitted } = createActions(
  23. {
  24. INPUT_CHANGE: [(name, value) => ({ value }), (name, value) => ({ name })],
  25. },
  26. 'VALIDATE_INPUTS',
  27. 'SUBMITTED',
  28. );
  29.  
  30. export const reducer = handleActions(
  31. {
  32. INPUT_CHANGE: (state, action) => {
  33. const errors = Object.keys(state.errors || {})
  34. .filter(errorField => errorField != action.meta.name)
  35. .reduce(
  36. (errors, errorKey) => ({
  37. ...errors,
  38. [errorKey]: state.errors[errorKey],
  39. }),
  40. {},
  41. );
  42. return {
  43. ...state,
  44. inputs: {
  45. ...state.inputs,
  46. [action.meta.name]: action.payload.value,
  47. },
  48. errors,
  49. submitted: false,
  50. };
  51. },
  52. VALIDATE_INPUTS: state => {
  53. const errors = getSignupFormFieldErrors(
  54. Object.keys(state.inputs).reduce(
  55. (inputs, name) => ({
  56. ...inputs,
  57. ...(state.inputs[name] == '' ? {} : { [name]: state.inputs[name] }),
  58. }),
  59. {},
  60. ),
  61. );
  62. return {
  63. ...state,
  64. errors,
  65. isValid: Object.keys(errors).length === 0,
  66. };
  67. },
  68. SUBMITTED: state => ({
  69. ...state,
  70. submitted: true,
  71. }),
  72. },
  73. defaultState,
  74. );
Add Comment
Please, Sign In to add comment