89e5697c985f6443fc894b4569a021de5d559ee6
[clamp.git] / ui-react / src / components / dialogs / Tosca / ViewLoopTemplatesModal.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 Button from 'react-bootstrap/Button';
26 import Modal from 'react-bootstrap/Modal';
27 import styled from 'styled-components';
28 import TemplateService from '../../../api/TemplateService';
29 import ArrowUpward from '@material-ui/icons/ArrowUpward';
30 import ChevronLeft from '@material-ui/icons/ChevronLeft';
31 import ChevronRight from '@material-ui/icons/ChevronRight';
32 import Clear from '@material-ui/icons/Clear';
33 import FirstPage from '@material-ui/icons/FirstPage';
34 import LastPage from '@material-ui/icons/LastPage';
35 import Search from '@material-ui/icons/Search';
36 import MaterialTable from "material-table";
37
38 const ModalStyled = styled(Modal)`
39         background-color: transparent;
40 `
41 const TextModal = styled.textarea`
42 margin-top: 20px;
43 white-space:pre;
44 background-color: ${props => props.theme.toscaTextareaBackgroundColor};;
45 text-align: justify;
46 font-size: ${props => props.theme.toscaTextareaFontSize};;
47 width: 100%;
48 height: 300px;
49 `
50 const cellStyle = { border: '1px solid black' };
51 const headerStyle = { backgroundColor: '#ddd',  border: '2px solid black'       };
52 const rowHeaderStyle = {backgroundColor:'#ddd',  fontSize: '15pt', text: 'bold', border: '1px solid black'};
53
54 export default class ViewLoopTemplatesModal extends React.Component {
55   state = {
56     show: true,
57                 content: 'Please select a loop template to display it',
58                 selectedRow: -1,
59                 loopTemplateData: [],
60                 loopTemplateColumnsDefinition: [
61                         { title: "#", field: "index", render: rowData => rowData.tableData.id + 1,
62                                 cellStyle: cellStyle,
63                                 headerStyle: headerStyle
64                         },
65                         { title: "Template Name", field: "name",
66                                 cellStyle: cellStyle,
67                                 headerStyle: headerStyle
68                         },
69                         { title: "Service Model Name", field: "modelService.serviceDetails.name",
70                                 cellStyle: cellStyle,
71                                 headerStyle: headerStyle
72                         },
73                         { title: "Loop Type Allowed", field: "allowedLoopType",
74                                 cellStyle: cellStyle,
75                                 headerStyle: headerStyle
76                         },
77                         { title: "# Instances Allowed", field: "maximumInstancesAllowed",
78                                 cellStyle: cellStyle,
79                                 headerStyle: headerStyle
80                         },
81                         { title: "Modified Date", field: "updatedDate", editable: 'never',
82                                 cellStyle: cellStyle,
83                                 headerStyle: headerStyle
84                         }
85                 ],
86                 tableIcons: {
87                         FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
88                         LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
89                         NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
90                         PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
91                         ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
92                         Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
93                         SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />)
94                 }
95         };
96
97         constructor(props, context) {
98                 super(props, context);
99                 this.handleClose = this.handleClose.bind(this);
100                 this.getBlueprintMicroServiceTemplates = this.getBlueprintMicroServiceTemplates.bind(this);
101                 this.handleYamlContent = this.handleYamlContent.bind(this);
102                 this.getBlueprintMicroServiceTemplates();
103         }
104
105         getBlueprintMicroServiceTemplates() {
106                 TemplateService.getBlueprintMicroServiceTemplates().then(loopTemplateData => {
107                 this.setState({ loopTemplateData: loopTemplateData })
108                 });
109         }
110
111         handleYamlContent = event => {
112                 this.setState({
113                         content: event.target.value
114                 });
115         }
116
117         handleClose() {
118                 this.setState({ show: false });
119                 this.props.history.push('/')
120         }
121
122         render() {
123     return (
124       <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}>
125         <Modal.Header closeButton>
126                                 </Modal.Header>
127                                 <Modal.Body>
128           <MaterialTable
129             title={"View Blueprint MicroService Templates"}
130             data={this.state.loopTemplateData}
131                                           columns={this.state.loopTemplateColumnsDefinition}
132                                           icons={this.state.tableIcons}
133                                           onRowClick={(event, rowData) => {this.setState({content: rowData.name, selectedRow: rowData.tableData.id})}}
134                                           options={{
135                                           headerStyle:rowHeaderStyle,
136                                           rowStyle: rowData => ({
137                                           backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF'
138             })
139           }}
140           />
141           <div>
142             <TextModal value={this.state.content} onChange={this.handleYamlContent} />
143           </div>
144         </Modal.Body>
145         <Modal.Footer>
146           <Button variant="secondary" onClick={this.handleClose}>Close</Button>
147         </Modal.Footer>
148       </ModalStyled>
149       );
150     }
151   }