public.ts 1.57 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';
Matija Obreza's avatar
Matija Obreza committed
6
import { APPEND_INSTITUTES, RECEIVE_INSTITUTE, RECEIVE_INSTITUTES } 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 10

const INITIAL_STATE: {
Matija Obreza's avatar
Matija Obreza committed
11 12 13 14
  institute: InstituteDetails;
  instituteError: any;
  paged: FilteredPage<FaoInstitute>;
  pagedError: any;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
15
} = {
Matija Obreza's avatar
Matija Obreza committed
16 17 18 19
  institute: null,
  instituteError: null,
  paged: null,
  pagedError: null,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
20 21
};

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

Matija Obreza's avatar
Matija Obreza committed
24
  switch (action.type) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
25

Matija Obreza's avatar
Matija Obreza committed
26 27
    case RECEIVE_INSTITUTE: {
      const { institute, error } = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
28

Matija Obreza's avatar
Matija Obreza committed
29 30 31 32 33
      return update(state, {
        institute: { $set: institute },
        instituteError: { $set: error },
      });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
34

Matija Obreza's avatar
Matija Obreza committed
35 36 37 38 39 40 41
    case RECEIVE_INSTITUTES: {
      const { paged, error } = action.payload;
      return update(state, {
        paged: { $set: paged },
        pagedError: { $set: error },
      });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
42

Matija Obreza's avatar
Matija Obreza committed
43 44
    case APPEND_INSTITUTES: {
      const { paged, error } = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
45

Matija Obreza's avatar
Matija Obreza committed
46 47 48 49 50 51 52 53 54 55 56 57
      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
58
    }
Matija Obreza's avatar
Matija Obreza committed
59 60 61 62

    default:
      return state;
  }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
63 64
}

65
export default publicInstitutes;