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';
19 const emptyModule = (isBase, currentLength) => ({
20 name: `${isBase ? 'base_' : 'module_'}${currentLength + 1}`,
24 function syncUnassignedFilesWithArtifactsChanges(
29 if (artifacts.length > oldArtifacts.length) {
30 return differenceWith(
33 (unassignedFile, artifact) => unassignedFile === artifact
36 const removedArtifact = differenceWith(
39 (oldArtifact, artifact) => artifact === oldArtifact
41 return [...unassigned, removedArtifact[0]];
45 function findModuleIndexByName(modules, name) {
46 return modules.findIndex(module => module.name === name);
49 function addDeletedModuleFilesToUnassigned(unassigned, deletedModule) {
51 for (let i in deletedModule) {
52 if (deletedModule.hasOwnProperty(i)) {
54 typeof deletedModule[i] === 'string' &&
58 files.push(deletedModule[i]);
63 return unassigned.concat(files);
66 export default (state = {}, action) => {
67 switch (action.type) {
68 case actionTypes.MANIFEST_LOADED:
72 modules: action.response.modules.map(module => ({
76 module.yaml.substring(0, module.yaml.lastIndexOf('.'))
79 case actionTypes.ARTIFACT_LIST_CHANGE:
82 artifacts: action.data.artifacts,
83 unassigned: syncUnassignedFilesWithArtifactsChanges(
85 action.data.artifacts,
89 case actionTypes.ADD_ALL_UNASSIGNED_TO_ARTIFACTS:
92 artifacts: [...state.artifacts, ...state.unassigned],
95 case actionTypes.ADD_ALL_ARTIFACTS_TO_UNASSIGNED:
99 unassigned: [...state.unassigned, ...state.artifacts]
101 case actionTypes.ADD_MODULE:
104 modules: state.modules.concat({
105 ...emptyModule(action.data.isBase, state.modules.length)
108 case actionTypes.REMOVE_MODULE:
109 const moduleIndexToDelete = findModuleIndexByName(
111 action.data.moduleName
113 let unassigned = addDeletedModuleFilesToUnassigned(
115 state.modules[moduleIndexToDelete]
121 ...state.modules.slice(0, moduleIndexToDelete),
122 ...state.modules.slice(moduleIndexToDelete + 1)
125 case actionTypes.RENAME_MODULE:
126 const indexToRename = findModuleIndexByName(
130 let moduleToRename = state.modules[indexToRename];
131 moduleToRename.name = action.data.newName;
135 ...state.modules.slice(0, indexToRename),
137 ...state.modules.slice(indexToRename + 1)
140 case actionTypes.FILE_ASSIGN_CHANGED:
141 let { module, value: { value }, type } = action.data;
142 const moduleIndexToModify = findModuleIndexByName(
146 let moduleToModify = state.modules[moduleIndexToModify];
147 let dumpedFile = moduleToModify[type];
148 if (dumpedFile !== value) {
150 moduleToModify[type] = value;
152 delete moduleToModify[type];
154 const newUnassignedList = dumpedFile
156 ...state.unassigned.filter(file => file !== value),
159 : state.unassigned.filter(file => file !== value);
163 ...state.modules.slice(0, moduleIndexToModify),
165 ...state.modules.slice(moduleIndexToModify + 1)
167 unassigned: newUnassignedList