app.js 966 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { Map } from "immutable";
  2. import {
  3. TEST_ACTION,
  4. TEST_ASYNC_ACTION_START,
  5. TEST_ASYNC_ACTION_ERROR,
  6. TEST_ASYNC_ACTION_SUCCESS,
  7. } from "actions/app";
  8. const initialState = Map({
  9. counter: 0,
  10. asyncLoading: false,
  11. asyncError: null,
  12. asyncData: null,
  13. });
  14. const actionsMap = {
  15. [TEST_ACTION]: (state) => {
  16. const counter = state.get("counter") + 1;
  17. return state.merge({
  18. counter,
  19. });
  20. },
  21. // Async action
  22. [TEST_ASYNC_ACTION_START]: (state) => {
  23. return state.merge({
  24. asyncLoading: true,
  25. asyncError: null,
  26. });
  27. },
  28. [TEST_ASYNC_ACTION_ERROR]: (state, action) => {
  29. return state.merge({
  30. asyncLoading: false,
  31. asyncError: action.data,
  32. });
  33. },
  34. [TEST_ASYNC_ACTION_SUCCESS]: (state, action) => {
  35. return state.merge({
  36. asyncLoading: false,
  37. asyncData: action.data,
  38. });
  39. },
  40. };
  41. export default function reducer(state = initialState, action = {}) {
  42. const fn = actionsMap[action.type];
  43. return fn ? fn(state, action) : state;
  44. }