-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathaotfReducer.ts
More file actions
157 lines (148 loc) · 4.49 KB
/
aotfReducer.ts
File metadata and controls
157 lines (148 loc) · 4.49 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { DocumentNode } from "graphql";
import {
defaulDatasourcesWeigths,
DEFAULT_TABLE_PAGINATION_STATE,
DEFAULT_TABLE_SORTING_STATE,
defaultDataSourcesFilter,
} from "../utils";
import { Action, ActionType, ENTITY, State, TABLE_VIEW } from "../types";
import { isEqual } from "lodash";
/*****************
* INITIAL STATE *
*****************/
export const initialState: State = {
pagination: DEFAULT_TABLE_PAGINATION_STATE,
loading: false,
query: null,
parentId: "",
enableIndirect: false,
sorting: DEFAULT_TABLE_SORTING_STATE,
parentEntity: null, // TODO: review initial state
rowEntity: null,
tableView: TABLE_VIEW.MAIN,
isMainView: true,
searchFilter: "",
advanceOptionsOpen: false,
pinnedEntities: [],
bodyData: [],
pinnedData: [],
interactors: new Map(),
dataSourceControls: defaulDatasourcesWeigths,
modifiedSourcesDataControls: false,
facetFilters: [],
dataSourcesFilter: defaultDataSourcesFilter,
};
type InitialStateParams = {
parentEntity: ENTITY;
parentId: string;
query: DocumentNode;
};
export function createInitialState({ parentEntity, parentId, query }: InitialStateParams): State {
const rowEntity = parentEntity === ENTITY.TARGET ? ENTITY.DISEASE : ENTITY.TARGET;
const state = { ...initialState, query, parentId, parentEntity, rowEntity };
return state;
}
export function aotfReducer(state: State = initialState, action: Action): State {
if (typeof state === undefined) {
throw Error("State provied to aotfReducer is undefined");
}
switch (action.type) {
case ActionType.PAGINATE: {
return {
...state,
pagination: action.pagination,
};
}
case ActionType.RESET_PAGINATION: {
return {
...state,
pagination: DEFAULT_TABLE_PAGINATION_STATE,
};
}
case ActionType.SORTING: {
return {
...state,
sorting: action.sorting,
};
}
case ActionType.SET_INTERACTORS: {
const currentInteractors = state.interactors;
if (typeof currentInteractors === "undefined" || !currentInteractors) return { ...state };
const payloadInteractor = action.payload;
// Todo: review
if (currentInteractors.has(payloadInteractor.id)) {
const row = currentInteractors.get(payloadInteractor.id);
row?.push(payloadInteractor.source);
currentInteractors.set(payloadInteractor.id, row);
}
currentInteractors.set(payloadInteractor.id, [payloadInteractor.source]);
return {
...state,
interactors: currentInteractors,
};
}
case ActionType.DATA_SOURCE_CONTROL: {
const colUpdatedControls = { ...action.payload };
const dataSourceControls = state.dataSourceControls.map(col => {
if (col.id === colUpdatedControls.id) return colUpdatedControls;
return col;
});
const modifiedSourcesDataControls = !isEqual(defaulDatasourcesWeigths, dataSourceControls);
return {
...state,
dataSourceControls,
modifiedSourcesDataControls,
pagination: DEFAULT_TABLE_PAGINATION_STATE,
};
}
case ActionType.RESET_DATA_SOURCE_CONTROL: {
return {
...state,
dataSourceControls: defaulDatasourcesWeigths,
modifiedSourcesDataControls: false,
pagination: DEFAULT_TABLE_PAGINATION_STATE,
};
}
case ActionType.HANDLE_AGGREGATION_CLICK: {
const aggregation = action.aggregation;
const isAllActive = state.dataSourceControls
.filter(el => el.aggregation === aggregation)
.every(el => el.required === true);
const dataSourceControls = state.dataSourceControls.map(col => {
if (col.aggregation === aggregation) {
return {
...col,
required: !isAllActive,
};
}
return col;
});
return {
...state,
dataSourceControls,
modifiedSourcesDataControls: !isAllActive,
pagination: DEFAULT_TABLE_PAGINATION_STATE,
};
}
case ActionType.FACETS_SEARCH: {
return {
...state,
facetFilters: action.facetFilters,
pagination: DEFAULT_TABLE_PAGINATION_STATE,
};
}
case ActionType.SET_INITIAL_STATE: {
return {
...initialState,
};
}
case ActionType.HANDLE_DATA_SOURCES_FILTER: {
const newDataSourcesFilter = action.newDataSourcesFilter;
return { ...state, dataSourcesFilter: newDataSourcesFilter };
}
default: {
throw Error("Unknown action: " + action);
return state;
}
}
}