public.ts 3.26 KB
Newer Older
Matija Obreza's avatar
Subsets  
Matija Obreza committed
1 2 3
import update from 'immutability-helper';

import { IReducerAction } from 'model/common.model';
4
import {RECEIVE_SUBSETS, RECEIVE_SUBSET, REMOVE_SUBSET, APPEND_SUBSETS, ADD_CREATOR_TO_SUBSET, REMOVE_CREATOR_FROM_SUBSET, UPDATE_SUBSET_CREATOR} from 'subsets/constants';
Matija Obreza's avatar
Subsets  
Matija Obreza committed
5 6 7 8

import FilteredPage from 'model/FilteredPage';
import Subset from 'model/Subset';

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
9 10
import * as _ from 'lodash';

Matija Obreza's avatar
Subsets  
Matija Obreza committed
11 12 13 14 15 16 17 18 19 20 21 22
const INITIAL_STATE: {
  subset: Subset;
  subsetError: any;
  paged: FilteredPage<Subset>;
  pagedError: any;
} = {
  subset: null,
  subsetError: null,
  paged: null,
  pagedError: null,
};

23
function publicSubsets(state = INITIAL_STATE, action: IReducerAction) {
Matija Obreza's avatar
Subsets  
Matija Obreza committed
24 25 26 27 28

  switch (action.type) {

    case RECEIVE_SUBSET: {
      const { subset, error } = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
      const receivedIndex = state.paged ? state.paged.content.findIndex((item) => item.uuid === subset.uuid) : -1;

      if (receivedIndex !== -1) {
        return update(state, {
          subset: { $set: subset },
          paged: {
            content: {
              [receivedIndex]: {$set: subset},
            },
          },
          subsetError: {$set: error},
        });
      } else {
        return update(state, {
          subset: { $set: subset},
          paged: {$set: null},
          subsetError: {$set: error},
        });
      }
Matija Obreza's avatar
Subsets  
Matija Obreza committed
48 49 50 51 52 53 54 55 56 57
    }

    case RECEIVE_SUBSETS: {
      const { paged, error } = action.payload;
      return update(state, {
        paged: { $set: paged },
        pagedError: { $set: error },
      });
    }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    case APPEND_SUBSETS: {
      const {paged, error} = action.payload;

      return !state.paged ? update(state, {
          paged: {$set: paged},
          pagedError: {$set: error},
        }) :
        update(state, {
          paged: {
            content: {$push: paged.content},
            number: {$set: paged.number},
            last: {$set: paged.last},
          },
          pagedError: {$set: error},
        });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
74 75 76 77 78 79 80 81 82 83 84
    case REMOVE_SUBSET: {
      const removeIndex = state.paged ? _.findIndex(state.paged.content, (item) => item.uuid === action.payload.subset.uuid) : -1;
      return removeIndex === -1 ? state
        : update(state, {
          paged: {
            content: {$splice: [[removeIndex, 1]]},
            numberOfElements: {$set: state.paged.numberOfElements - 1},
            totalElements: {$set: state.paged.totalElements - 1},
          },
        });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    case ADD_CREATOR_TO_SUBSET: {
      if (state.subset) {
        return update(state, {
          subset: { creators: { $push: [ action.payload.creator ] } },
        });
      } else {
        return state;
      }
    }

    case REMOVE_CREATOR_FROM_SUBSET: {
      if (state.subset) {
        return update(state, {
          subset: { creators: { $apply: (creators) => creators.filter((creator) => creator.uuid !== action.payload.creator.uuid) } },
        });
      } else {
        return state;
      }
    }

    case UPDATE_SUBSET_CREATOR: {
      if (state.subset) {
        const index = state.subset.creators.findIndex((creator) => creator.uuid === action.payload.creator.uuid);
        return update(state, {
          subset: { creators: { [index]: { $set: action.payload.creator } } },
        });
      } else {
        return state;
      }
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
115

Matija Obreza's avatar
Subsets  
Matija Obreza committed
116 117 118 119 120
    default:
      return state;
  }
}

121
export default publicSubsets;