f63c12544b7e8da3660fd7f4e55fdc7f260f776b
[sdc/sdc-workflow-designer.git] /
1 /*
2 * Copyright © 2018 European Support Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 import {
18     INPUTS,
19     OUTPUTS,
20     STRING,
21     BOOLEAN,
22     INTEGER,
23     FLOAT,
24     SET_INPUTS_OUTPUTS,
25     CHANGE_ERROR,
26     SHOW_INPUTS,
27     SHOW_OUTPUTS,
28     SEARCH,
29     ADD,
30     CHANGE_NAME,
31     CHANGE_TYPE,
32     CHANGE_MANDATORY,
33     REMOVE
34 } from 'features/version/inputOutput/inputOutputConstants';
35
36 export const defaultInputOutput = {
37     [INPUTS]: {
38         name: '',
39         value: STRING,
40         mandatory: false
41     },
42     [OUTPUTS]: {
43         name: '',
44         value: STRING,
45         mandatory: false
46     }
47 };
48
49 export const initialState = {
50     current: INPUTS,
51     [INPUTS]: [],
52     [OUTPUTS]: [],
53     search: '',
54     types: [STRING, BOOLEAN, INTEGER, FLOAT],
55     error: {
56         [INPUTS]: {},
57         [OUTPUTS]: {}
58     }
59 };
60
61 const inputOutputReducer = (state = initialState, action) => {
62     const { type, payload } = action;
63     switch (type) {
64         case SET_INPUTS_OUTPUTS:
65             return {
66                 ...initialState,
67                 ...payload
68             };
69
70         case CHANGE_ERROR:
71             return {
72                 ...state,
73                 error: {
74                     ...state.error,
75                     [state.current]: payload
76                 }
77             };
78
79         case SHOW_INPUTS:
80             return { ...state, current: INPUTS };
81
82         case SHOW_OUTPUTS:
83             return { ...state, current: OUTPUTS };
84
85         case SEARCH:
86             return { ...state, search: payload };
87
88         case ADD:
89             return {
90                 ...state,
91                 [state.current]: [
92                     ...state[state.current],
93                     defaultInputOutput[state.current]
94                 ]
95             };
96
97         /* eslint-disable no-case-declarations */
98         case CHANGE_NAME:
99         case CHANGE_TYPE:
100         case CHANGE_MANDATORY:
101             const { key, ...rest } = payload;
102             return {
103                 ...state,
104                 [state.current]: state[state.current].map(
105                     (row, index) =>
106                         key === index
107                             ? {
108                                   ...row,
109                                   ...rest
110                               }
111                             : row
112                 )
113             };
114         /* eslint-enable no-case-declarations */
115
116         case REMOVE:
117             return {
118                 ...state,
119                 [state.current]: state[state.current].filter(
120                     (_, index) => index !== payload
121                 )
122             };
123
124         default:
125             return state;
126     }
127 };
128
129 export default inputOutputReducer;