dashboard.ts 4.43 KB
Newer Older
Matija Obreza's avatar
Subsets    
Matija Obreza committed
1
2
3
import update from 'immutability-helper';

import { IReducerAction } from 'model/common.model';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
4
5
import {DASHBOARD_RECEIVE_SUBSET, DASHBOARD_REMOVE_SUBSET, DASHBOARD_APPEND_SUBSETS, ADD_CREATOR_TO_SUBSET,
  REMOVE_CREATOR_FROM_SUBSET, UPDATE_SUBSET_CREATOR, DASHBOARD_APPEND_ACCESSIONS} from 'subsets/constants';
6
import {LOGOUT} from 'constants/login';
Matija Obreza's avatar
Subsets    
Matija Obreza committed
7
8

import FilteredPage from 'model/FilteredPage';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
9
import ApiCall from 'model/ApiCall';
10
import Subset from 'model/subset/Subset';
11
12
import Page from 'model/Page';
import {AccessionRef} from 'model/accession/AccessionRef';
Matija Obreza's avatar
Subsets    
Matija Obreza committed
13

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
14
15
import * as _ from 'lodash';

Matija Obreza's avatar
Subsets    
Matija Obreza committed
16
const INITIAL_STATE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
17
18
19
  accessionRefs: ApiCall<Page<AccessionRef>>;
  subset: ApiCall<Subset>;
  paged: ApiCall<FilteredPage<Subset>>;
Matija Obreza's avatar
Subsets    
Matija Obreza committed
20
} = {
21
  accessionRefs: null,
Matija Obreza's avatar
Subsets    
Matija Obreza committed
22
23
24
25
  subset: null,
  paged: null,
};

26
function dashboardSubsets(state = INITIAL_STATE, action: IReducerAction) {
Matija Obreza's avatar
Subsets    
Matija Obreza committed
27
28

  switch (action.type) {
29
30
31
    case LOGOUT: {
      return update(state, { $set: INITIAL_STATE });
    }
Matija Obreza's avatar
Subsets    
Matija Obreza committed
32

33
    case DASHBOARD_RECEIVE_SUBSET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
34
35
36
37
      const {apiCall} = action.payload;
      const receivedIndex = state.paged && state.paged.data && apiCall.data ? _.findIndex(state.paged.data.content, (item) => item.uuid === apiCall.data.uuid) : -1;
      const mustRemoveAccessions = state.subset && state.subset.data && apiCall.data && (apiCall.data.uuid !== state.subset.data.uuid);
      apiCall.data = apiCall.loading && state.subset ? state.subset.data : apiCall.data;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
38
39
40

      if (receivedIndex !== -1) {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
41
          subset: {$set: apiCall},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
42
          paged: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
43
44
45
46
            data: {
              content: {
                [receivedIndex]: {$set: apiCall.data},
              },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
47
48
            },
          },
49
          accessionRefs: { $set: mustRemoveAccessions ? null : state.accessionRefs },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
50
51
52
        });
      } else {
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
53
          subset: {$set: apiCall},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
54
          paged: {$set: null},
55
          accessionRefs: {$set: null},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
56
57
        });
      }
Matija Obreza's avatar
Subsets    
Matija Obreza committed
58
59
    }

60
    case DASHBOARD_APPEND_ACCESSIONS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
61
      const {apiCall: {loading, error, timestamp, data}} = action.payload;
62

Matija Obreza's avatar
Subsets    
Matija Obreza committed
63
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
64
65
66
67
68
        accessionRefs: {
          $set: {
            loading,
            error,
            timestamp,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
69
            data: Page.merge(state.accessionRefs && state.accessionRefs.data, data),
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
70
71
          },
        },
Matija Obreza's avatar
Subsets    
Matija Obreza committed
72
73
74
      });
    }

75
    case DASHBOARD_APPEND_SUBSETS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
76
77
78
79
80
81
82
83
      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
84
          },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
85
86
        },
      });
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
87
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
88

89
    case DASHBOARD_REMOVE_SUBSET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
      const {apiCall} = action.payload;
      if (state.paged) {
        const removeIndex = state.paged.data ? _.findIndex(state.paged.data.content, (item) => item.uuid === apiCall.data.uuid) : -1;
        return removeIndex === -1 ? state
          : update(state, {
            paged: {
              data: {
                content: {$splice: [[removeIndex, 1]]},
                numberOfElements: {$set: state.paged.data.number - 1},
                totalElements: {$set: state.paged.data.totalElements - 1},
              },
            },
          });
      }
      return state;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
105
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
106

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
107
    case ADD_CREATOR_TO_SUBSET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
108
109
      const {apiCall} = action.payload;
      if (state.subset && apiCall.data) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
110
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
111
          subset: { data: { creators: { $push: [ apiCall.data ] } } },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
112
113
114
115
116
117
118
        });
      } else {
        return state;
      }
    }

    case REMOVE_CREATOR_FROM_SUBSET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
119
120
      const {apiCall} = action.payload;
      if (state.subset && apiCall.data) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
121
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
122
          subset: { data: { creators: { $apply: (creators) => creators.filter((creator) => creator.uuid !== apiCall.data.uuid) } } },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
123
124
125
126
127
128
129
        });
      } else {
        return state;
      }
    }

    case UPDATE_SUBSET_CREATOR: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
130
131
132
      const {apiCall} = action.payload;
      if (state.subset && apiCall.data) { // && action.payload.datasetUUID === state.dataset.uuid) {
        const index = state.subset.data && state.subset.data.creators.findIndex((creator) => creator.uuid === apiCall.data.uuid);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
133
        return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
134
          subset: { data: { creators: { [index]: { $set: apiCall.data } } } },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
135
136
137
138
139
        });
      } else {
        return state;
      }
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
140

Matija Obreza's avatar
Subsets    
Matija Obreza committed
141
142
143
144
145
    default:
      return state;
  }
}

146
export default dashboardSubsets;