public.ts 2.79 KB
Newer Older
1
2
3
import update from 'immutability-helper';

import { IReducerAction } from 'model/common.model';
4
import {RECEIVE_ACCESSIONS, RECEIVE_ACCESSION, RECEIVE_ACCESSION_OVERVIEW, APPEND_ACCESSIONS, RECEIVE_ACCESSION_MAPINFO} from 'accessions/constants';
5
6

import FilteredPage from 'model/FilteredPage';
7
8
9
import Accession from 'model/accession/Accession';
import AccessionMapInfo from 'model/accession/AccessionMapInfo';
import AccessionOverview from 'model/accession/AccessionOverview';
10
11
12
13
14
15

const INITIAL_STATE: {
  accession: Accession;
  accessionError: any;
  paged: FilteredPage<Accession>;
  pagedError: any;
16
  overview: AccessionOverview;
17
  mapInfo: AccessionMapInfo;
18
19
20
21
22
} = {
  accession: null,
  accessionError: null,
  paged: null,
  pagedError: null,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
23
  overview: null,
24
  mapInfo: null,
25
26
};

27
function publicAccessions(state = INITIAL_STATE, action: IReducerAction) {
28
29
30
31

  switch (action.type) {

    case RECEIVE_ACCESSION: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
      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},
        });
      }
52
53
54
55
56
57
58
    }

    case RECEIVE_ACCESSIONS: {
      const { paged, error } = action.payload;
      return update(state, {
        paged: { $set: paged },
        pagedError: { $set: error },
59
60
        overview: {$set: null},
        mapInfo: { $set: null },
61
62
63
      });
    }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
64
65
66
67
68
    case RECEIVE_ACCESSION_OVERVIEW: {
      const { overview, error } = action.payload;
      return update(state, {
        overview: { $set: overview },
        pagedError: { $set: error },
69
        paged: {$set: null},
70
        mapInfo: { $set: null },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
71
72
      });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

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

91
92
93
94
95
96
97
98
99
100
    case RECEIVE_ACCESSION_MAPINFO: {
      const { mapInfo, error } = action.payload;
      return update(state, {
        mapInfo: { $set: mapInfo },
        pagedError: { $set: error },
        paged: {$set: null},
        overview: {$set: null},
      });
    }

101
102
103
104
105
    default:
      return state;
  }
}

106
export default publicAccessions;