bc0f5c71c0754c75a78aea244f8a7e14da3b3f79
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / featureGroups / FeatureGroupListEditorView.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 Modal from 'nfvo-components/modal/Modal.jsx';
20 import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
21 import ListEditorItemView from 'nfvo-components/listEditor/ListEditorItemView.jsx';
22
23 import FeatureGroupEditor from './FeatureGroupEditor.js';
24
25 class FeatureGroupListEditorView extends React.Component {
26         static propTypes = {
27                 vendorName: React.PropTypes.string,
28                 licenseModelId: React.PropTypes.string.isRequired,
29                 featureGroupsModal: React.PropTypes.shape({
30                         show: React.PropTypes.bool,
31                         editMode: React.PropTypes.bool
32                 }),
33                 isReadOnlyMode: React.PropTypes.bool.isRequired,
34                 onAddFeatureGroupClick: React.PropTypes.func,
35                 onEditFeatureGroupClick: React.PropTypes.func,
36                 onDeleteFeatureGroupClick: React.PropTypes.func,
37                 onCancelFeatureGroupsEditor: React.PropTypes.func,
38                 featureGroupsList: React.PropTypes.array
39         };
40
41         static defaultProps = {
42                 featureGroupsList: [],
43                 featureGroupsModal: {
44                         show: false,
45                         editMode: false
46                 }
47         };
48
49         state = {
50                 localFilter: ''
51         };
52
53         render() {
54                 let {vendorName, licenseModelId, featureGroupsModal, isReadOnlyMode, onAddFeatureGroupClick, version} = this.props;
55                 const {localFilter} = this.state;
56                 return (
57                         <div className='feature-groups-list-editor'>
58                                 <ListEditorView
59                                         title={i18n('Feature Groups', {vendorName})}
60                                         plusButtonTitle={i18n('Add Feature Group')}
61                                         filterValue={localFilter}
62                                         onFilter={value => this.setState({localFilter: value})}
63                                         onAdd={() => onAddFeatureGroupClick(version)}
64                                         isReadOnlyMode={isReadOnlyMode}>
65                                         {this.filterList().map(listItem => this.renderFeatureGroupListItem(listItem, isReadOnlyMode, version))}
66                                 </ListEditorView>
67                                 {featureGroupsModal.show && <Modal show={featureGroupsModal.show} bsSize='large' animation={true}
68                                                className='onborading-modal feature-group-modal'>
69                                                 <Modal.Header>
70                                                         <Modal.Title>{`${featureGroupsModal.editMode ? i18n('Edit Feature Group') : i18n('Create New Feature Group')}`}</Modal.Title>
71                                                 </Modal.Header>
72                                                 <Modal.Body>
73                                                         <FeatureGroupEditor
74                                                                 version={version}
75                                                                 licenseModelId={licenseModelId}
76                                                                 isReadOnlyMode={isReadOnlyMode}/>
77                                                 </Modal.Body>
78                                         </Modal>
79                                 }
80
81                         </div>
82                 );
83         }
84
85
86         renderFeatureGroupListItem(listItem, isReadOnlyMode, version) {
87                 let {name, description, entitlementPoolsIds = [], licenseKeyGroupsIds = []} = listItem;
88                 return (
89                         <ListEditorItemView
90                                 key={listItem.id}
91                                 onDelete={() => this.deleteFeatureGroupItem(listItem, version)}
92                                 onSelect={() => this.editFeatureGroupItem(listItem, version)}
93                                 className='list-editor-item-view'
94                                 isReadOnlyMode={isReadOnlyMode}>
95                                 <div className='list-editor-item-view-field'>
96                                         <div className='title'>{i18n('Name')}</div>
97                                         <div className='text name'>{name}</div>
98                                 </div>
99
100                                 <div className='list-editor-item-view-field'>
101                                         <div className='feature-groups-count-field'>
102                                                 <div className='title'>{i18n('Entitlement')}</div>
103                                                 <div className='title'>{i18n('Pools')}</div>
104                                                 <div className='feature-groups-count-ep'>{entitlementPoolsIds.length || 0}</div>
105                                         </div>
106                                         <div className='feature-groups-count-field'>
107                                                 <div className='title'>{i18n('License key')}</div>
108                                                 <div className='title'>{i18n('Groups')}</div>
109                                                 <div className='feature-groups-count-lk'>{licenseKeyGroupsIds.length || 0}</div>
110                                         </div>
111                                 </div>
112
113                                 <div className='list-editor-item-view-field'>
114                                         <div className='title'>{i18n('Description')}</div>
115                                         <div className='text description'>{description}</div>
116                                 </div>
117
118                         </ListEditorItemView>
119                 );
120         }
121
122         filterList() {
123                 let {featureGroupsList} = this.props;
124                 let {localFilter} = this.state;
125                 if (localFilter.trim()) {
126                         const filter = new RegExp(escape(localFilter), 'i');
127                         return featureGroupsList.filter(({name = '', description = ''}) => {
128                                 return escape(name).match(filter) || escape(description).match(filter);
129                         });
130                 }
131                 else {
132                         return featureGroupsList;
133                 }
134         }
135
136         editFeatureGroupItem(featureGroup, version) {
137                 this.props.onEditFeatureGroupClick(featureGroup, version);
138         }
139
140         deleteFeatureGroupItem(featureGroup, version) {
141                 this.props.onDeleteFeatureGroupClick(featureGroup, version);
142         }
143 }
144
145 export default FeatureGroupListEditorView;
146
147 export function generateConfirmationMsg(featureGroupToDelete) {
148         let name = featureGroupToDelete ? featureGroupToDelete.name : '';
149         let msg = i18n('Are you sure you want to delete "{name}"?', {name});
150         let subMsg = featureGroupToDelete.referencingLicenseAgreements
151         && featureGroupToDelete.referencingLicenseAgreements.length > 0 ?
152                 i18n('This feature group is associated with one ore more license agreements') :
153                 '';
154         return (
155                 <div>
156                         <p>{msg}</p>
157                         <p>{subMsg}</p>
158                 </div>
159         );
160 }