dashboard.ts 1.71 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 33 34 35
                },
              },
            });
          } else {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
36
            return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
37
              institute: {$set: apiCall},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
38
            });
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
39
          }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
40 41
        }

42
        case DASHBOARD_APPEND_INSTITUTES: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
43 44 45 46 47 48 49 50
          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
51
              },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
52 53
            },
          });
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
54 55
        }

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

61
export default dashboardInstitutes;