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