Add collaboration feature
[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 import PropTypes from 'prop-types';
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/validation/InputOptions.jsx';
25 import {optionsInputValues} from './LicenseKeyGroupsConstants';
26
27 class LicenseKeyGroupsListEditorView extends React.Component {
28         static propTypes = {
29                 vendorName: PropTypes.string,
30                 licenseModelId: PropTypes.string.isRequired,
31                 licenseKeyGroupsList: PropTypes.array,
32                 isReadOnlyMode: PropTypes.bool.isRequired,
33                 isDisplayModal: PropTypes.bool,
34                 isModalInEditMode: PropTypes.bool,
35                 onAddLicenseKeyGroupClick: PropTypes.func,
36                 onEditLicenseKeyGroupClick: PropTypes.func,
37                 onDeleteLicenseKeyGroupClick: PropTypes.func
38         };
39
40         static defaultProps = {
41                 licenseKeyGroupsList: []
42         };
43
44         state = {
45                 localFilter: ''
46         };
47
48         render() {
49                 let {licenseModelId, isReadOnlyMode, isDisplayModal, isModalInEditMode, version} = this.props;
50                 let {onAddLicenseKeyGroupClick} = this.props;
51                 const {localFilter} = this.state;
52
53                 return (
54                         <div className='license-model-list-editor license-key-groups-list-editor'>
55                                 <ListEditorView
56                                         title={i18n('License Key Groups')}
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-model-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
126                 if (operationalScope.choices && operationalScope.choices.toString() === i18n(optionInputOther.OTHER)) {
127                         return operationalScope.other;
128                 } else if (operationalScope.choices) {
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                 } else {
135                         return '';
136                 }
137         }
138
139         extractValue(item) {
140                 if (item === undefined) {
141                         return '';
142                 } //TODO fix it sooner rather than later
143
144                 return item ? item.choice === optionInputOther.OTHER ? item.other : InputOptions.getTitleByName(optionsInputValues, item.choice) : '';
145         }
146 }
147
148 export default LicenseKeyGroupsListEditorView;
149
150 export function generateConfirmationMsg(licenseKeyGroupToDelete) {
151         let name = licenseKeyGroupToDelete ? licenseKeyGroupToDelete.name : '';
152         let msg = i18n('Are you sure you want to delete "{name}"?', {name: name});
153         let subMsg = licenseKeyGroupToDelete.referencingFeatureGroups
154         && licenseKeyGroupToDelete.referencingFeatureGroups.length > 0 ?
155                 i18n('This license key group is associated with one or more feature groups') :
156                 '';
157         return (
158                 <div>
159                         <p>{msg}</p>
160                         <p>{subMsg}</p>
161                 </div>
162         );
163 }