[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / SoftwareProductComponentsListView.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 import React from 'react';
17
18 import i18n from 'nfvo-utils/i18n/i18n.js';
19 import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
20 import ListEditorItemView from 'nfvo-components/listEditor/ListEditorItemView.jsx';
21 import ListEditorItemViewField from 'nfvo-components/listEditor/ListEditorItemViewField.jsx';
22
23 const ComponentPropType = React.PropTypes.shape({
24         id: React.PropTypes.string,
25         name: React.PropTypes.string,
26         displayName: React.PropTypes.string,
27         description: React.PropTypes.string
28 });
29
30 class SoftwareProductComponentsListView extends React.Component {
31
32         state = {
33                 localFilter: ''
34         };
35
36         static propTypes = {
37                 isReadOnlyMode: React.PropTypes.bool,
38                 componentsList: React.PropTypes.arrayOf(ComponentPropType),
39                 onComponentSelect: React.PropTypes.func
40         };
41
42         render() {
43                 let {componentsList = []} =  this.props;
44                 return (
45                         <div className=''>
46                                 {
47                                         componentsList.length > 0 && this.renderComponents()
48                                 }
49                         </div>
50                 );
51         }
52
53         renderComponents() {
54                 const {localFilter} = this.state;
55                 let {isReadOnlyMode} =  this.props;
56
57                 return (
58                         <ListEditorView
59                                 title={i18n('Virtual Function Components')}
60                                 filterValue={localFilter}
61                                 placeholder={i18n('Filter Components')}
62                                 onFilter={value => this.setState({localFilter: value})}
63                                 isReadOnlyMode={isReadOnlyMode}
64                                 twoColumns>
65                                 {this.filterList().map(component => this.renderComponentsListItem(component))}
66                         </ListEditorView>
67                 );
68         }
69
70         renderComponentsListItem(component) {
71                 let {id: componentId, name, displayName, description = ''} = component;
72                 let {currentSoftwareProduct: {id, version}, onComponentSelect} = this.props;
73                 return (
74                         <ListEditorItemView
75                                 key={name + Math.floor(Math.random() * (100 - 1) + 1).toString()}
76                                 className='list-editor-item-view'
77                                 onSelect={() => onComponentSelect({id, componentId, version})}>
78                                 <ListEditorItemViewField>
79                                         <div className='name'>{displayName}</div>
80                                 </ListEditorItemViewField>
81                                 <ListEditorItemViewField>
82                                         <div className='description'>{description}</div>
83                                 </ListEditorItemViewField>
84                         </ListEditorItemView>
85                 );
86         }
87
88         filterList() {
89                 let {componentsList = []} = this.props;
90
91                 let {localFilter} = this.state;
92                 if (localFilter.trim()) {
93                         const filter = new RegExp(escape(localFilter), 'i');
94                         return componentsList.filter(({displayName = '', description = ''}) => {
95                                 return escape(displayName).match(filter) || escape(description).match(filter);
96                         });
97                 }
98                 else {
99                         return componentsList;
100                 }
101         }
102 }
103
104 export default SoftwareProductComponentsListView;