Adding viewToscamodels menu
[clamp.git] / ui-react / src / components / dialogs / ViewToscaModals / ViewToscaModals.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 TemplateMenu from '../../../api/TemplateMenu';
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 StyledToscaView = styled.textarea`
43 `
44 const StyledToscaDiv = styled.div`
45 `
46 const vtmCellStyle = { border: '1px solid black' };
47 const vtmHeaderStyle = { backgroundColor: '#ddd',       border: '2px solid black'       };
48 const vtmRowHeaderStyle = {backgroundColor:'#ddd',  fontSize: '15pt', text: 'bold', border: '1px solid black'};
49
50 export default class ViewToscalModals extends React.Component {
51
52         state = {
53                 show: true,
54                 content: 'Please select Tosca model to view the details',
55                 selectedRow: -1,
56                 toscaNames: [],
57                 toscaColumns: [
58                         { title: "#", field: "index", render: rowData => rowData.tableData.id + 1,
59                                 cellStyle: vtmCellStyle,
60                                 headerStyle: vtmHeaderStyle
61                         },
62                         { title: "Micro Service Name", field: "toscaModelName",
63                                 cellStyle: vtmCellStyle,
64                                 headerStyle: vtmHeaderStyle
65                         },
66                         { title: "PolicyType", field: "policyType",
67                                 cellStyle: vtmCellStyle,
68                                 headerStyle: vtmHeaderStyle
69                         },
70                         { title: "Version", field: "version",
71                                 cellStyle: vtmCellStyle,
72                                 headerStyle: vtmHeaderStyle
73                         },
74                         { title: "Uploaded By", field: "userId",
75                                 cellStyle: vtmCellStyle,
76                                 headerStyle: vtmHeaderStyle
77                         },
78                         { title: "Uploaded Date", field: "lastUpdatedDate", editable: 'never',
79                                 cellStyle: vtmCellStyle,
80                                 headerStyle: vtmHeaderStyle
81                         }
82                 ],
83                 tableIcons: {
84       FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
85       LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
86       NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
87       PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
88       ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
89       Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
90       SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />),
91                 }
92         };
93
94         constructor(props, context) {
95                 super(props, context);
96                 this.handleClose = this.handleClose.bind(this);
97                 this.getToscaModals = this.getToscaModals.bind(this);
98                 this.handleYamlContent = this.handleYamlContent.bind(this);
99         }
100
101         componentWillMount() {
102                 this.getToscaModals();
103         }
104
105         getToscaModals() {
106                 TemplateMenu.getToscaModals().then(toscaNames => {
107                         this.setState({ toscaNames: toscaNames });
108                 });
109         }
110
111         handleYamlContent(event) {
112                 console.log('inside handleYamlContent');
113                 this.setState({ content: event.target.value });
114         }
115
116         handleClose() {
117                 this.setState({ show: false });
118                 this.props.history.push('/');
119         }
120
121         render() {
122                 return (
123                         <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}>
124                                 <Modal.Header closeButton>
125                                         <Modal.Title className="title">View Tosca Model</Modal.Title>
126                                 </Modal.Header>
127                                 <Modal.Body>
128                                         <MaterialTable
129                                         title={"View ToscaModel"}
130                                         data={this.state.toscaNames}
131                                         columns={this.state.toscaColumns}
132                                         icons={this.state.tableIcons}
133                                         onRowClick={(event, rowData) => {this.setState({content: rowData.toscaModelRevisions[0].toscaModelYaml, selectedRow: rowData.tableData.id})}}
134                                         options={{
135                                                 headerStyle: vtmRowHeaderStyle,
136                                                 rowStyle: rowData => ({
137                                                         backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF'
138                                                 })
139                                         }}
140                                         />
141                                         <StyledToscaDiv>
142                                                 <StyledToscaView value={this.state.content} onChange={this.handleYamlContent}/>
143                                         </StyledToscaDiv>
144                                 </Modal.Body>
145                                 <Modal.Footer>
146                                         <Button variant="secondary" onClick={this.handleClose}>Close</Button>
147                                 </Modal.Footer>
148                         </ModalStyled>
149                 );
150         }
151 }