search.reducer.ts 5.99 KB
Newer Older
François Agneray's avatar
François Agneray committed
1
2
import * as actions from './search.action';

François Agneray's avatar
François Agneray committed
3
import { Criterion, SearchMeta } from './model';
François Agneray's avatar
François Agneray committed
4
5

export interface State {
François Agneray's avatar
François Agneray committed
6
    pristine: boolean;
François Agneray's avatar
François Agneray committed
7
8
    currentStep: string;
    datasetName: string;
François Agneray's avatar
François Agneray committed
9
10
11
    criteriaStepChecked: boolean;
    outputStepChecked: boolean;
    resultStepChecked: boolean;
François Agneray's avatar
François Agneray committed
12
13
    criteriaList: Criterion[];
    outputList: number[];
Tifenn Guillas's avatar
Tifenn Guillas committed
14
15
    searchMetaIsLoading: boolean;
    searchMetaIsLoaded: boolean;
François Agneray's avatar
François Agneray committed
16
17
    searchMeta: SearchMeta;
    searchData: any[];
François Agneray's avatar
François Agneray committed
18
    selectedData: any[];
Tifenn Guillas's avatar
DONE    
Tifenn Guillas committed
19
20
21
    processWip: boolean;
    processDone: boolean;
    processId: string;
François Agneray's avatar
François Agneray committed
22
23
24
}

const initialState: State = {
François Agneray's avatar
François Agneray committed
25
    pristine: true,
François Agneray's avatar
François Agneray committed
26
27
    currentStep: null,
    datasetName: null,
François Agneray's avatar
François Agneray committed
28
29
30
    criteriaStepChecked: false,
    outputStepChecked: false,
    resultStepChecked: false,
François Agneray's avatar
François Agneray committed
31
    criteriaList: [],
François Agneray's avatar
François Agneray committed
32
    outputList: [],
Tifenn Guillas's avatar
Tifenn Guillas committed
33
34
    searchMetaIsLoading: false,
    searchMetaIsLoaded: false,
François Agneray's avatar
François Agneray committed
35
    searchMeta: null,
François Agneray's avatar
François Agneray committed
36
    searchData: null,
Tifenn Guillas's avatar
DONE    
Tifenn Guillas committed
37
38
39
40
    selectedData: [],
    processWip: false,
    processDone: false,
    processId: null
François Agneray's avatar
François Agneray committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
};

