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