da975a7be2f2d87b1fe799ac73804c2e19575c6e
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / dependencies / SoftwareProductDependenciesView.jsx
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 import React from 'react';
18 import i18n from 'nfvo-utils/i18n/i18n.js';
19
20 import SelectActionTable from 'nfvo-components/table/SelectActionTable.jsx';
21 import SelectActionTableRow from 'nfvo-components/table/SelectActionTableRow.jsx';
22 import SelectActionTableCell from 'nfvo-components/table/SelectActionTableCell.jsx';
23 import {relationTypesOptions} from './SoftwareProductDependenciesConstants.js';
24
25 export default class SoftwareProductDependenciesView extends React.Component {
26         filterTargets({componentsOptions, sourceToTargetMapping, selectedSourceId, selectedTargetId}) {
27                 let isInMap = sourceToTargetMapping.hasOwnProperty(selectedSourceId);
28                 return componentsOptions.filter(component => {
29                         if (component.value === selectedTargetId) {
30                                 return true;
31                         } else {
32                                 return component.value !== selectedSourceId && (isInMap ? sourceToTargetMapping[selectedSourceId].indexOf(component.value) < 0 : true);
33                         }
34                 });
35         }
36
37         filterSources({componentsOptions, sourceToTargetMapping, selectedSourceId, selectedTargetId}) {
38                 return componentsOptions.filter(component => {
39                         if (component.value === selectedSourceId) {
40                                 return true;
41                         } else {
42                                 let isInMap = sourceToTargetMapping.hasOwnProperty(component.value);
43                                 return component.value !== selectedTargetId && (isInMap ? sourceToTargetMapping[component.value].indexOf(selectedTargetId) < 0 : true);
44                         }
45                 });
46         }
47
48         render() {
49                 let {componentsOptions, softwareProductDependencies, onDataChanged, onAddDependency, isReadOnlyMode} = this.props;
50                 let canAdd = softwareProductDependencies.length < componentsOptions.length * (componentsOptions.length - 1);
51                 let sourceToTargetMapping = {};
52                 softwareProductDependencies.map(dependency => {
53                         let isInMap = sourceToTargetMapping.hasOwnProperty(dependency.sourceId);
54                         if (dependency.targetId) {
55                                 sourceToTargetMapping[dependency.sourceId] = isInMap ? [...sourceToTargetMapping[dependency.sourceId], dependency.targetId] : [dependency.targetId];
56                         }
57                 });
58                 return (
59                         <div className='software-product-dependencies'>
60                                 <div className='software-product-dependencies-title'>{i18n('Dependencies')}</div>
61                                 <SelectActionTable
62                                         columns={['Source', 'Relation Type', 'Target']}
63                                         isReadOnlyMode={isReadOnlyMode}
64                                         onAdd={canAdd ? onAddDependency : undefined}
65                                         onAddItem={i18n('Add Rule')}>                                   
66                                         {softwareProductDependencies.map(dependency => (
67                                                 <SelectActionTableRow
68                                                         key={dependency.id}
69                                                         onDelete={() => onDataChanged(softwareProductDependencies.filter(currentDependency => currentDependency.id !== dependency.id))}
70                                                         overlayMsg={i18n('There is a loop between selections')}
71                                                         hasError={dependency.hasCycle}>
72                                                         <SelectActionTableCell
73                                                                 options={this.filterSources({componentsOptions, sourceToTargetMapping, selectedSourceId: dependency.sourceId, selectedTargetId: dependency.targetId})}
74                                                                 selected={dependency.sourceId}
75                                                                 placeholder={i18n('Select VFC...')}
76                                                                 onChange={newSourceId => onDataChanged(softwareProductDependencies.map(currentDependency =>
77                                                                         ({...currentDependency, sourceId: currentDependency.id === dependency.id ? newSourceId : currentDependency.sourceId})
78                                                                 ))} />
79                                                         <SelectActionTableCell options={relationTypesOptions} selected={dependency.relationType} clearable={false}/>
80                                                         <SelectActionTableCell
81                                                                 placeholder={i18n('Select VFC...')}
82                                                                 options={this.filterTargets({componentsOptions, sourceToTargetMapping, selectedSourceId: dependency.sourceId, selectedTargetId: dependency.targetId})}
83                                                                 selected={dependency.targetId}
84                                                                 onChange={newTargetId => onDataChanged(softwareProductDependencies.map(currentDependency =>
85                                                                         ({...currentDependency, targetId: currentDependency.id === dependency.id ? newTargetId : currentDependency.targetId})
86                                                                 ))} />
87                                                 </SelectActionTableRow>
88                                         ))}
89                                 </SelectActionTable>
90                         </div>
91                 );
92         }
93
94         save() {
95                 let {onSubmit, softwareProductDependencies} = this.props;
96                 return onSubmit(softwareProductDependencies);
97         }
98 }