export function reducer(state: State = initialState, action: actions.Actions): State {
    switch (action.type) {
        case actions.CHANGE_STEP:
            const currentStep = action.payload as string;

            return {
                ...state,
                currentStep
            };

        case actions.SELECT_DATASET:
            const datasetName = action.payload as string;

            return {
                ...state,
François Agneray's avatar
François Agneray committed
58
                pristine: false,
François Agneray's avatar
François Agneray committed
59
60
61
                datasetName
            };

François Agneray's avatar
François Agneray committed
62
        case actions.NEW_SEARCH:
François Agneray's avatar
François Agneray committed
63
64
65
66
            return {
                ...initialState,
                currentStep: 'dataset'
            };
François Agneray's avatar
François Agneray committed
67

François Agneray's avatar
François Agneray committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        case actions.CRITERIA_CHECKED:
            return {
                ...state,
                criteriaStepChecked: true
            };

        case actions.OUTPUT_CHECKED:
            return {
                ...state,
                outputStepChecked: true
            };

        case actions.RESULT_CHECKED:
            return {
                ...state,
                resultStepChecked: true
            };

François Agneray's avatar
François Agneray committed
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
        case actions.UPDATE_CRITERIA_LIST:
            const criteriaList = action.payload as Criterion[];

            return {
                ...state,
                criteriaList
            };

        case actions.ADD_CRITERION:
            const criterion = action.payload as Criterion;

            return {
                ...state,
                criteriaList: [...state.criteriaList, criterion]
            };

        case actions.DELETE_CRITERION:
            const id = action.payload as number;

            return {
                ...state,
                criteriaList: [...state.criteriaList.filter(c => c.id !== id)]
            };

        case actions.UPDATE_OUTPUT_LIST:
            const outputList = action.payload as number[];

            return {
                ...state,
                outputList
            };

Tifenn Guillas's avatar
Tifenn Guillas committed
118
119
120
121
122
123
        case actions.RETRIEVE_META:
            return {
                ...state,
                searchMetaIsLoading: true
            };

François Agneray's avatar
François Agneray committed
124
125
126
127
128
        case actions.RETRIEVE_META_SUCCESS:
            const searchMeta = action.payload as SearchMeta;

            return {
                ...state,
Tifenn Guillas's avatar
Tifenn Guillas committed
129
130
131
132
133
134
135
136
137
138
139
                searchMeta,
                searchMetaIsLoading: false,
                searchMetaIsLoaded: true
            };

        case actions.RETRIEVE_META_FAIL:
            return {
                ...state,
                searchMeta,
                searchMetaIsLoading: false,
                searchMetaIsLoaded: false
140
            };
François Agneray's avatar
François Agneray committed
141
142
143
144
145
146
147

        case actions.RETRIEVE_DATA_SUCCESS:
            const searchData = action.payload as any[];

            return {
                ...state,
                searchData
148
149
            };

François Agneray's avatar
François Agneray committed
150
        case actions.ADD_SELECTED_DATA:
Tifenn Guillas's avatar
Tifenn Guillas committed
151
            const addData = action.payload;
François Agneray's avatar
François Agneray committed
152
153
154
155

            return {
                ...state,
                selectedData: [...state.selectedData, addData]
Tifenn Guillas's avatar
DONE    
Tifenn Guillas committed
156
            };
François Agneray's avatar
François Agneray committed
157
158

        case actions.DELETE_SELECTED_DATA:
Tifenn Guillas's avatar
Tifenn Guillas committed
159
            const deleteData = action.payload;
François Agneray's avatar
François Agneray committed
160
161
162
163

            return {
                ...state,
                selectedData: [...state.selectedData.filter(d => d !== deleteData)]
Tifenn Guillas's avatar
DONE    
Tifenn Guillas committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
            };

        case actions.EXECUTE_PROCESS:
            return {
                ...state,
                processWip: true,
                processDone: false
            };

        case actions.EXECUTE_PROCESS_WIP:
            const processId = action.payload as string;

            return {
                ...state,
                processId
            };

        case actions.EXECUTE_PROCESS_SUCCESS:
            return {
                ...state,
                processWip: false,
                processDone: true
            };

        case actions.EXECUTE_PROCESS_FAIL:
            return {
                ...state,
                processWip: false,
                processDone: false,
                processId: null
            };
François Agneray's avatar
François Agneray committed
195

François Agneray's avatar
François Agneray committed
196
197
198
199
200
        default:
            return state;
    }
}

François Agneray's avatar
François Agneray committed
201
export const getPristine = (state: State) => state.pristine;
François Agneray's avatar
François Agneray committed
202
203
export const getCurrentStep = (state: State) => state.currentStep;
export const getDatasetName = (state: State) => state.datasetName;
François Agneray's avatar
François Agneray committed
204
205
206
export const getCriteriaStepChecked = (state: State) => state.criteriaStepChecked;
export const getOutputStepChecked = (state: State) => state.outputStepChecked;
export const getResultStepChecked = (state: State) => state.resultStepChecked;
François Agneray's avatar
François Agneray committed
207
208
export const getCriteriaList = (state: State) => state.criteriaList;
export const getOutputList = (state: State) => state.outputList;
Tifenn Guillas's avatar
Tifenn Guillas committed
209
210
export const getSearchMetaIsLoading = (state: State) => state.searchMetaIsLoading;
export const getSearchMetaIsLoaded = (state: State) => state.searchMetaIsLoaded;
François Agneray's avatar
François Agneray committed
211
212
export const getSearchMeta = (state: State) => state.searchMeta;
export const getSearchData = (state: State) => state.searchData;
François Agneray's avatar
François Agneray committed
213
export const getSelectedData = (state: State) => state.selectedData;
Tifenn Guillas's avatar
DONE    
Tifenn Guillas committed
214
215
216
export const getProcessWip = (state: State) => state.processWip;
export const getProcessDone = (state: State) => state.processDone;
export const getProcessId = (state: State) => state.processId;