UserReducer.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. export const UserReducer = (state={}, { type, user={} }) => {
  2. if (type === 'USER_CREATE') {
  3. if (Object.entries(user).length !== 0) {
  4. return {
  5. ...state,
  6. ...user
  7. }
  8. }
  9. else
  10. return state
  11. }
  12. if (type === 'USER_CHANGE') {
  13. if(Object.entries(user).length !== 0) {
  14. let changeUser = {}
  15. for (let key in state) {
  16. if (state[key] === user[key]) {
  17. changeUser[key] = state[key]
  18. }
  19. else {
  20. changeUser[key] = user[key]
  21. }
  22. }
  23. return {
  24. ...changeUser
  25. }
  26. }
  27. else {
  28. return state
  29. }
  30. }
  31. if (type === 'USER_REMOVE') {
  32. return {}
  33. }
  34. return state
  35. }
  36. export const actionUserCreate = user => ({ type: 'USER_CREATE', user })
  37. export const actionUserChange = user => ({ type: 'USER_CHANGE', user })
  38. export const actionUserRemove = () => ({ type: 'USER_REMOVE' })