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 | ` |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 37 | const LoopViewSvgDivStyled = styled.svg` |
| 38 | display: flex; |
| 39 | flex-direction: row; |
| 40 | overflow-x: scroll; |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 41 | background-color: ${props => (props.theme.loopViewerBackgroundColor)}; |
| 42 | border-color: ${props => (props.theme.loopViewerHeaderColor)}; |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 43 | margin-top: 3em; |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 44 | margin-left: auto; |
| 45 | margin-right:auto; |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 46 | margin-bottom: -1em; |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 47 | text-align: center; |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 48 | align-items: center; |
| 49 | height: 100%; |
| 50 | width: 100%; |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 51 | ` |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 52 | |
| 53 | export default class CreateLoopModal extends React.Component { |
| 54 | constructor(props, context) { |
| 55 | super(props, context); |
| 56 | |
| 57 | this.getTemplateNames = this.getTemplateNames.bind(this); |
| 58 | this.handleCreate = this.handleCreate.bind(this); |
| 59 | this.handleModelName = this.handleModelName.bind(this); |
| 60 | this.handleClose = this.handleClose.bind(this); |
| 61 | this.handleDropdownListChange = this.handleDropdownListChange.bind(this); |
| 62 | this.state = { |
| 63 | show: true, |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 64 | content: '', |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 65 | chosenTemplateName: '', |
| 66 | modelName: '', |
| 67 | templateNames: [] |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | componentWillMount() { |
| 72 | this.getTemplateNames(); |
| 73 | } |
| 74 | |
| 75 | handleClose() { |
| 76 | this.setState({ show: false }); |
| 77 | this.props.history.push('/'); |
| 78 | } |
| 79 | |
| 80 | handleDropdownListChange(e) { |
| 81 | this.setState({ chosenTemplateName: e.value }); |
sebdet | 8424215 | 2020-04-02 15:31:34 +0200 | [diff] [blame] | 82 | TemplateService.getBlueprintMicroServiceTemplateSvg(e.value).then(svgXml => { |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 83 | if (svgXml.length !== 0) { |
| 84 | this.setState({ content: svgXml }) |
| 85 | } else { |
| 86 | this.setState({ content: 'Error1' }) |
| 87 | } |
| 88 | }) |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | getTemplateNames() { |
| 92 | TemplateService.getTemplateNames().then(templateNames => { |
| 93 | const templateOptions = templateNames.map((templateName) => { return { label: templateName, value: templateName } }); |
| 94 | this.setState({ templateNames: templateOptions }) |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | handleCreate() { |
| 99 | if (!this.state.modelName) { |
| 100 | alert("A model name is required"); |
| 101 | return; |
| 102 | } |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 103 | console.debug("Create Model " + this.state.modelName + ", Template " + this.state.chosenTemplateName + " is chosen"); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 104 | this.setState({ show: false }); |
| 105 | LoopService.createLoop("LOOP_" + this.state.modelName, this.state.chosenTemplateName).then(text => { |
| 106 | console.debug("CreateLoop response received: ", text); |
| 107 | try { |
| 108 | this.props.history.push('/'); |
| 109 | this.props.loadLoopFunction("LOOP_" + this.state.modelName); |
| 110 | } catch(err) { |
| 111 | alert(text); |
| 112 | this.props.history.push('/'); |
| 113 | } |
| 114 | }) |
| 115 | .catch(error => { |
| 116 | console.debug("Create Loop failed"); |
| 117 | }); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | handleModelName = event => { |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 121 | this.setState({ |
| 122 | modelName: event.target.value |
| 123 | }) |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | render() { |
| 127 | return ( |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 128 | <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 129 | <Modal.Header closeButton> |
| 130 | <Modal.Title>Create Model</Modal.Title> |
| 131 | </Modal.Header> |
| 132 | <Modal.Body> |
| 133 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 134 | <Form.Label column sm="2">Template Name:</Form.Label> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 135 | <Col sm="10"> |
| 136 | <Select onChange={this.handleDropdownListChange} options={this.state.templateNames} /> |
| 137 | </Col> |
| 138 | </Form.Group> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 139 | <Form.Group as={Row} style={{alignItems: 'center'}} controlId="formSvgPreview"> |
| 140 | <Form.Label column sm="2">Model Preview:</Form.Label> |
| 141 | <Col sm="10"> |
| 142 | <LoopViewSvgDivStyled dangerouslySetInnerHTML={{ __html: this.state.content }} |
| 143 | value={this.state.content} > |
| 144 | </LoopViewSvgDivStyled> |
| 145 | </Col> |
| 146 | </Form.Group> |
| 147 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 148 | <Form.Label column sm="2">Model Name:</Form.Label> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 149 | <input type="text" style={{width: '50%', marginLeft: '1em' }} |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 150 | value={this.state.modelName} |
| 151 | onChange={this.handleModelName} |
| 152 | /> |
| 153 | </Form.Group> |
| 154 | </Modal.Body> |
| 155 | <Modal.Footer> |
| 156 | <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> |
| 157 | <Button variant="primary" type="submit" onClick={this.handleCreate}>Create</Button> |
| 158 | </Modal.Footer> |
| 159 | </ModalStyled> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 160 | ); |
| 161 | } |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 162 | } |