Minor UI fixes for dialogues and Tosca upload feature
[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.svg`
41         overflow-x: scroll;
42         display: flex;
43         flex-direction: row;
44         background-color: ${props => (props.theme.loopViewerBackgroundColor)};
45         border-color: ${props => (props.theme.loopViewerHeaderColor)};
46         margin-top: 2em;
47         margin-left: auto;
48         margin-right:auto;
49         margin-bottom: -3em;
50         text-align: center;
51         align-items: center;
52         height: 100%;
53         width: 100%;
54 `
55
56 export default class OpenLoopModal extends React.Component {
57         constructor(props, context) {
58                 super(props, context);
59
60                 this.getLoopNames = this.getLoopNames.bind(this);
61                 this.handleOpen = this.handleOpen.bind(this);
62                 this.handleClose = this.handleClose.bind(this);
63                 this.handleDropdownListChange = this.handleDropdownListChange.bind(this);
64                 this.showReadOnly = props.showReadOnly ? props.showReadOnly : true;
65                 this.state = {
66                         show: true,
67                         chosenLoopName: '',
68                         loopNames: [],
69                         content:''
70                 };
71         }
72
73         componentWillMount() {
74                 this.getLoopNames();
75         }
76
77         handleClose() {
78                 this.setState({ show: false });
79                 this.props.history.push('/');
80         }
81
82         handleDropdownListChange(e) {
83                 this.setState({ chosenLoopName: e.value });
84                 LoopService.getSvg(e.value).then(svgXml => {
85                         if (svgXml.length !== 0) {
86                                 this.setState({ content: svgXml })
87                         } else {
88                                 this.setState({ content: 'Error1' })
89                         }
90                 });
91         }
92
93         getLoopNames() {
94                 LoopService.getLoopNames().then(loopNames => {
95                     if (Object.entries(loopNames).length !== 0) {
96                         const loopOptions = loopNames.filter(loopName => loopName!=='undefined').map((loopName) => { return { label: loopName, value: loopName } });
97                 this.setState({ loopNames: loopOptions })
98
99                     }
100
101                 });
102         }
103
104         handleOpen() {
105                 console.info("Loop " + this.state.chosenLoopName + " is chosen");
106         this.handleClose();
107                 this.props.loadLoopFunction(this.state.chosenLoopName);
108         }
109
110         render() {
111                 return (
112                         <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} >
113                                 <Modal.Header closeButton>
114                                         <Modal.Title>Open Model</Modal.Title>
115                                 </Modal.Header>
116                                 <Modal.Body>
117                                         <Form.Group as={Row} controlId="formPlaintextEmail">
118                                                 <Form.Label column sm="2">Model Name:</Form.Label>
119                                                 <Col sm="10">
120                                                         <Select onChange={this.handleDropdownListChange}
121                                                         options={this.state.loopNames} />
122                                                 </Col>
123                                         </Form.Group>
124                                         <Form.Group as={Row} style={{alignItems: 'center'}} controlId="formSvgPreview">
125                                                 <Form.Label column sm="2">Model Preview:</Form.Label>
126                                                 <Col sm="10">
127                                                         <LoopViewSvgDivStyled dangerouslySetInnerHTML={{ __html: this.state.content }}
128                                                                 value={this.state.content} >
129                                                         </LoopViewSvgDivStyled>
130                                                 </Col>
131                                         </Form.Group>
132                                         {this.showReadOnly === true ?
133                                                 <Form.Group as={Row} controlId="formBasicChecbox">
134                                                         <Form.Check>
135                                                                 <FormCheck.Label>Read Only Mode:</FormCheck.Label>
136                                                                 <CheckBoxStyled style={{marginLeft: '3.5em'}} type="checkbox" />
137                                                         </Form.Check>
138                                                 </Form.Group>
139                                         : null}
140                                 </Modal.Body>
141                                 <Modal.Footer>
142                                         <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
143                                         <Button variant="primary" type="submit" onClick={this.handleOpen}>Open</Button>
144                                 </Modal.Footer>
145                         </ModalStyled>
146
147                 );
148         }
149 }