Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. FETCH_REQUEST // API promise is pending
  2. FETCH_SUCCESS // API promise resolved
  3. FETCH_FAILURE // API promise rejected
  4.  
  5. export const loadEntity = (
  6. entity,
  7. dataPromise,
  8. ) => {
  9. if (!dataPromise || !dataPromise.then)
  10. throw new Error('dataPromise must be a Promise, and cannot be null/undefined');
  11.  
  12. return (dispatch) => {
  13.  
  14. // Notify UI of fetch request
  15. dispatch(apiRequest(entity)());
  16.  
  17. return dataPromise
  18. .then(data => {
  19. // Dispatch success to update model state
  20. dispatch(
  21. apiSuccess(entity)(data)
  22. )
  23. })
  24. .catch(error => {
  25. // Dispatch failure to notify UI
  26. dispatch(
  27. apiFailure(entity)(error)
  28. )
  29. })
  30. }
  31. };
  32.  
  33. export const apiRequest = (entity) => {
  34. return makeEntityActionCreator(FETCH_REQUEST, entity);
  35. };
  36.  
  37. export const apiSuccess = (entity) => {
  38. return makeEntityActionCreator(FETCH_SUCCESS, entity, 'data');
  39. };
  40.  
  41. export const apiFailure = (entity) => {
  42. return makeEntityActionCreator(FETCH_FAILURE, entity, 'error');
  43. };
  44.  
  45. export function makeEntityActionCreator(type, entity, ...keys) {
  46. if (!type) throw new Error('Type cannot be null/undefined');
  47. if (!entity) throw new Error('Entity cannot be null/undefined');
  48. return function(...args) {
  49. let action = { type, entity };
  50. keys.forEach((arg, index) => {
  51. action[keys[index]] = args[index]
  52. });
  53. return action;
  54. }
  55. }
  56.  
  57. export default function model (state = {}, action) {
  58. switch (action.type) {
  59. case FETCH_SUCCESS: // fall through
  60. case FETCH_FAILURE: // fall through
  61. case FETCH_REQUEST: {
  62. return {
  63. ...state,
  64. [action.entity]: entity(
  65. state[action.entity],
  66. action
  67. )
  68. }
  69. }
  70. default: {
  71. return state;
  72. }
  73. }
  74. }
  75.  
  76. const INITIAL_ENTITY_STATE = {
  77. isFetching: false,
  78. lastUpdated: undefined,
  79. data: {}
  80. };
  81.  
  82. function entity (
  83. state = INITIAL_ENTITY_STATE,
  84. action
  85. ) {
  86. switch (action.type) {
  87. case FETCH_REQUEST: {
  88. return {
  89. ...state,
  90. isFetching: true
  91. }
  92. }
  93. case FETCH_SUCCESS: {
  94. return {
  95. ...state,
  96. isFetching: false,
  97. lastUpdated: Date.now(),
  98. data: action.data
  99. }
  100. }
  101. case FETCH_FAILURE: {
  102. return {
  103. ...state,
  104. isFetching: false,
  105. lastUpdated: Date.now(),
  106. error: action.error
  107. }
  108. }
  109. default: {
  110. return state;
  111. }
  112. }
  113. }
  114.  
  115. "model": {
  116. "fooEntity": {
  117. "isFetching": false,
  118. "data": ['foo','bar','from','server'],
  119. "lastUpdated": 1467308062465
  120. },
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement