Guest User

Untitled

a guest
Jan 9th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import { createStore, combineReducers, applyMiddleware } from 'redux';
  2.  
  3. const initialState = {
  4. loading: false,
  5. list: []
  6. };
  7. const userReducer = (state = initialState, action) => {
  8. switch(action.type) {
  9. case 'ADD_USER': {
  10. const list = [...state.list];
  11. list.push(action.user)
  12. return { ...state, list };
  13. }
  14. case 'FETCH': {
  15. return { ...state, loading: true };
  16. }
  17. default: {
  18. return state;
  19. }
  20. }
  21. };
  22.  
  23. const initialStateProducts = {
  24. loading: false,
  25. list: []
  26. };
  27. const productsReducer = (state = initialStateProducts, action) => {
  28. switch(action.type) {
  29. case 'ADD_PRODUCT': {
  30. const list = [...state.list];
  31. list.push(action.product);
  32. return { ...state, list };
  33. }
  34. case 'FETCH': {
  35. return { ...state, loading: true };
  36. }
  37. default: {
  38. return state;
  39. }
  40. }
  41. };
  42.  
  43. const reducers = combineReducers({
  44. users: userReducer,
  45. products: productsReducer
  46. });
  47.  
  48. const logger = store => next => action => {
  49. console.log(action.type, "Fired!!");
  50. next(action);
  51. }
  52.  
  53. const arrayMiddleware = store => next => action => {
  54. if(Array.isArray(action)) {
  55. return action.map(ac => store.dispatch(ac));
  56. }
  57. return next(action);
  58. }
  59.  
  60. const fnMiddleware = store => next => action => {
  61. if(typeof action === "function") {
  62. return action(store.getState(), store.dispatch);
  63. }
  64. return next(action);
  65. }
  66.  
  67. const middlewares = applyMiddleware(fnMiddleware, arrayMiddleware, logger);
  68.  
  69. const store = createStore(reducers, middlewares);
  70. store.subscribe(() => {
  71. console.log("State changed", store.getState());
  72. });
  73.  
  74. const addUser = (user) => {
  75. return {
  76. type: 'ADD_USER',
  77. user
  78. }
  79. };
  80.  
  81. store.dispatch(addUser({name: 'ABCD', email: 'some_mail@email.com'}));
  82. store.dispatch({ type: 'ADD_PRODUCT', product: {name: 'Apple', qty: 10} });
  83. store.dispatch({ type: 'FETCH' });
  84.  
  85. store.dispatch([
  86. { type: 'ADD_USER', user: { name: 'ABCD1', email: 'some_mail@email.com' } },
  87. { type: 'ADD_USER', user: { name: 'ABCD1', email: 'some_mail@email.com' } },
  88. { type: 'ADD_PRODUCT', product: { name: 'someproduct', qty: 10 } },
  89. { type: 'ADD_PRODUCT', product: { name: 'someproduct', qty: 10 } },
  90. { type: 'ADD_PRODUCT', product: { name: 'someproduct', qty: 10 } },
  91. ]);
  92. store.dispatch(function(state, dispatch) {
  93. console.log(state);
  94. console.log("Waiting for API response");
  95. setTimeout(() => {
  96. console.log("API responded");
  97. dispatch({ type: 'ADD_PRODUCT', product: { name: 'someproduct', qty: 10 } })
  98. }, 1000);
  99. });
Add Comment
Please, Sign In to add comment