Change Oparent
[clamp.git] / ui-react / src / components / dialogs / Tosca / ViewToscaPolicyModal.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 import React, { forwardRef } from 'react'
25 import MaterialTable from "material-table";
26 import Button from 'react-bootstrap/Button';
27 import Modal from 'react-bootstrap/Modal';
28 import styled from 'styled-components';
29 import TemplateMenuService from '../../../api/TemplateMenuService';
30 import ArrowUpward from '@material-ui/icons/ArrowUpward';
31 import ChevronLeft from '@material-ui/icons/ChevronLeft';
32 import ChevronRight from '@material-ui/icons/ChevronRight';
33 import Clear from '@material-ui/icons/Clear';
34 import FirstPage from '@material-ui/icons/FirstPage';
35 import LastPage from '@material-ui/icons/LastPage';
36 import Search from '@material-ui/icons/Search';
37
38
39 const ModalStyled = styled(Modal)`
40         background-color: transparent;
41 `
42 const TextModal = styled.textarea`
43         margin-top: 20px;
44         white-space:pre;
45         background-color: ${props => props.theme.toscaTextareaBackgroundColor};
46         text-align: justify;
47         font-size: ${props => props.theme.toscaTextareaFontSize};
48         width: 100%;
49         height: 300px;
50 `
51 const cellStyle = { border: '1px solid black' };
52 const headerStyle = { backgroundColor: '#ddd',  border: '2px solid black'       };
53 const rowHeaderStyle = {backgroundColor:'#ddd',  fontSize: '15pt', text: 'bold', border: '1px solid black'};
54
55 export default class ViewToscalPolicyModal extends React.Component {
56
57         state = {
58                 show: true,
59                 content: 'Please select Tosca model to view the details',
60                 selectedRow: -1,
61                 toscaNames: [],
62                 toscaColumns: [
63                         { title: "#", field: "index", render: rowData => rowData.tableData.id + 1,
64                                 cellStyle: cellStyle,
65                                 headerStyle: headerStyle
66                         },
67                         { title: "Micro Service Name", field: "toscaModelName",
68                                 cellStyle: cellStyle,
69                                 headerStyle: headerStyle
70                         },
71                         { title: "PolicyType", field: "policyType",
72                                 cellStyle: cellStyle,
73                                 headerStyle: headerStyle
74                         },
75                         { title: "Version", field: "toscaModelRevisions[0].version",
76                                 cellStyle: cellStyle,
77                                 headerStyle: headerStyle
78                         },
79                         { title: "Uploaded By", field: "userId",
80                                 cellStyle: cellStyle,
81                                 headerStyle: headerStyle
82                         },
83                         { title: "Uploaded Date", field: "lastUpdatedDate", editable: 'never',
84                                 cellStyle: cellStyle,
85                                 headerStyle: headerStyle
86                         }
87                 ],
88                 tableIcons: {
89                     FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
90                     LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
91                     NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
92                     PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
93                     ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
94                     Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
95                     SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />)
96                 }
97         };
98
99         constructor(props, context) {
100                 super(props, context);
101                 this.handleClose = this.handleClose.bind(this);
102                 this.getPolicyToscaModels = this.getToscaPolicyModels.bind(this);
103                 this.handleYamlContent = this.handleYamlContent.bind(this);
104         }
105
106         componentWillMount() {
107                 this.getToscaPolicyModels();
108         }
109
110         getToscaPolicyModels() {
111             TemplateMenuService.getToscaPolicyModels().then(toscaNames => {
112                         this.setState({ toscaNames: toscaNames });
113                 });
114         }
115
116         handleYamlContent(event) {
117                 this.setState({ content: event.target.value });
118         }
119
120         handleClose() {
121                 this.setState({ show: false });
122                 this.props.history.push('/');
123         }
124
125         render() {
126                 return (
127                         <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}>
128                                 <Modal.Header closeButton>
129                                 </Modal.Header>
130                                 <Modal.Body>
131                                         <MaterialTable
132                                         title={"View Tosca Policy Models"}
133                                         data={this.state.toscaNames}
134                                         columns={this.state.toscaColumns}
135                                         icons={this.state.tableIcons}
136                                         onRowClick={(event, rowData) => {this.setState({content: rowData.toscaModelRevisions[0].toscaModelYaml, selectedRow: rowData.tableData.id})}}
137                                         options={{
138                                                 headerStyle: rowHeaderStyle,
139                                                 rowStyle: rowData => ({
140                                                         backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF'
141                                                 })
142                                         }}
143                                         />
144                                         <div>
145                                                 <TextModal value={this.state.content} onChange={this.handleYamlContent}/>
146                                         </div>
147                                 </Modal.Body>
148                                 <Modal.Footer>
149                                         <Button variant="secondary" onClick={this.handleClose}>Close</Button>
150                                 </Modal.Footer>
151                         </ModalStyled>
152                 );
153         }
154 }