react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / attachments / setup / HeatSetupReducer.js
1 /*
2  * Copyright © 2016-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 import { actionTypes } from './HeatSetupConstants.js';
17 import differenceWith from 'lodash/differenceWith.js';
18 import cloneDeep from 'lodash/cloneDeep';
19
20 const emptyModule = (isBase, currentLength) => ({
21     name: `${isBase ? 'base_' : 'module_'}${currentLength + 1}`,
22     isBase: isBase
23 });
24
25 function syncUnassignedFilesWithArtifactsChanges(
26     unassigned,
27     artifacts,
28     oldArtifacts
29 ) {
30     if (artifacts.length > oldArtifacts.length) {
31         return differenceWith(
32             unassigned,
33             artifacts,
34             (unassignedFile, artifact) => unassignedFile === artifact
35         );
36     } else {
37         const removedArtifact = differenceWith(
38             oldArtifacts,
39             artifacts,
40             (oldArtifact, artifact) => artifact === oldArtifact
41         );
42         return [...unassigned, removedArtifact[0]];
43     }
44 }
45
46 function findModuleIndexByName(modules, name) {
47     return modules.findIndex(module => module.name === name);
48 }
49
50 function addDeletedModuleFilesToUnassigned(unassigned, deletedModule) {
51     let files = [];
52     for (let i in deletedModule) {
53         if (deletedModule.hasOwnProperty(i)) {
54             if (
55                 typeof deletedModule[i] === 'string' &&
56                 deletedModule[i] &&
57                 i !== 'name'
58             ) {
59                 files.push(deletedModule[i]);
60             }
61         }
62     }
63
64     return unassigned.concat(files);
65 }
66
67 export default (state = {}, action) => {
68     switch (action.type) {
69         case actionTypes.TOGGLE_VOL_DISPLAY:
70             let clonedState = cloneDeep(state);
71             const indexToModify = findModuleIndexByName(
72                 clonedState.modules,
73                 action.data.module.name
74             );
75             let modToModify = clonedState.modules[indexToModify];
76             modToModify.showVolFiles = action.data.value;
77             return clonedState;
78         case actionTypes.MANIFEST_LOADED:
79             return {
80                 ...state,
81                 ...action.response,
82                 modules: action.response.modules.map(module => ({
83                     ...module,
84                     name:
85                         module.name ||
86                         module.yaml.substring(0, module.yaml.lastIndexOf('.'))
87                 }))
88             };
89         case actionTypes.ARTIFACT_LIST_CHANGE:
90             return {
91                 ...state,
92                 artifacts: action.data.artifacts,
93                 unassigned: syncUnassignedFilesWithArtifactsChanges(
94                     state.unassigned,
95                     action.data.artifacts,
96                     state.artifacts
97                 )
98             };
99         case actionTypes.ADD_ALL_UNASSIGNED_TO_ARTIFACTS:
100             return {
101                 ...state,
102                 artifacts: [...state.artifacts, ...state.unassigned],
103                 unassigned: []
104             };
105         case actionTypes.ADD_ALL_ARTIFACTS_TO_UNASSIGNED:
106             return {
107                 ...state,
108                 artifacts: [],
109                 unassigned: [...state.unassigned, ...state.artifacts]
110             };
111         case actionTypes.ADD_MODULE:
112             return {
113                 ...state,
114                 modules: state.modules.concat({
115                     ...emptyModule(action.data.isBase, state.modules.length)
116                 })
117             };
118         case actionTypes.REMOVE_MODULE:
119             const moduleIndexToDelete = findModuleIndexByName(
120                 state.modules,
121                 action.data.moduleName
122             );
123             let unassigned = addDeletedModuleFilesToUnassigned(
124                 state.unassigned,
125                 state.modules[moduleIndexToDelete]
126             );
127             return {
128                 ...state,
129                 unassigned,
130                 modules: [
131                     ...state.modules.slice(0, moduleIndexToDelete),
132                     ...state.modules.slice(moduleIndexToDelete + 1)
133                 ]
134             };
135         case actionTypes.RENAME_MODULE:
136             const indexToRename = findModuleIndexByName(
137                 state.modules,
138                 action.data.oldName
139             );
140             let moduleToRename = state.modules[indexToRename];
141             moduleToRename.name = action.data.newName;
142             return {
143                 ...state,
144                 modules: [
145                     ...state.modules.slice(0, indexToRename),
146                     moduleToRename,
147                     ...state.modules.slice(indexToRename + 1)
148                 ]
149             };
150         case actionTypes.FILE_ASSIGN_CHANGED:
151             let { module, value: { value }, type } = action.data;
152             const moduleIndexToModify = findModuleIndexByName(
153                 state.modules,
154                 module.name
155             );
156             let moduleToModify = state.modules[moduleIndexToModify];
157             let dumpedFile = moduleToModify[type];
158             if (dumpedFile !== value) {
159                 if (value) {
160                     moduleToModify[type] = value;
161                 } else {
162                     delete moduleToModify[type];
163                 }
164                 const newUnassignedList = dumpedFile
165                     ? [
166                           ...state.unassigned.filter(file => file !== value),
167                           dumpedFile
168                       ]
169                     : state.unassigned.filter(file => file !== value);
170                 return {
171                     ...state,
172                     modules: [
173                         ...state.modules.slice(0, moduleIndexToModify),
174                         moduleToModify,
175                         ...state.modules.slice(moduleIndexToModify + 1)
176                     ],
177                     unassigned: newUnassignedList
178                 };
179             } else {
180                 return state;
181             }
182         default:
183             return state;
184     }
185 };