Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / entitlementPools / EntitlementPoolsListEditorView.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 import PropTypes from 'prop-types';
18
19 import i18n from 'nfvo-utils/i18n/i18n.js';
20 import Modal from 'nfvo-components/modal/Modal.jsx';
21 import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
22 import ListEditorItemView from 'nfvo-components/listEditor/ListEditorItemView.jsx';
23
24 import EntitlementPoolsEditor from './EntitlementPoolsEditor.js';
25 import {extractUnits} from './EntitlementPoolsConstants';
26
27 class EntitlementPoolsListEditorView extends React.Component {
28         static propTypes = {
29                 vendorName: PropTypes.string,
30                 licenseModelId: PropTypes.string.isRequired,
31                 entitlementPoolsList: PropTypes.array,
32                 isReadOnlyMode: PropTypes.bool.isRequired,
33                 isDisplayModal: PropTypes.bool,
34                 isModalInEditMode: PropTypes.bool,
35                 onAddEntitlementPoolClick: PropTypes.func,
36                 onEditEntitlementPoolClick: PropTypes.func,
37                 onDeleteEntitlementPool: PropTypes.func,
38         };
39
40         static defaultProps = {
41                 entitlementPoolsList: []
42         };
43
44         state = {
45                 localFilter: ''
46         };
47
48         render() {
49                 let {licenseModelId, isReadOnlyMode, isDisplayModal, isModalInEditMode, version} = this.props;
50                 let {onAddEntitlementPoolClick} = this.props;
51                 const {localFilter} = this.state;
52
53                 return (
54                         <div className='license-model-list-editor entitlement-pools-list-editor'>
55                                 <ListEditorView
56                                         title={i18n('Entitlement Pools')}
57                                         plusButtonTitle={i18n('Add Entitlement Pool')}
58                                         onAdd={onAddEntitlementPoolClick}
59                                         filterValue={localFilter}
60                                         onFilter={value => this.setState({localFilter: value})}
61                                         isReadOnlyMode={isReadOnlyMode}>
62                                         {this.filterList().map(entitlementPool => this.renderEntitlementPoolListItem(entitlementPool, isReadOnlyMode))}
63                                 </ListEditorView>
64                                 <Modal show={isDisplayModal} bsSize='large' animation={true} className='onborading-modal license-model-modal entitlement-pools-modal'>
65                                         <Modal.Header>
66                                                 <Modal.Title>{`${isModalInEditMode ? i18n('Edit Entitlement Pool') : i18n('Create New Entitlement Pool')}`}</Modal.Title>
67                                         </Modal.Header>
68                                         <Modal.Body>
69                                                 {
70                                                         isDisplayModal && (
71                                                                 <EntitlementPoolsEditor version={version} licenseModelId={licenseModelId} isReadOnlyMode={isReadOnlyMode}/>
72                                                         )
73                                                 }
74                                         </Modal.Body>
75                                 </Modal>
76                         </div>
77                 );
78         }
79
80         filterList() {
81                 let {entitlementPoolsList} = this.props;
82                 let {localFilter} = this.state;
83                 if(localFilter.trim()) {
84                         const filter = new RegExp(escape(localFilter), 'i');
85                         return entitlementPoolsList.filter(({name = '', description = ''}) => {
86                                 return escape(name).match(filter) || escape(description).match(filter);
87                         });
88                 }
89                 else {
90                         return entitlementPoolsList;
91                 }
92         }
93
94         renderEntitlementPoolListItem(entitlementPool, isReadOnlyMode) {
95                 let {id, name, description, thresholdValue, thresholdUnits} = entitlementPool;
96                 let {onEditEntitlementPoolClick, onDeleteEntitlementPool} = this.props;
97                 return (
98                         <ListEditorItemView
99                                 key={id}
100                                 onSelect={() => onEditEntitlementPoolClick(entitlementPool)}
101                                 onDelete={() => onDeleteEntitlementPool(entitlementPool)}
102                                 className='list-editor-item-view'
103                                 isReadOnlyMode={isReadOnlyMode}>
104                                 <div className='list-editor-item-view-field'>
105
106                                         <div className='title'>{i18n('Name')}</div>
107                                         <div ><div className='textEllipses text name'>{name}</div></div>
108                                 </div>
109
110                                 <div className='list-editor-item-view-field'>
111                                         <div className='title'>{i18n('Entitlement')}</div>
112                                         <div className='entitlement-pools-count'>{thresholdValue && `${thresholdValue} ${extractUnits(thresholdUnits)}`}</div>
113                                 </div>
114
115                                 <div className='list-editor-item-view-field'>
116                                         <div className='title'>{i18n('Description')}</div>
117                                         <div className='text description'>{description}</div>
118                                 </div>
119                         </ListEditorItemView>
120                 );
121         }
122
123 }
124
125 export default EntitlementPoolsListEditorView;
126
127 export function generateConfirmationMsg(entitlementPoolToDelete) {
128         let poolName = entitlementPoolToDelete ? entitlementPoolToDelete.name : '';
129         let msg = i18n('Are you sure you want to delete "{poolName}"?', {poolName: poolName});
130         let subMsg = entitlementPoolToDelete
131         && entitlementPoolToDelete.referencingFeatureGroups
132         && entitlementPoolToDelete.referencingFeatureGroups.length > 0 ?
133                 i18n('This entitlement pool is associated with one or more feature groups') :
134                 '';
135         return (
136                 <div>
137                         <p>{msg}</p>
138                         <p>{subMsg}</p>
139                 </div>
140         );
141 }