| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- import { createReducerObject } from 'create-reducer-object';
- import {
- DOC_CREATED,
- DOC_CREATE_RESPONDED,
- DOC_READ,
- DOC_READ_RESPONDED,
- DOC_UPDATED,
- DOC_UPDATE_RESPONDED,
- DOC_DELETED,
- DOC_DELETE_RESPONDED
- } from 'constants/actions';
- export const initialState = {};
- function insertDoc(routeDocs, id, pending, fields) {
- return [
- ...routeDocs,
- {
- id,
- pending,
- ...fields
- }
- ];
- }
- function updateDoc(routeDocs, index, id, pending, fields = {}) {
- if (index === -1) {
- return insertDoc(routeDocs, id, false, fields);
- }
- return [
- ...routeDocs.slice(0, index - 1),
- {
- ...routeDocs[index],
- id,
- pending,
- ...fields
- },
- ...routeDocs.slice(index + 1)
- ];
- }
- function deleteDoc(docs, index) {
- return docs.slice(0, index - 1)
- .concat(docs.slice(index + 1));
- }
- function getRouteDocs(state, route) {
- if (state[route]) {
- return state[route].items;
- }
- return [];
- }
- function onCreate(state, { route, pendingId, fields }) {
- if (state[route] && state[route].loading) {
- return {};
- }
- const routeDocs = getRouteDocs(state, route);
- return {
- [route]: {
- loading: true,
- items: insertDoc(routeDocs, pendingId, true, fields)
- }
- };
- }
- function onCreateResponse(state, { route, pendingId, err, response }) {
- const routeDocs = getRouteDocs(state, route);
- const index = routeDocs.findIndex(({ id }) => id === pendingId);
- if (err) {
- const newRoute = {
- ...(state[route] || {}),
- error: true
- };
- if (index === -1) {
- return {
- [route]: newRoute
- };
- }
- return {
- [route]: {
- ...newRoute,
- items: deleteDoc(routeDocs, index)
- }
- };
- }
- const { id: actualId, ...fields } = response.data;
- return {
- [route]: {
- ...state[route],
- items: updateDoc(routeDocs, index, actualId, false, fields)
- }
- };
- }
- function onRead(state, { route }) {
- if (state[route]) {
- return {
- [route]: {
- ...state[route],
- loading: true
- }
- };
- }
- return {
- [route]: {
- loading: true,
- items: []
- }
- };
- }
- function onReadResponse(state, { route, err, response }) {
- if (err) {
- return {
- [route]: {
- ...(state[route] || {}),
- loading: false,
- error: true
- }
- };
- }
- return {
- [route]: {
- ...(state[route] || {}),
- loading: false,
- error: false,
- items: response.data[route]
- }
- };
- }
- function onUpdate(state, { route, id, fields }) {
- if (state[route] && state[route].loading) {
- return {};
- }
- const routeDocs = getRouteDocs(state, route);
- const index = routeDocs.findIndex(({ id: docId }) => docId === id);
- return {
- [route]: {
- loading: true,
- items: updateDoc(routeDocs, index, id, true, fields)
- }
- };
- }
- function onUpdateResponse(state, { route, id, err }) {
- const routeDocs = getRouteDocs(state, route);
- const index = routeDocs.findIndex(({ id: docId }) => docId === id);
- const newItems = updateDoc(routeDocs, index, id, false);
- if (err) {
- const newRoute = {
- ...(state[route] || {}),
- error: true
- };
- if (index === -1) {
- return {
- [route]: newRoute
- };
- }
- return {
- [route]: {
- ...newRoute,
- items: newItems
- }
- };
- }
- return {
- [route]: {
- ...state[route],
- error: false,
- items: newItems
- }
- };
- }
- function onDelete(state, { route, id }) {
- const routeDocs = getRouteDocs(state, route);
- const index = routeDocs.findIndex(({ id: docId }) => docId === id);
- return {
- [route]: {
- ...state[route],
- error: false,
- items: updateDoc(routeDocs, index, id, true)
- }
- };
- }
- function onDeleteResponse(state, { route, id, err }) {
- const routeDocs = getRouteDocs(state, route);
- const index = routeDocs.findIndex(({ id: docId }) => docId === id);
- if (err) {
- return {
- [route]: {
- ...state[route],
- error: true,
- items: updateDoc(routeDocs, index, id, false)
- }
- };
- }
- return {
- [route]: {
- ...state[route],
- error: false,
- items: deleteDoc(routeDocs, index)
- }
- };
- }
- const reducerMap = {
- [DOC_CREATED]: onCreate,
- [DOC_CREATE_RESPONDED]: onCreateResponse,
- [DOC_READ]: onRead,
- [DOC_READ_RESPONDED]: onReadResponse,
- [DOC_UPDATED]: onUpdate,
- [DOC_UPDATE_RESPONDED]: onUpdateResponse,
- [DOC_DELETED]: onDelete,
- [DOC_DELETE_RESPONDED]: onDeleteResponse
- };
- export const crud = createReducerObject(reducerMap, initialState);
|