public.ts 1.6 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1 2 3 4 5
import update from 'immutability-helper';

import { IReducerAction } from 'model/common.model';

import FilteredPage from 'model/FilteredPage';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
6
import { APPEND_INSTITUTES, RECEIVE_INSTITUTE } from 'institutes/constants';
7
import FaoInstitute from 'model/genesys/FaoInstitute';
Matija Obreza's avatar
Matija Obreza committed
8
import InstituteDetails from 'model/genesys/InstituteDetails';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
9
import ApiCall from 'model/ApiCall';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
10 11

const INITIAL_STATE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
12 13
  institute: ApiCall<InstituteDetails>;
  paged: ApiCall<FilteredPage<FaoInstitute>>;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
14
} = {
Matija Obreza's avatar
Matija Obreza committed
15 16
  institute: null,
  paged: null,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
17 18
};

19
function publicInstitutes(state = INITIAL_STATE, action: IReducerAction) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
20

Matija Obreza's avatar
Matija Obreza committed
21
  switch (action.type) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
22

Matija Obreza's avatar
Matija Obreza committed
23
    case RECEIVE_INSTITUTE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
24 25
      const {apiCall} = action.payload;
      const receivedIndex = state.paged && state.paged.data && apiCall.data ? state.paged.data.content.findIndex((item) => item.code === apiCall.data.code) : -1;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
26

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40
      if (receivedIndex !== -1) {
        return update(state, {
          institute: {$set: apiCall},
          paged: {
            content: {
              [receivedIndex]: {$set: apiCall.data},
            },
          },
        });
      } else {
        return update(state, {
          institute: {$set: apiCall},
        });
      }
Matija Obreza's avatar
Matija Obreza committed
41
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
42

Matija Obreza's avatar
Matija Obreza committed
43
    case APPEND_INSTITUTES: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
44 45 46 47 48 49 50 51
      const {apiCall: {loading, error, timestamp, data}} = action.payload;
      return update(state, {
        paged: {
          $set: {
            loading,
            error,
            timestamp,
            data: FilteredPage.merge(state.paged && state.paged.data, data),
Matija Obreza's avatar
Matija Obreza committed
52
          },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
53 54
        },
      });
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
55
    }
Matija Obreza's avatar
Matija Obreza committed
56 57 58 59

    default:
      return state;
  }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
60 61
}

62
export default publicInstitutes;