crud.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { createReducerObject } from 'create-reducer-object';
  2. import {
  3. DOC_CREATED,
  4. DOC_CREATE_RESPONDED,
  5. DOC_READ,
  6. DOC_READ_RESPONDED,
  7. DOC_UPDATED,
  8. DOC_UPDATE_RESPONDED,
  9. DOC_DELETED,
  10. DOC_DELETE_RESPONDED
  11. } from 'constants/actions';
  12. export const initialState = {};
  13. function insertDoc(routeDocs, id, pending, fields) {
  14. return [
  15. ...routeDocs,
  16. {
  17. id,
  18. pending,
  19. ...fields
  20. }
  21. ];
  22. }
  23. function updateDoc(routeDocs, index, id, pending, fields = {}) {
  24. if (index === -1) {
  25. return insertDoc(routeDocs, id, false, fields);
  26. }
  27. return [
  28. ...routeDocs.slice(0, index - 1),
  29. {
  30. ...routeDocs[index],
  31. id,
  32. pending,
  33. ...fields
  34. },
  35. ...routeDocs.slice(index + 1)
  36. ];
  37. }
  38. function deleteDoc(docs, index) {
  39. return docs.slice(0, index - 1)
  40. .concat(docs.slice(index + 1));
  41. }
  42. function getRouteDocs(state, route) {
  43. if (state[route]) {
  44. return state[route].items;
  45. }
  46. return [];
  47. }
  48. function onCreate(state, { route, pendingId, fields }) {
  49. if (state[route] && state[route].loading) {
  50. return {};
  51. }
  52. const routeDocs = getRouteDocs(state, route);
  53. return {
  54. [route]: {
  55. loading: true,
  56. items: insertDoc(routeDocs, pendingId, true, fields)
  57. }
  58. };
  59. }
  60. function onCreateResponse(state, { route, pendingId, err, response }) {
  61. const routeDocs = getRouteDocs(state, route);
  62. const index = routeDocs.findIndex(({ id }) => id === pendingId);
  63. if (err) {
  64. const newRoute = {
  65. ...(state[route] || {}),
  66. error: true
  67. };
  68. if (index === -1) {
  69. return {
  70. [route]: newRoute
  71. };
  72. }
  73. return {
  74. [route]: {
  75. ...newRoute,
  76. items: deleteDoc(routeDocs, index)
  77. }
  78. };
  79. }
  80. const { id: actualId, ...fields } = response.data;
  81. return {
  82. [route]: {
  83. ...state[route],
  84. items: updateDoc(routeDocs, index, actualId, false, fields)
  85. }
  86. };
  87. }
  88. function onRead(state, { route }) {
  89. if (state[route]) {
  90. return {
  91. [route]: {
  92. ...state[route],
  93. loading: true
  94. }
  95. };
  96. }
  97. return {
  98. [route]: {
  99. loading: true,
  100. items: []
  101. }
  102. };
  103. }
  104. function onReadResponse(state, { route, err, response }) {
  105. if (err) {
  106. return {
  107. [route]: {
  108. ...(state[route] || {}),
  109. loading: false,
  110. error: true
  111. }
  112. };
  113. }
  114. return {
  115. [route]: {
  116. ...(state[route] || {}),
  117. loading: false,
  118. error: false,
  119. items: response.data[route]
  120. }
  121. };
  122. }
  123. function onUpdate(state, { route, id, fields }) {
  124. if (state[route] && state[route].loading) {
  125. return {};
  126. }
  127. const routeDocs = getRouteDocs(state, route);
  128. const index = routeDocs.findIndex(({ id: docId }) => docId === id);
  129. return {
  130. [route]: {
  131. loading: true,
  132. items: updateDoc(routeDocs, index, id, true, fields)
  133. }
  134. };
  135. }
  136. function onUpdateResponse(state, { route, id, err }) {
  137. const routeDocs = getRouteDocs(state, route);
  138. const index = routeDocs.findIndex(({ id: docId }) => docId === id);
  139. const newItems = updateDoc(routeDocs, index, id, false);
  140. if (err) {
  141. const newRoute = {
  142. ...(state[route] || {}),
  143. error: true
  144. };
  145. if (index === -1) {
  146. return {
  147. [route]: newRoute
  148. };
  149. }
  150. return {
  151. [route]: {
  152. ...newRoute,
  153. items: newItems
  154. }
  155. };
  156. }
  157. return {
  158. [route]: {
  159. ...state[route],
  160. error: false,
  161. items: newItems
  162. }
  163. };
  164. }
  165. function onDelete(state, { route, id }) {
  166. const routeDocs = getRouteDocs(state, route);
  167. const index = routeDocs.findIndex(({ id: docId }) => docId === id);
  168. return {
  169. [route]: {
  170. ...state[route],
  171. error: false,
  172. items: updateDoc(routeDocs, index, id, true)
  173. }
  174. };
  175. }
  176. function onDeleteResponse(state, { route, id, err }) {
  177. const routeDocs = getRouteDocs(state, route);
  178. const index = routeDocs.findIndex(({ id: docId }) => docId === id);
  179. if (err) {
  180. return {
  181. [route]: {
  182. ...state[route],
  183. error: true,
  184. items: updateDoc(routeDocs, index, id, false)
  185. }
  186. };
  187. }
  188. return {
  189. [route]: {
  190. ...state[route],
  191. error: false,
  192. items: deleteDoc(routeDocs, index)
  193. }
  194. };
  195. }
  196. const reducerMap = {
  197. [DOC_CREATED]: onCreate,
  198. [DOC_CREATE_RESPONDED]: onCreateResponse,
  199. [DOC_READ]: onRead,
  200. [DOC_READ_RESPONDED]: onReadResponse,
  201. [DOC_UPDATED]: onUpdate,
  202. [DOC_UPDATE_RESPONDED]: onUpdateResponse,
  203. [DOC_DELETED]: onDelete,
  204. [DOC_DELETE_RESPONDED]: onDeleteResponse
  205. };
  206. export const crud = createReducerObject(reducerMap, initialState);