dashboard.ts 1.75 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 {DASHBOARD_APPEND_INSTITUTES, DASHBOARD_RECEIVE_INSTITUTE} from 'institutes/constants';
7
import FaoInstitute from 'model/genesys/FaoInstitute';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
8
import ApiCall from 'model/ApiCall';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
9 10

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

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

    switch (action.type) {

22
        case DASHBOARD_RECEIVE_INSTITUTE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
23 24
          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
25 26 27

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

43
        case DASHBOARD_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),
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
52
              },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
53 54
            },
          });
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
55 56
        }

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

62
export default dashboardInstitutes;