2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
16 import {actionTypes} from './HeatSetupConstants.js';
17 import differenceWith from 'lodash/differenceWith.js';
20 const emptyModule = (isBase, currentLength) => ({
21 name: `${isBase ? 'base_' : 'module_'}${currentLength + 1}`,
25 function syncUnassignedFilesWithArtifactsChanges(unassigned, artifacts, oldArtifacts) {
26 if (artifacts.length > oldArtifacts.length) {
27 return differenceWith(unassigned, artifacts, (unassignedFile, artifact) => unassignedFile === artifact);
30 const removedArtifact = differenceWith(oldArtifacts, artifacts, (oldArtifact, artifact) => artifact === oldArtifact);
31 return [...unassigned, removedArtifact[0]];
35 function findModuleIndexByName(modules, name) {
36 return modules.findIndex(module => module.name === name);
39 function addDeletedModuleFilesToUnassigned(unassigned, deletedModule){
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]);
49 return unassigned.concat(files);
52 export default (state = {}, action) => {
53 switch (action.type) {
54 case actionTypes.MANIFEST_LOADED:
58 modules: action.response.modules.map(module => ({...module, name: module.name || module.yaml.substring(0, module.yaml.lastIndexOf('.'))}))
60 case actionTypes.ARTIFACT_LIST_CHANGE:
63 artifacts: action.data.artifacts,
64 unassigned: syncUnassignedFilesWithArtifactsChanges(state.unassigned, action.data.artifacts, state.artifacts)
66 case actionTypes.ADD_ALL_UNASSIGNED_TO_ARTIFACTS:
69 artifacts: [...state.artifacts,...state.unassigned],
72 case actionTypes.ADD_ALL_ARTIFACTS_TO_UNASSIGNED:
76 unassigned: [...state.unassigned, ...state.artifacts]
78 case actionTypes.ADD_MODULE:
81 modules: state.modules.concat({...emptyModule(action.data.isBase, state.modules.length)})
83 case actionTypes.REMOVE_MODULE:
84 const moduleIndexToDelete = findModuleIndexByName(state.modules, action.data.moduleName);
85 let unassigned = addDeletedModuleFilesToUnassigned(state.unassigned, state.modules[moduleIndexToDelete]);
89 modules: [...state.modules.slice(0, moduleIndexToDelete), ...state.modules.slice(moduleIndexToDelete + 1)]
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;
97 modules: [...state.modules.slice(0, indexToRename), moduleToRename, ...state.modules.slice(indexToRename + 1)]
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) {
106 moduleToModify[type] = value;
109 delete moduleToModify[type];
111 const newUnassignedList = dumpedFile ? [...state.unassigned.filter(file => file !== value), dumpedFile] : state.unassigned.filter(file => file !== value);
114 modules: [...state.modules.slice(0, moduleIndexToModify), moduleToModify, ...state.modules.slice(moduleIndexToModify + 1)],
115 unassigned: newUnassignedList