[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / attachments / setup / HeatSetupReducer.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 import {actionTypes} from './HeatSetupConstants.js';
17 import differenceWith from 'lodash/differenceWith.js';
18
19
20 const emptyModule = (isBase, currentLength) => ({
21         name: `${isBase ? 'base_' : 'module_'}${currentLength + 1}`,
22         isBase: isBase
23 });
24
25 function syncUnassignedFilesWithArtifactsChanges(unassigned, artifacts, oldArtifacts) {
26         if (artifacts.length > oldArtifacts.length) {
27                 return differenceWith(unassigned, artifacts, (unassignedFile, artifact) => unassignedFile === artifact);
28         }
29         else {
30                 const removedArtifact = differenceWith(oldArtifacts, artifacts, (oldArtifact, artifact) => artifact === oldArtifact);
31                 return [...unassigned, removedArtifact[0]];
32         }
33 }
34
35 function findModuleIndexByName(modules, name) {
36         return modules.findIndex(module => module.name === name);
37 }
38
39 function addDeletedModuleFilesToUnassigned(unassigned, deletedModule){
40         let files = [];
41         for(let i in deletedModule){
42                 if (deletedModule.hasOwnProperty(i)) {
43                         if (typeof deletedModule[i] === 'string' && deletedModule[i] && i !== 'name') {
44                                 files.push(deletedModule[i]);
45                         }
46                 }
47         }
48
49         return unassigned.concat(files);
50 }
51
52 export default (state = {}, action) => {
53         switch (action.type) {
54                 case actionTypes.MANIFEST_LOADED:
55                         return {
56                                 ...state,
57                                 ...action.response,
58                                 modules: action.response.modules.map(module => ({...module, name: module.name || module.yaml.substring(0, module.yaml.lastIndexOf('.'))}))
59                         };
60                 case actionTypes.ARTIFACT_LIST_CHANGE:
61                         return {
62                                 ...state,
63                                 artifacts: action.data.artifacts,
64                                 unassigned: syncUnassignedFilesWithArtifactsChanges(state.unassigned, action.data.artifacts, state.artifacts)
65                         };
66                 case actionTypes.ADD_ALL_UNASSIGNED_TO_ARTIFACTS:
67                         return {
68                                 ...state,
69                                 artifacts: [...state.artifacts,...state.unassigned],
70                                 unassigned: []
71                         };
72                 case actionTypes.ADD_ALL_ARTIFACTS_TO_UNASSIGNED:
73                         return {
74                                 ...state,
75                                 artifacts: [],
76                                 unassigned: [...state.unassigned, ...state.artifacts]
77                         };
78                 case actionTypes.ADD_MODULE:
79                         return {
80                                 ...state,
81                                 modules: state.modules.concat({...emptyModule(action.data.isBase, state.modules.length)})
82                         };
83                 case actionTypes.REMOVE_MODULE:
84                         const moduleIndexToDelete = findModuleIndexByName(state.modules, action.data.moduleName);
85                         let unassigned = addDeletedModuleFilesToUnassigned(state.unassigned, state.modules[moduleIndexToDelete]);
86                         return {
87                                 ...state,
88                                 unassigned,
89                                 modules: [...state.modules.slice(0, moduleIndexToDelete), ...state.modules.slice(moduleIndexToDelete + 1)]
90                         };
91                 case actionTypes.RENAME_MODULE:
92                         const indexToRename = findModuleIndexByName(state.modules, action.data.oldName);
93                         let moduleToRename = state.modules[indexToRename];
94                         moduleToRename.name = action.data.newName;
95                         return {
96                                 ...state,
97                                 modules: [...state.modules.slice(0, indexToRename), moduleToRename, ...state.modules.slice(indexToRename + 1)]
98                         };
99                 case actionTypes.FILE_ASSIGN_CHANGED:
100                         let {module, value:{value}, type} = action.data;
101                         const moduleIndexToModify = findModuleIndexByName(state.modules, module.name);
102                         let moduleToModify = state.modules[moduleIndexToModify];
103                         let dumpedFile = moduleToModify[type];
104                         if (dumpedFile !== value) {
105                                 if(value) {
106                                         moduleToModify[type] = value;
107                                 }
108                                 else {
109                                         delete moduleToModify[type];
110                                 }
111                                 const newUnassignedList = dumpedFile ? [...state.unassigned.filter(file => file !== value), dumpedFile] : state.unassigned.filter(file => file !== value);
112                                 return {
113                                         ...state,
114                                         modules: [...state.modules.slice(0, moduleIndexToModify), moduleToModify, ...state.modules.slice(moduleIndexToModify + 1)],
115                                         unassigned: newUnassignedList
116                                 };
117                         }
118                         else {
119                                 return state;
120                         }
121                 default:
122                         return state;
123         }
124 };