[SDC] Onboarding 1710 rebase.
[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                                         numOfIcons={2}
64                                         isReadOnlyMode={isReadOnlyMode}
65                                         onAdd={canAdd ? onAddDependency : undefined}
66                                         onAddItem={i18n('Add Rule')}>                                   
67                                         {softwareProductDependencies.map(dependency => (
68                                                 <SelectActionTableRow
69                                                         key={dependency.id}
70                                                         onDelete={() => onDataChanged(softwareProductDependencies.filter(currentDependency => currentDependency.id !== dependency.id))}
71                                                         overlayMsg={i18n('There is a loop between selections')}
72                                                         hasError={dependency.hasCycle}
73                                                         hasErrorIndication>
74                                                         <SelectActionTableCell
75                                                                 options={this.filterSources({componentsOptions, sourceToTargetMapping, selectedSourceId: dependency.sourceId, selectedTargetId: dependency.targetId})}
76                                                                 selected={dependency.sourceId}
77                                                                 placeholder={i18n('Select VFC...')}
78                                                                 onChange={newSourceId => onDataChanged(softwareProductDependencies.map(currentDependency =>
79                                                                         ({...currentDependency, sourceId: currentDependency.id === dependency.id ? newSourceId : currentDependency.sourceId})
80                                                                 ))} />
81                                                         <SelectActionTableCell options={relationTypesOptions} selected={dependency.relationType} clearable={false}/>
82                                                         <SelectActionTableCell
83                                                                 placeholder={i18n('Select VFC...')}
84                                                                 options={this.filterTargets({componentsOptions, sourceToTargetMapping, selectedSourceId: dependency.sourceId, selectedTargetId: dependency.targetId})}
85                                                                 selected={dependency.targetId}
86                                                                 onChange={newTargetId => onDataChanged(softwareProductDependencies.map(currentDependency =>
87                                                                         ({...currentDependency, targetId: currentDependency.id === dependency.id ? newTargetId : currentDependency.targetId})
88                                                                 ))} />
89                                                 </SelectActionTableRow>
90                                         ))}
91                                 </SelectActionTable>
92                         </div>
93                 );
94         }
95
96         save() {
97                 let {onSubmit, softwareProductDependencies} = this.props;
98                 return onSubmit(softwareProductDependencies);
99         }
100 }