6986209d433888c8c3960c339beac9153058c955
[clamp.git] / ui-react / src / components / dialogs / OpenLoop / 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
41 export default class OpenLoopModal extends React.Component {
42         constructor(props, context) {
43                 super(props, context);
44
45                 this.getLoopNames = this.getLoopNames.bind(this);
46                 this.handleOpen = this.handleOpen.bind(this);
47                 this.getModel = this.getModel.bind(this);
48                 this.handleClose = this.handleClose.bind(this);
49                 this.handleDropdownListChange = this.handleDropdownListChange.bind(this);
50                 this.state = {
51                         show: true,
52                         chosenLoopName: '',
53                         loopNames: []
54                 };
55         }
56
57         componentDidMount() {
58                 this.getLoopNames();
59         }
60
61         handleClose() {
62                 this.setState({ show: false });
63                 this.props.history.push('/');
64         }
65
66         handleDropdownListChange(e) {
67                 this.setState({ chosenLoopName: e.value });
68         }
69
70         getLoopNames() {
71                 LoopService.getLoopNames().then(loopNames => {
72                         const loopOptions = loopNames.map((loopName) => { return { label: loopName, value: loopName } });
73                         this.setState({ loopNames: loopOptions })
74                 });
75         }
76
77         getModel() {
78                 LoopService.getLoop(this.state.chosenLoopName).then(loop => {
79                         console.debug("Settingloop cache with json");
80                         this.props.updateLoopCacheFunction(loop);
81                 });
82         }
83
84         handleOpen() {
85                 console.info("Loop " + this.state.chosenLoopName + " is chosen");
86                 this.setState({ show: false });
87                 this.props.history.push('/');
88                 this.getModel();
89         }
90
91         render() {
92                 return (
93                         <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose}>
94                                 <Modal.Header closeButton>
95                                         <Modal.Title>Open Model</Modal.Title>
96                                 </Modal.Header>
97                                 <Modal.Body>
98                                         <Form.Group as={Row} controlId="formPlaintextEmail">
99                                                 <Form.Label column sm="2">Model Name</Form.Label>
100                                                 <Col sm="10">
101                                                         <Select onChange={this.handleDropdownListChange} options={this.state.loopNames} />
102                                                 </Col>
103                                         </Form.Group>
104                                         <Form.Group controlId="formBasicChecbox">
105                                                 <Form.Check>
106                                                         <FormCheck.Label>Read Only</FormCheck.Label>
107                                                         <CheckBoxStyled type="checkbox" />
108                                                 </Form.Check>
109                                         </Form.Group>
110                                 </Modal.Body>
111                                 <Modal.Footer>
112                                         <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
113                                         <Button variant="primary" type="submit" onClick={this.handleOpen}>OK</Button>
114                                 </Modal.Footer>
115                         </ModalStyled>
116
117                 );
118         }
119 }