Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / compute / computeComponents / ComputeFlavors.js
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 {connect} from 'react-redux';
19 import i18n from 'nfvo-utils/i18n/i18n.js';
20 import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
21 import ListEditorItemView from 'nfvo-components/listEditor/ListEditorItemView.jsx';
22 import ComputeFlavorActionHelper from 'sdc-app/onboarding/softwareProduct/components/compute/ComputeFlavorActionHelper.js';
23 import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
24
25 const mapActionsToProps = (dispatch, {softwareProductId, componentId, version}) => {
26         return {
27                 onAddComputeClick: (isReadOnlyMode) => ComputeFlavorActionHelper.openComputeEditor(dispatch, {props: {softwareProductId, componentId, isReadOnlyMode, version}}),
28                 onEditCompute: ({computeId, isReadOnlyMode}) => ComputeFlavorActionHelper.loadCompute(dispatch, {softwareProductId, componentId, version, computeId, isReadOnlyMode}),
29                 onDeleteCompute: ({id, name}) => dispatch({
30                         type: modalActionTypes.GLOBAL_MODAL_WARNING,
31                         data:{
32                                 msg: i18n('Are you sure you want to delete "{name}"?', {name: name}),
33                                 onConfirmed: () => ComputeFlavorActionHelper.deleteCompute(dispatch, {softwareProductId, componentId, computeId: id, version})
34                         }
35                 })
36         };
37 };
38
39 const computeItemPropType = PropTypes.shape({
40         id: PropTypes.string,
41         name: PropTypes.string,
42         description: PropTypes.string
43 });
44
45 class ComputeFlavors extends React.Component {
46
47         static propTypes = {
48                 isReadOnlyMode: PropTypes.bool,
49                 isManual: PropTypes.bool,
50                 onAddComputeClick: PropTypes.func,
51                 computeFlavorsList: PropTypes.arrayOf(computeItemPropType)
52         };
53
54         state = {
55                 localFilter: ''
56         };
57
58         render() {
59                 const {localFilter} = this.state;
60                 const {isReadOnlyMode, isManual, onAddComputeClick, onEditCompute, onDeleteCompute} = this.props;
61                 return (
62                         <div className='computes-list'>
63                                 <ListEditorView
64                                         title={i18n('Computes')}
65                                         plusButtonTitle={i18n('Add Compute')}
66                                         onAdd={isManual ? () => onAddComputeClick(isReadOnlyMode) : null}
67                                         isReadOnlyMode={isReadOnlyMode}
68                                         onFilter={isManual ? value => this.setState({localFilter: value}) : null}
69                                         filterValue={localFilter}
70                                         twoColumns>
71                                         {this.filterList().map(computeItem =>
72                                                 <ComputeItem key={computeItem.id}
73                                                         computeItem={computeItem} isReadOnlyMode={isReadOnlyMode} isManual={isManual}
74                                                         onEditCompute={onEditCompute} onDeleteCompute={onDeleteCompute}/>)
75                                         }
76                                 </ListEditorView>
77                         </div>
78                 );
79         }
80
81         filterList() {
82                 const {computeFlavorsList = []} = this.props;
83
84                 const {localFilter} = this.state;
85                 if (localFilter.trim()) {
86                         const filter = new RegExp(escape(localFilter), 'i');
87                         return computeFlavorsList.filter(({name = '', description = ''}) => {
88                                 return escape(name).match(filter) || escape(description).match(filter);
89                         });
90                 }
91                 else {
92                         return computeFlavorsList;
93                 }
94         }
95 }
96
97 const ComputeItem = ({computeItem, isReadOnlyMode, isManual, onEditCompute, onDeleteCompute}) => {
98         const {id, name, description} = computeItem;
99         return (
100                 <ListEditorItemView
101                         key={'item_' + id}
102                         className='list-editor-item-view'
103                         isReadOnlyMode={isReadOnlyMode}
104                         onSelect={() => onEditCompute({computeId: id, isReadOnlyMode})}
105                         onDelete={isManual ? () => onDeleteCompute({id, name}) : null}>
106
107                         <div className='list-editor-item-view-field'>
108                                 <div className='name'>{name}</div>
109                         </div>
110                         <div className='list-editor-item-view-field'>
111                                 <div className='description'>{description}</div>
112                         </div>
113                 </ListEditorItemView>
114         );
115 };
116
117 export default connect(null, mapActionsToProps, null, {withRef: true})(ComputeFlavors);