Guest User

Untitled

a guest
Jun 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. export interface Loadable {
  2. isLoading: boolean;
  3. success: boolean;
  4. error: any | null;
  5. }
  6.  
  7. export interface LoadableActionTypes {
  8. isLoadingAction: string;
  9. successAction: string;
  10. errorAction: string;
  11. clearAction: string;
  12. }
  13.  
  14. export const INITIAL_LOADABLE_STATE: Loadable = {
  15. isLoading: false,
  16. success: false,
  17. error: null,
  18. };
  19.  
  20. export const withLoadable = (actionTypes: LoadableActionTypes) =>
  21. reducer =>
  22. (state, action) => {
  23. switch (action.type) {
  24. case actionTypes.isLoadingAction: {
  25. return {
  26. ...state,
  27. isLoading: true,
  28. success: false,
  29. error: null,
  30. };
  31. }
  32. case actionTypes.errorAction: {
  33. return {
  34. ...state,
  35. isLoading: false,
  36. error: action.payload,
  37. success: false,
  38. };
  39. }
  40. case actionTypes.successAction: {
  41. return {
  42. ...state,
  43. ...action.payload,
  44. isLoading: false,
  45. success: true,
  46. error: null,
  47. };
  48. }
  49. case actionTypes.clearAction: {
  50. return {
  51. ...state,
  52. ...INITIAL_LOADABLE_STATE,
  53. };
  54. }
  55. default: {
  56. return reducer(state, action);
  57. }
  58. }
  59. };
Add Comment
Please, Sign In to add comment