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 as softwareProductsActionTypes} from 'sdc-app/onboarding/softwareProduct/SoftwareProductConstants.js';
17 import {actionTypes, nodeFilters} from './HeatValidationConstants.js';
19 const mapVolumeData = ({fileName, env, errors}) => ({
31 const mapNetworkData = ({fileName, env, errors}) => ({
43 const mapArtifactsData = ({fileName, errors}) => ({
49 const mapOtherData = ({fileName, errors}) => ({
56 const mapHeatData = ({fileName, env, nested, volume, network, artifacts, errors, other}) => ({
62 ...(volume ? volume.map(mapVolumeData) : []),
63 ...(network ? network.map(mapNetworkData) : []),
69 ...(artifacts ? artifacts.map(mapArtifactsData) : []),
70 ...(other ? other.map(mapOtherData) : []),
71 ...(nested ? nested.map(mapHeatData) : [])
75 function createErrorList(node, parent, deep = 0, errorList = []) {
77 errorList.push(...node.errors.map((error) => ({
79 errorMessage: error.message,
82 parentName: parent.name,
86 if (node.children && node.children.length) {
87 node.children.map((child) => createErrorList(child, node, deep + 1, errorList));
92 const mapValidationDataToTree = validationData => {
93 let {heat, volume, network, artifacts, other} = validationData.importStructure || {};
101 children: (heat ? heat.map(mapHeatData) : [])
107 children: (artifacts ? artifacts.map(mapArtifactsData) : [])
113 children: (network ? network.map(mapNetworkData) : []),
119 children: (volume ? volume.map(mapVolumeData) : []),
125 children: (other ? other.map(mapOtherData) : []),
131 const toggleExpanded = (node, path) => {
132 let newNode = {...node};
133 if (path.length === 0) {
134 newNode.expanded = !node.expanded;
138 ...node.children.slice(0, index),
139 toggleExpanded(node.children[index], path.slice(1)),
140 ...node.children.slice(index + 1)
146 const expandSelected = (node, selectedNode) => {
147 let shouldExpand = node.name === selectedNode;
148 let children = node.children && node.children.map(child => {
149 let {shouldExpand: shouldExpandChild, node: newChild} = expandSelected(child, selectedNode);
150 shouldExpand = shouldExpand || shouldExpandChild;
157 expanded: node.expanded || shouldExpand,
164 export default (state = {attachmentsTree: {}}, action) => {
165 switch (action.type) {
166 case softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED:
167 let currentSoftwareProduct = action.response;
168 let attachmentsTree = currentSoftwareProduct.validationData ? mapValidationDataToTree(currentSoftwareProduct.validationData) : {};
169 let errorList = createErrorList(attachmentsTree);
174 selectedNode: nodeFilters.ALL
176 case actionTypes.TOGGLE_EXPANDED:
179 attachmentsTree: toggleExpanded(state.attachmentsTree, action.path)
181 case actionTypes.SELECTED_NODE:
182 let selectedNode = action.nodeName;
185 attachmentsTree: expandSelected(state.attachmentsTree, selectedNode).node,
188 case actionTypes.UNSELECTED_NODE:
191 selectedNode: nodeFilters.ALL