2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 import {actionTypes as softwareProductsActionTypes} from 'sdc-app/onboarding/softwareProduct/SoftwareProductConstants.js';
22 import {actionTypes} from './SoftwareProductAttachmentsConstants.js';
24 const mapVolumeData = ({fileName, env, errors}) => ({
36 const mapNetworkData = ({fileName, env, errors}) => ({
48 const mapArtifactsData = ({fileName, errors}) => ({
54 const mapOtherData = ({fileName, errors}) => ({
61 const mapHeatData = ({fileName, env, nested, volume, network, artifacts, errors, other}) => ({
67 ...(volume ? volume.map(mapVolumeData) : []),
68 ...(network ? network.map(mapNetworkData) : []),
74 ...(artifacts ? artifacts.map(mapArtifactsData) : []),
75 ...(other ? other.map(mapOtherData) : []),
76 ...(nested ? nested.map(mapHeatData) : [])
80 function createErrorList(node, parent, deep = 0, errorList = []) {
82 errorList.push(...node.errors.map((error) => ({
83 errorLevel: error.level,
84 errorMessage: error.message,
87 parentName: parent.name,
91 if (node.children && node.children.length) {
92 node.children.map((child) => createErrorList(child, node, deep + 1, errorList));
97 const mapValidationDataToTree = validationData => {
98 let {HEAT, volume, network, artifacts, other} = validationData.importStructure || {};
105 children: (HEAT ? HEAT.map(mapHeatData) : [])
111 children: (artifacts ? artifacts.map(mapArtifactsData) : [])
117 children: (network ? network.map(mapNetworkData) : []),
123 children: (volume ? volume.map(mapVolumeData) : []),
129 children: (other ? other.map(mapOtherData) : []),
135 const toggleExpanded = (node, path) => {
136 let newNode = {...node};
137 if (path.length === 0) {
138 newNode.expanded = !node.expanded;
142 ...node.children.slice(0, index),
143 toggleExpanded(node.children[index], path.slice(1)),
144 ...node.children.slice(index + 1)
150 const expandSelected = (node, selectedNode) => {
151 let shouldExpand = node.name === selectedNode;
152 let children = node.children && node.children.map(child => {
153 let {shouldExpand: shouldExpandChild, node: newChild} = expandSelected(child, selectedNode);
154 shouldExpand = shouldExpand || shouldExpandChild;
161 expanded: node.expanded || shouldExpand,
168 export default (state = {attachmentsTree: {}}, action) => {
169 switch (action.type) {
170 case softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED:
171 let currentSoftwareProduct = action.response;
172 let attachmentsTree = currentSoftwareProduct.validationData ? mapValidationDataToTree(currentSoftwareProduct.validationData) : {};
173 let errorList = createErrorList(attachmentsTree);
179 case actionTypes.TOGGLE_EXPANDED:
182 attachmentsTree: toggleExpanded(state.attachmentsTree, action.path)
184 case actionTypes.SELECTED_NODE:
185 let selectedNode = action.nodeName;
188 attachmentsTree: expandSelected(state.attachmentsTree, selectedNode).node,
191 case actionTypes.UNSELECTED_NODE:
194 selectedNode: undefined