a303e46706dac1d0ab65d2cfcdc3b3044fe0bc9b
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / licenseKeyGroups / LicenseKeyGroupsListEditorView.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 LicenseKeyGroupsEditor from './LicenseKeyGroupsEditor.js';
24 import InputOptions, {other as optionInputOther} from 'nfvo-components/input/inputOptions/InputOptions.jsx';
25 import {optionsInputValues} from './LicenseKeyGroupsConstants';
26
27 class LicenseKeyGroupsListEditorView extends React.Component {
28         static propTypes = {
29                 vendorName: React.PropTypes.string,
30                 licenseModelId: React.PropTypes.string.isRequired,
31                 licenseKeyGroupsList: React.PropTypes.array,
32                 isReadOnlyMode: React.PropTypes.bool.isRequired,
33                 isDisplayModal: React.PropTypes.bool,
34                 isModalInEditMode: React.PropTypes.bool,
35                 onAddLicenseKeyGroupClick: React.PropTypes.func,
36                 onEditLicenseKeyGroupClick: React.PropTypes.func,
37                 onDeleteLicenseKeyGroupClick: React.PropTypes.func
38         };
39
40         static defaultProps = {
41                 licenseKeyGroupsList: []
42         };
43
44         state = {
45                 localFilter: ''
46         };
47
48         render() {
49                 let {licenseModelId, vendorName, isReadOnlyMode, isDisplayModal, isModalInEditMode, version} = this.props;
50                 let {onAddLicenseKeyGroupClick} = this.props;
51                 const {localFilter} = this.state;
52
53                 return (
54                         <div className='license-key-groups-list-editor'>
55                                 <ListEditorView
56                                         title={i18n('License Key Groups', {vendorName})}
57                                         plusButtonTitle={i18n('Add License Key Group')}
58                                         onAdd={onAddLicenseKeyGroupClick}
59                                         filterValue={localFilter}
60                                         onFilter={value => this.setState({localFilter: value})}
61                                         isReadOnlyMode={isReadOnlyMode}>
62                                         {this.filterList().map(licenseKeyGroup => (this.renderLicenseKeyGroupListItem(licenseKeyGroup, isReadOnlyMode)))}
63                                 </ListEditorView>
64                                 <Modal show={isDisplayModal} bsSize='large' animation={true} className='onborading-modal license-key-groups-modal'>
65                                         <Modal.Header>
66                                                 <Modal.Title>{`${isModalInEditMode ? i18n('Edit License Key Group') : i18n('Create New License Key Group')}`}</Modal.Title>
67                                         </Modal.Header>
68                                         <Modal.Body>
69                                                 {
70                                                         isDisplayModal && (
71                                                                 <LicenseKeyGroupsEditor version={version} licenseModelId={licenseModelId} isReadOnlyMode={isReadOnlyMode}/>
72                                                         )
73                                                 }
74                                         </Modal.Body>
75                                 </Modal>
76                         </div>
77                 );
78         }
79
80         filterList() {
81                 let {licenseKeyGroupsList} = this.props;
82                 let {localFilter} = this.state;
83                 if (localFilter.trim()) {
84                         const filter = new RegExp(escape(localFilter), 'i');
85                         return licenseKeyGroupsList.filter(({name = '', description = '', operationalScope = '', type = ''}) => {
86                                 return escape(name).match(filter) || escape(description).match(filter) || escape(this.extractValue(operationalScope)).match(filter) || escape(type).match(filter);
87                         });
88                 }
89                 else {
90                         return licenseKeyGroupsList;
91                 }
92         }
93
94         renderLicenseKeyGroupListItem(licenseKeyGroup, isReadOnlyMode) {
95                 let {id, name, description, operationalScope, type} = licenseKeyGroup;
96                 let {onEditLicenseKeyGroupClick, onDeleteLicenseKeyGroupClick} = this.props;
97                 return (
98                         <ListEditorItemView
99                                 key={id}
100                                 onSelect={() => onEditLicenseKeyGroupClick(licenseKeyGroup)}
101                                 onDelete={() => onDeleteLicenseKeyGroupClick(licenseKeyGroup)}
102                                 className='list-editor-item-view'
103                                 isReadOnlyMode={isReadOnlyMode}>
104                                 <div className='list-editor-item-view-field'>
105                                         <div className='title'>{i18n('Name')}</div>
106                                         <div className='text name'>{name}</div>
107                                 </div>
108
109                                 <div className='list-editor-item-view-field'>
110                                         <div className='title'>{i18n('Operational Scope')}</div>
111                                         <div className='text operational-scope'>{operationalScope && this.getOperationalScopes(operationalScope)}</div>
112
113                                         <div className='title'>{i18n('Type')}</div>
114                                         <div className='text type'>{InputOptions.getTitleByName(optionsInputValues, type)}</div>
115                                 </div>
116                                 <div className='list-editor-item-view-field'>
117                                         <div className='title'>{i18n('Description')}</div>
118                                         <div className='text description'>{description}</div>
119                                 </div>
120                         </ListEditorItemView>
121                 );
122         }
123
124         getOperationalScopes(operationalScope) {
125                 if(operationalScope.choices.toString() === i18n(optionInputOther.OTHER) && operationalScope.other !== '') {
126                         return operationalScope.other;
127                 }
128                 else {
129                         let allOpScopes = '';
130                         for (let opScope of operationalScope.choices) {
131                                 allOpScopes += allOpScopes === '' ? InputOptions.getTitleByName(optionsInputValues, opScope) : `, ${InputOptions.getTitleByName(optionsInputValues, opScope)}`;
132                         }
133                         return allOpScopes;
134                 }
135         }
136
137         extractValue(item) {
138                 if (item === undefined) {
139                         return '';
140                 } //TODO fix it later
141
142                 return item ? item.choice === optionInputOther.OTHER ? item.other : InputOptions.getTitleByName(optionsInputValues, item.choice) : '';
143         }
144 }
145
146 export default LicenseKeyGroupsListEditorView;
147
148 export function generateConfirmationMsg(licenseKeyGroupToDelete) {
149         let name = licenseKeyGroupToDelete ? licenseKeyGroupToDelete.name : '';
150         let msg = i18n('Are you sure you want to delete "{name}"?', {name});
151         let subMsg = licenseKeyGroupToDelete.referencingFeatureGroups
152         && licenseKeyGroupToDelete.referencingFeatureGroups.length > 0 ?
153                 i18n('This license key group is associated with one or more feature groups') :
154                 '';
155         return (
156                 <div>
157                         <p>{msg}</p>
158                         <p>{subMsg}</p>
159                 </div>
160         );
161 }