dashboard.ts 2.21 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';
6
import {DASHBOARD_APPEND_INSTITUTES, DASHBOARD_RECEIVE_INSTITUTE, DASHBOARD_RECEIVE_INSTITUTES} from 'institutes/constants';
7
import FaoInstitute from 'model/genesys/FaoInstitute';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
8 9 10 11 12 13 14 15 16 17 18 19 20

const INITIAL_STATE: {
    institute: FaoInstitute;
    instituteError: any;
    paged: FilteredPage<FaoInstitute>;
    pagedError: any;
} = {
    institute: null,
    instituteError: null,
    paged: null,
    pagedError: null,
};

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

    switch (action.type) {

25
        case DASHBOARD_RECEIVE_INSTITUTE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39
          const { institute, error } = action.payload;
          const receivedIndex = state.paged ? state.paged.content.findIndex((item) => item.code === institute.code) : -1;

          if (receivedIndex !== -1) {
            return update(state, {
              institute: { $set: institute },
              paged: {
                content: {
                  [receivedIndex]: {$set: institute},
                },
              },
              instituteError: {$set: error},
            });
          } else {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
40
            return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
41 42 43
              institute: { $set: institute},
              paged: {$set: null},
              instituteError: {$set: error},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
44
            });
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
45
          }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
46 47
        }

48
        case DASHBOARD_RECEIVE_INSTITUTES: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
49 50 51 52 53 54 55
            const { paged, error } = action.payload;
            return update(state, {
                paged: { $set: paged },
                pagedError: { $set: error },
            });
        }

56
        case DASHBOARD_APPEND_INSTITUTES: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
          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
73 74 75 76 77
        default:
            return state;
    }
}

78
export default dashboardInstitutes;