dashboard.ts 5.58 KB
Newer Older
1
import update from 'immutability-helper';
2
import { log } from 'utilities/debug';
3
4
5
import * as _ from 'lodash';

import {
6
7
8
9
10
11
12
13
14
15
16
  CREATE_DATASET,
  ADD_CREATOR_TO_DATASET,
  REMOVE_CREATOR_FROM_DATASET,
  UPDATE_DATASET_CREATOR,
  ADD_LOCATION,
  RECEIVE_LOCATION,
  REMOVE_LOCATION,
  DASHBOARD_REMOVE_DATASET,
  DASHBOARD_RECEIVE_DATASET_PAGE,
  RECEIVE_DATASET,
  DASHBOARD_APPEND_DATASET_PAGE,
17
18
} from 'datasets/constants';

19
import Dataset from 'model/catalog/Dataset';
20
import { Page } from 'model/common.model';
21
22

const INITIAL_STATE: {
23
24
25
  dataset: Dataset,
  paged: Page<Dataset>,
  pagedQuery: any,
26
} = {
27
28
29
  dataset: null,
  paged: null,
  pagedQuery: null,
30
31
32
33
};

function datasetsDashboard(state = INITIAL_STATE, action: { type?: string, payload?: any } = { type: '', payload: {} }) {

34
35
36
37
38
39
40
  switch (action.type) {
    case CREATE_DATASET: {
      const dataset: Dataset = new Dataset();
      dataset.versionTag = '1.0';
      return update(state, {
        dataset: { $set: dataset },
      });
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    case RECEIVE_DATASET: {
      const receivedIndex = state.paged ? _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid) : -1;

      if (receivedIndex !== -1) {
        return update(state, {
          dataset: { $set: action.payload },
          paged: {
            content: {
              [receivedIndex]: { $set: action.payload },
            },
          },
        });
      } else {
        return update(state, {
          dataset: { $set: action.payload },
          paged: { $set: null },
        });
      }
    }

    case DASHBOARD_REMOVE_DATASET: {
      if (state.paged) {
        const removeIndex = _.findIndex(state.paged.content, (item) => item.uuid === action.payload.uuid);
        return removeIndex === -1 ? state
          : update(state, {
            paged: {
              content: { $splice: [[removeIndex, 1]] },
              numberOfElements: { $set: state.paged.numberOfElements - 1 },
              totalElements: { $set: state.paged.totalElements - 1 },
            },
          });
      }
      return state;
    }
    case DASHBOARD_RECEIVE_DATASET_PAGE: {
      return update(state, {
        paged: { $set: action.payload.paged },
        pagedQuery: { $set: action.payload.query },
      });
    }
    case DASHBOARD_APPEND_DATASET_PAGE: {
      const { paged, query } = action.payload;

      return !state.paged ? update(state, {
        paged: { $set: paged },
        pagedQuery: { $set: query },
      }) :
        update(state, {
          paged: {
            content: { $push: paged.content },
            number: { $set: paged.number },
            last: { $set: paged.last },
          },
          pagedQuery: { $set: query },
        });
    }

    // Creators step
    case ADD_CREATOR_TO_DATASET: {
      log('Payload ADD_CREATOR_TO_DATASET', action);
      if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) {
        return update(state, {
104
105
106
107
          dataset: {
            version: { $set: state.dataset.version + 1 },
            creators: { $push: [action.payload.creator] },
          },
108
109
110
111
112
113
114
115
116
117
        });
      } else {
        return state;
      }
    }

    case REMOVE_CREATOR_FROM_DATASET: {
      log('Payload REMOVE_CREATOR_FROM_DATASET', action);
      if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) {
        return update(state, {
118
119
120
121
          dataset: {
            version: { $set: state.dataset.version + 1 },
            creators: { $apply: (creators) => creators.filter((creator) => creator.uuid !== action.payload.creator.uuid) },
          },
122
123
124
125
126
127
128
129
130
131
132
        });
      } else {
        return state;
      }
    }

    case UPDATE_DATASET_CREATOR: {
      log('Payload UPDATE_DATASET_CREATOR', action);
      if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) {
        const index = state.dataset.creators.findIndex((creator) => creator.uuid === action.payload.creator.uuid);
        return update(state, {
133
134
135
136
          dataset: {
            version: { $set: state.dataset.version + 1 },
            creators: { [index]: { $set: action.payload.creator } },
          },
137
138
139
140
141
142
143
144
145
146
147
        });
      } else {
        return state;
      }
    }

    // Location step
    case ADD_LOCATION: {
      log('Payload ADD_LOCATION', action);
      if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) {
        return update(state, {
148
149
150
151
          dataset: {
            version: { $set: state.dataset.version + 1 },
            locations: { $push: [action.payload.location] },
          },
152
153
154
155
156
157
158
159
160
161
        });
      } else {
        return state;
      }
    }

    case REMOVE_LOCATION: {
      log('Payload REMOVE_LOCATION', action);
      if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) {
        return update(state, {
162
163
164
165
          dataset: {
            version: { $set: state.dataset.version + 1 },
            locations: { $apply: (locations) => locations.filter((location) => location.uuid !== action.payload.location.uuid) },
          },
166
167
168
169
170
171
172
173
174
175
176
        });
      } else {
        return state;
      }
    }

    case RECEIVE_LOCATION: {
      log('Payload RECEIVE_LOCATION', action);
      if (state.dataset) { // && action.payload.datasetUUID === state.dataset.uuid) {
        const index = state.dataset.locations.findIndex((location) => location.uuid === action.payload.location.uuid);
        return update(state, {
177
178
179
180
          dataset: {
            version: { $set: state.dataset.version + 1 },
            locations: { [index]: { $set: action.payload.location } },
          },
181
182
183
184
185
186
187
188
189
        });
      } else {
        return state;
      }
    }

    default:
      return state;
  }
190
191
192
}

export default datasetsDashboard;