dashboard.ts 2.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import update from 'immutability-helper';

import { IReducerAction } from 'model/common.model';
import {DASHBOARD_RECEIVE_ACCESSIONS, DASHBOARD_RECEIVE_ACCESSION, DASHBOARD_APPEND_ACCESSIONS} from 'accessions/constants';

import FilteredPage from 'model/FilteredPage';
import Accession from 'model/accession/Accession';

const INITIAL_STATE: {
  accession: Accession;
  accessionError: any;
  paged: FilteredPage<Accession>;
  pagedError: any;
} = {
  accession: null,
  accessionError: null,
  paged: null,
  pagedError: null,
};

function dashboardAccessions(state = INITIAL_STATE, action: IReducerAction) {

  switch (action.type) {

    case DASHBOARD_RECEIVE_ACCESSION: {
      const {accession, error} = action.payload;
      const receivedIndex = state.paged ? state.paged.content.findIndex((item) => item.uuid === accession.uuid) : -1;

      if (receivedIndex !== -1) {
        return update(state, {
          accession: { $set: accession },
          paged: {
            content: {
              [receivedIndex]: {$set: accession},
            },
          },
          accessionError: {$set: error},
        });
      } else {
        return update(state, {
          accession: { $set: accession},
          paged: {$set: null},
          accessionError: {$set: error},
        });
      }
    }

    case DASHBOARD_RECEIVE_ACCESSIONS: {
      const { paged, error } = action.payload;
      return update(state, {
        paged: { $set: paged },
        pagedError: { $set: error },
        overview: {$set: null},
        mapInfo: { $set: null },
      });
    }

    case DASHBOARD_APPEND_ACCESSIONS: {
      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},
        });
    }

    default:
      return state;
  }
}

export default dashboardAccessions;