Add collaboration feature
[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, NEW_RULE_TEMP_ID} from './SoftwareProductDependenciesConstants.js';
24
25
26 const TableActionRow = ({onAction, actionIcon, showAction, dependency, sourceOptions, targetOptions, onDataChanged}) => {
27         return (
28                 <SelectActionTableRow
29                         key={dependency.id}
30                         onAction={onAction}
31                         overlayMsg={i18n('There is a loop between selections')}
32                         hasError={dependency.hasCycle}
33                         hasErrorIndication
34                         showAction={showAction}
35                         actionIcon={actionIcon}>
36                         <SelectActionTableCell
37                                 options={sourceOptions}
38                                 selected={dependency.sourceId}
39                                 placeholder={i18n('Select VFC...')}
40                                 clearable={false}
41                                 onChange={newVal =>  {
42                                         dependency.sourceId = newVal;
43                                         onDataChanged(dependency);
44                                 }} />
45                         <SelectActionTableCell options={relationTypesOptions} selected={dependency.relationType} clearable={false}/>
46                         <SelectActionTableCell
47                                 placeholder={i18n('Select VFC...')}
48                                 options={targetOptions}
49                                 selected={dependency.targetId}
50                                 clearable={false}
51                                 onChange={newVal =>  {
52                                         dependency.targetId = newVal;
53                                         onDataChanged(dependency);
54                                 }} />
55                 </SelectActionTableRow>
56         );
57 };
58
59
60 export default class SoftwareProductDependenciesView extends React.Component {
61         filterTargets({componentsOptions, sourceToTargetMapping, selectedSourceId, selectedTargetId}) {
62                 let isInMap = sourceToTargetMapping.hasOwnProperty(selectedSourceId);
63                 return componentsOptions.filter(component => {
64                         if (component.value === selectedTargetId) {
65                                 return true;
66                         } else {
67                                 return component.value !== selectedSourceId && (isInMap ? sourceToTargetMapping[selectedSourceId].indexOf(component.value) < 0 : true);
68                         }
69                 });
70         }
71
72         filterSources({componentsOptions, sourceToTargetMapping, selectedSourceId, selectedTargetId}) {
73                 return componentsOptions.filter(component => {
74                         if (component.value === selectedSourceId) {
75                                 return true;
76                         } else {
77                                 let isInMap = sourceToTargetMapping.hasOwnProperty(component.value);
78                                 return component.value !== selectedTargetId && (isInMap ? sourceToTargetMapping[component.value].indexOf(selectedTargetId) < 0 : true);
79                         }
80                 });
81         }
82
83         render() {
84                 let {componentsOptions, softwareProductDependencies, onDataChanged, onAddDependency, onDeleteDependency, isReadOnlyMode} = this.props;
85                 let sourceToTargetMapping = {};
86                 softwareProductDependencies.map(dependency => {
87                         let isInMap = sourceToTargetMapping.hasOwnProperty(dependency.sourceId);
88                         if (dependency.targetId) {
89                                 sourceToTargetMapping[dependency.sourceId] = isInMap ? [...sourceToTargetMapping[dependency.sourceId], dependency.targetId] : [dependency.targetId];
90                         }
91                 });
92                 let depList = softwareProductDependencies.filter(dependency => dependency.id !== NEW_RULE_TEMP_ID);
93                 let newDependency = softwareProductDependencies.find(dependency => dependency.id === NEW_RULE_TEMP_ID);
94                 return (
95                         <div className='software-product-dependencies'>
96                                 <div className='page-title'>{i18n('Dependencies')}</div>
97                                 <SelectActionTable
98                                         columns={[i18n('Source'), i18n('Relation Type'), i18n('Target')]}
99                                         numOfIcons={2}
100                                         isReadOnlyMode={isReadOnlyMode}>
101                                         {!isReadOnlyMode && <TableActionRow
102                                                 key={newDependency.id}
103                                                 actionIcon='plusCircle'
104                                                 onAction={() => onAddDependency(newDependency)}
105                                                 dependency={newDependency}
106                                                 componentsOptions={componentsOptions}
107                                                 sourceToTargetMapping={sourceToTargetMapping}
108                                                 onDataChanged={onDataChanged}
109                                                 sourceOptions={this.filterSources({componentsOptions, sourceToTargetMapping, selectedSourceId: newDependency.sourceId, selectedTargetId: newDependency.targetId})}
110                                                 targetOptions={this.filterTargets({componentsOptions, sourceToTargetMapping, selectedSourceId: newDependency.sourceId, selectedTargetId: newDependency.targetId})}
111                                                 showAction={newDependency.targetId !== null && newDependency.relationType !== null && newDependency.sourceId !== null}/> }
112                                         {depList.map(dependency => (
113                                                 <TableActionRow
114                                                         key={dependency.id}
115                                                         actionIcon='trashO'
116                                                         onAction={() => onDeleteDependency(dependency)}
117                                                         dependency={dependency}
118                                                         componentsOptions={componentsOptions}
119                                                         sourceToTargetMapping={sourceToTargetMapping}
120                                                         sourceOptions={this.filterSources({componentsOptions, sourceToTargetMapping, selectedSourceId: dependency.sourceId, selectedTargetId: dependency.targetId})}
121                                                         targetOptions={this.filterTargets({componentsOptions, sourceToTargetMapping, selectedSourceId: dependency.sourceId, selectedTargetId: dependency.targetId})}
122                                                         onDataChanged={onDataChanged}
123                                                         showAction={true}/>
124                                         ))}
125                                 </SelectActionTable>
126                         </div>
127                 );
128         }
129
130 }