Add template name to UI
[clamp.git] / ui-react / src / components / dialogs / Loop / OpenLoopModal.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END============================================
19  * ===================================================================
20  *
21  */
22
23 import React from 'react'
24 import Select from 'react-select';
25 import Button from 'react-bootstrap/Button';
26 import Modal from 'react-bootstrap/Modal';
27 import Form from 'react-bootstrap/Form';
28 import Row from 'react-bootstrap/Row';
29 import Col from 'react-bootstrap/Col';
30 import FormCheck from 'react-bootstrap/FormCheck'
31 import styled from 'styled-components';
32 import LoopService from '../../../api/LoopService';
33
34 const ModalStyled = styled(Modal)`
35         background-color: transparent;
36 `
37 const CheckBoxStyled = styled(FormCheck.Input)`
38         margin-left:3rem;
39 `
40 const LoopViewSvgDivStyled = styled.div`
41         overflow: hidden;
42         background-color: ${props => (props.theme.loopViewerBackgroundColor)};
43         border-color: ${props => (props.theme.loopViewerHeaderColor)};
44         margin-left: auto;
45         margin-right:auto;
46         text-align: center;
47 `
48
49 export default class OpenLoopModal extends React.Component {
50         constructor(props, context) {
51                 super(props, context);
52
53                 this.getLoopNames = this.getLoopNames.bind(this);
54                 this.handleOpen = this.handleOpen.bind(this);
55                 this.handleClose = this.handleClose.bind(this);
56                 this.handleDropdownListChange = this.handleDropdownListChange.bind(this);
57                 this.state = {
58                         show: true,
59                         chosenLoopName: '',
60                         loopNames: [],
61                         content:''
62                 };
63         }
64
65         componentWillMount() {
66                 this.getLoopNames();
67         }
68
69         handleClose() {
70                 this.setState({ show: false });
71                 this.props.history.push('/');
72         }
73
74         handleDropdownListChange(e) {
75                 this.setState({ chosenLoopName: e.value });
76                 LoopService.getSvg(e.value).then(svgXml => {
77                         if (svgXml.length !== 0) {
78                                 this.setState({ content: svgXml })
79                         } else {
80                                 this.setState({ content: 'Error1' })
81                         }
82                 });
83         }
84
85         getLoopNames() {
86                 LoopService.getLoopNames().then(loopNames => {
87                     if (Object.entries(loopNames).length !== 0) {
88                         const loopOptions = loopNames.filter(loopName => loopName!=='undefined').map((loopName) => { return { label: loopName, value: loopName } });
89                 this.setState({ loopNames: loopOptions })
90
91                     }
92
93                 });
94         }
95
96         handleOpen() {
97                 console.info("Loop " + this.state.chosenLoopName + " is chosen");
98         this.handleClose();
99                 this.props.loadLoopFunction(this.state.chosenLoopName);
100         }
101
102         render() {
103                 return (
104                         <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static">
105                                 <Modal.Header closeButton>
106                                         <Modal.Title>Open Model</Modal.Title>
107                                 </Modal.Header>
108                                 <Modal.Body>
109                                         <Form.Group as={Row} controlId="formPlaintextEmail">
110                                                 <Form.Label column sm="2">Model Name</Form.Label>
111                                                 <Col sm="10">
112                                                         <Select onChange={this.handleDropdownListChange}
113                                                         options={this.state.loopNames} />
114                                                 </Col>
115                                         </Form.Group>
116                                         <Form.Group controlId="formPlaintextEmail">
117                          <LoopViewSvgDivStyled dangerouslySetInnerHTML={{ __html: this.state.content }}   value={this.state.content} >
118                          </LoopViewSvgDivStyled>
119                     </Form.Group>
120                                         <Form.Group controlId="formBasicChecbox">
121                                                 <Form.Check>
122                                                         <FormCheck.Label>Read Only</FormCheck.Label>
123                                                         <CheckBoxStyled type="checkbox" />
124                                                 </Form.Check>
125                                         </Form.Group>
126                                 </Modal.Body>
127                                 <Modal.Footer>
128                                         <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
129                                         <Button variant="primary" type="submit" onClick={this.handleOpen}>Open</Button>
130                                 </Modal.Footer>
131                         </ModalStyled>
132
133                 );
134         }
135 }