2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 import { actionTypes } from './HeatSetupConstants.js';
17 import differenceWith from 'lodash/differenceWith.js';
18 import cloneDeep from 'lodash/cloneDeep';
20 const emptyModule = (isBase, currentLength) => ({
21 name: `${isBase ? 'base_' : 'module_'}${currentLength + 1}`,
25 function syncUnassignedFilesWithArtifactsChanges(
30 if (artifacts.length > oldArtifacts.length) {
31 return differenceWith(
34 (unassignedFile, artifact) => unassignedFile === artifact
37 const removedArtifact = differenceWith(
40 (oldArtifact, artifact) => artifact === oldArtifact
42 return [...unassigned, removedArtifact[0]];
46 function findModuleIndexByName(modules, name) {
47 return modules.findIndex(module => module.name === name);
50 function addDeletedModuleFilesToUnassigned(unassigned, deletedModule) {
52 for (let i in deletedModule) {
53 if (deletedModule.hasOwnProperty(i)) {
55 typeof deletedModule[i] === 'string' &&
59 files.push(deletedModule[i]);
64 return unassigned.concat(files);
67 export default (state = {}, action) => {
68 switch (action.type) {
69 case actionTypes.TOGGLE_VOL_DISPLAY:
70 let clonedState = cloneDeep(state);
71 const indexToModify = findModuleIndexByName(
73 action.data.module.name
75 let modToModify = clonedState.modules[indexToModify];
76 modToModify.showVolFiles = action.data.value;
78 case actionTypes.MANIFEST_LOADED:
82 modules: action.response.modules.map(module => ({
86 module.yaml.substring(0, module.yaml.lastIndexOf('.'))
89 case actionTypes.ARTIFACT_LIST_CHANGE:
92 artifacts: action.data.artifacts,
93 unassigned: syncUnassignedFilesWithArtifactsChanges(
95 action.data.artifacts,
99 case actionTypes.ADD_ALL_UNASSIGNED_TO_ARTIFACTS:
102 artifacts: [...state.artifacts, ...state.unassigned],
105 case actionTypes.ADD_ALL_ARTIFACTS_TO_UNASSIGNED:
109 unassigned: [...state.unassigned, ...state.artifacts]
111 case actionTypes.ADD_MODULE:
114 modules: state.modules.concat({
115 ...emptyModule(action.data.isBase, state.modules.length)
118 case actionTypes.REMOVE_MODULE:
119 const moduleIndexToDelete = findModuleIndexByName(
121 action.data.moduleName
123 let unassigned = addDeletedModuleFilesToUnassigned(
125 state.modules[moduleIndexToDelete]
131 ...state.modules.slice(0, moduleIndexToDelete),
132 ...state.modules.slice(moduleIndexToDelete + 1)
135 case actionTypes.RENAME_MODULE:
136 const indexToRename = findModuleIndexByName(
140 let moduleToRename = state.modules[indexToRename];
141 moduleToRename.name = action.data.newName;
145 ...state.modules.slice(0, indexToRename),
147 ...state.modules.slice(indexToRename + 1)
150 case actionTypes.FILE_ASSIGN_CHANGED:
151 let { module, value: { value }, type } = action.data;
152 const moduleIndexToModify = findModuleIndexByName(
156 let moduleToModify = state.modules[moduleIndexToModify];
157 let dumpedFile = moduleToModify[type];
158 if (dumpedFile !== value) {
160 moduleToModify[type] = value;
162 delete moduleToModify[type];
164 const newUnassignedList = dumpedFile
166 ...state.unassigned.filter(file => file !== value),
169 : state.unassigned.filter(file => file !== value);
173 ...state.modules.slice(0, moduleIndexToModify),
175 ...state.modules.slice(moduleIndexToModify + 1)
177 unassigned: newUnassignedList