sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * ONAP CLAMP |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2020 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 styled from 'styled-components'; |
| 31 | import LoopService from '../../../api/LoopService'; |
| 32 | import TemplateService from '../../../api/TemplateService'; |
| 33 | |
| 34 | const ModalStyled = styled(Modal)` |
| 35 | background-color: transparent; |
| 36 | ` |
| 37 | |
| 38 | export default class CreateLoopModal extends React.Component { |
| 39 | constructor(props, context) { |
| 40 | super(props, context); |
| 41 | |
| 42 | this.getTemplateNames = this.getTemplateNames.bind(this); |
| 43 | this.handleCreate = this.handleCreate.bind(this); |
| 44 | this.handleModelName = this.handleModelName.bind(this); |
| 45 | this.handleClose = this.handleClose.bind(this); |
| 46 | this.handleDropdownListChange = this.handleDropdownListChange.bind(this); |
| 47 | this.state = { |
| 48 | show: true, |
| 49 | chosenTemplateName: '', |
| 50 | modelName: '', |
| 51 | templateNames: [] |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | componentWillMount() { |
| 56 | this.getTemplateNames(); |
| 57 | } |
| 58 | |
| 59 | handleClose() { |
| 60 | this.setState({ show: false }); |
| 61 | this.props.history.push('/'); |
| 62 | } |
| 63 | |
| 64 | handleDropdownListChange(e) { |
| 65 | this.setState({ chosenTemplateName: e.value }); |
| 66 | } |
| 67 | |
| 68 | getTemplateNames() { |
| 69 | TemplateService.getTemplateNames().then(templateNames => { |
| 70 | const templateOptions = templateNames.map((templateName) => { return { label: templateName, value: templateName } }); |
| 71 | this.setState({ templateNames: templateOptions }) |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | handleCreate() { |
| 76 | if (!this.state.modelName) { |
| 77 | alert("A model name is required"); |
| 78 | return; |
| 79 | } |
| 80 | console.info("Create Model " + this.state.modelName + ", Template " + this.state.chosenTemplateName + " is chosen"); |
| 81 | this.setState({ show: false }); |
| 82 | LoopService.createLoop("LOOP_" + this.state.modelName, this.state.chosenTemplateName).then(text => { |
| 83 | console.debug("CreateLoop response received: ", text); |
| 84 | try { |
| 85 | this.props.history.push('/'); |
| 86 | this.props.loadLoopFunction("LOOP_" + this.state.modelName); |
| 87 | } catch(err) { |
| 88 | alert(text); |
| 89 | this.props.history.push('/'); |
| 90 | } |
| 91 | }) |
| 92 | .catch(error => { |
| 93 | console.debug("Create Loop failed"); |
| 94 | }); |
| 95 | |
| 96 | } |
| 97 | |
| 98 | handleModelName = event => { |
| 99 | this.setState({ |
| 100 | modelName: event.target.value |
| 101 | }) |
| 102 | } |
| 103 | |
| 104 | render() { |
| 105 | return ( |
| 106 | <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose}> |
| 107 | <Modal.Header closeButton> |
| 108 | <Modal.Title>Create Model</Modal.Title> |
| 109 | </Modal.Header> |
| 110 | <Modal.Body> |
| 111 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
| 112 | <Form.Label column sm="2">Template Name</Form.Label> |
| 113 | <Col sm="10"> |
| 114 | <Select onChange={this.handleDropdownListChange} options={this.state.templateNames} /> |
| 115 | </Col> |
| 116 | </Form.Group> |
| 117 | <Form.Group controlId="formPlaintextEmail"> |
| 118 | <Form.Label column sm="2">Model Name:</Form.Label> |
| 119 | <input type="text" style={{width: '50%'}} |
| 120 | value={this.state.modelName} |
| 121 | onChange={this.handleModelName} |
| 122 | /> |
| 123 | </Form.Group> |
| 124 | </Modal.Body> |
| 125 | <Modal.Footer> |
| 126 | <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> |
| 127 | <Button variant="primary" type="submit" onClick={this.handleCreate}>Create</Button> |
| 128 | </Modal.Footer> |
| 129 | </ModalStyled> |
| 130 | |
| 131 | ); |
| 132 | } |
| 133 | } |