drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * ONAP CLAMP |
| 4 | * ================================================================================ |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 5 | * Copyright (C) 2020 AT&T Intellectual Property. All rights |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 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'; |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 29 | import PolicyToscaService from '../../../api/PolicyToscaService'; |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 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 | ` |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 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'}; |
sebdet | 81f5cab | 2019-11-06 11:40:46 +0100 | [diff] [blame] | 54 | |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 55 | export default class ViewToscalPolicyModal extends React.Component { |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 56 | |
| 57 | state = { |
| 58 | show: true, |
| 59 | content: 'Please select Tosca model to view the details', |
| 60 | selectedRow: -1, |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 61 | toscaPolicyModelNames: [], |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 62 | toscaColumns: [ |
| 63 | { title: "#", field: "index", render: rowData => rowData.tableData.id + 1, |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 64 | cellStyle: cellStyle, |
| 65 | headerStyle: headerStyle |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 66 | }, |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 67 | { title: "Policy Model Type", field: "policyModelType", |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 68 | cellStyle: cellStyle, |
| 69 | headerStyle: headerStyle |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 70 | }, |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 71 | { title: "Policy Acronym", field: "policyAcronym", |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 72 | cellStyle: cellStyle, |
| 73 | headerStyle: headerStyle |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 74 | }, |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 75 | { title: "Version", field: "version", |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 76 | cellStyle: cellStyle, |
| 77 | headerStyle: headerStyle |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 78 | }, |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 79 | { title: "Uploaded By", field: "updatedBy", |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 80 | cellStyle: cellStyle, |
| 81 | headerStyle: headerStyle |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 82 | }, |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 83 | { title: "Uploaded Date", field: "updatedDate", editable: 'never', |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 84 | cellStyle: cellStyle, |
| 85 | headerStyle: headerStyle |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 86 | } |
| 87 | ], |
| 88 | tableIcons: { |
sebdet | 81f5cab | 2019-11-06 11:40:46 +0100 | [diff] [blame] | 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} />) |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 96 | } |
| 97 | }; |
| 98 | |
| 99 | constructor(props, context) { |
| 100 | super(props, context); |
| 101 | this.handleClose = this.handleClose.bind(this); |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 102 | this.getPolicyToscaModels = this.getToscaPolicyModels.bind(this); |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 103 | this.handleYamlContent = this.handleYamlContent.bind(this); |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 104 | this.getToscaPolicyModelYaml = this.getToscaPolicyModelYaml.bind(this); |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | componentWillMount() { |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 108 | this.getToscaPolicyModels(); |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 109 | } |
| 110 | |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 111 | getToscaPolicyModels() { |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 112 | PolicyToscaService.getToscaPolicyModels().then(toscaPolicyModelNames => { |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 113 | this.setState({ toscaPolicyModelNames: toscaPolicyModelNames }); |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 114 | }); |
| 115 | } |
| 116 | |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 117 | getToscaPolicyModelYaml(policyModelType, policyModelVersion) { |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 118 | if (typeof policyModelType !== "undefined") { |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 119 | PolicyToscaService.getToscaPolicyModelYaml(policyModelType, policyModelVersion).then(toscaYaml => { |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 120 | if (toscaYaml.length !== 0) { |
| 121 | this.setState({content: toscaYaml}) |
| 122 | } else { |
| 123 | this.setState({ content: 'Please select Tosca model to view the details' }) |
| 124 | } |
| 125 | }); |
| 126 | } else { |
| 127 | this.setState({ content: 'Please select Tosca model to view the details' }) |
| 128 | } |
| 129 | } |
| 130 | |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 131 | handleYamlContent(event) { |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 132 | this.setState({ content: event.target.value }); |
| 133 | } |
| 134 | |
| 135 | handleClose() { |
| 136 | this.setState({ show: false }); |
| 137 | this.props.history.push('/'); |
| 138 | } |
| 139 | |
| 140 | render() { |
| 141 | return ( |
| 142 | <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}> |
| 143 | <Modal.Header closeButton> |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 144 | </Modal.Header> |
| 145 | <Modal.Body> |
| 146 | <MaterialTable |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 147 | title={"View Tosca Policy Models"} |
ash74268 | 3a83e2a | 2020-01-31 15:40:15 +0000 | [diff] [blame] | 148 | data={this.state.toscaPolicyModelNames} |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 149 | columns={this.state.toscaColumns} |
| 150 | icons={this.state.tableIcons} |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 151 | onRowClick={(event, rowData) => {this.getToscaPolicyModelYaml(rowData.policyModelType,rowData.version);this.setState({selectedRow: rowData.tableData.id})}} |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 152 | options={{ |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 153 | headerStyle: rowHeaderStyle, |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 154 | rowStyle: rowData => ({ |
| 155 | backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF' |
| 156 | }) |
| 157 | }} |
| 158 | /> |
sebdet | 81f5cab | 2019-11-06 11:40:46 +0100 | [diff] [blame] | 159 | <div> |
drveerendra | 684057e | 2019-11-11 19:37:39 -0500 | [diff] [blame] | 160 | <TextModal value={this.state.content} onChange={this.handleYamlContent}/> |
sebdet | 81f5cab | 2019-11-06 11:40:46 +0100 | [diff] [blame] | 161 | </div> |
drveerendra | f6b2625 | 2019-10-31 12:45:30 -0400 | [diff] [blame] | 162 | </Modal.Body> |
| 163 | <Modal.Footer> |
| 164 | <Button variant="secondary" onClick={this.handleClose}>Close</Button> |
| 165 | </Modal.Footer> |
| 166 | </ModalStyled> |
| 167 | ); |
| 168 | } |
| 169 | } |