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'; |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 33 | import LoopCache from '../../../api/LoopCache'; |
| 34 | import SvgGenerator from '../../loop_viewer/svg/SvgGenerator'; |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 35 | |
| 36 | const ModalStyled = styled(Modal)` |
| 37 | background-color: transparent; |
| 38 | ` |
| 39 | |
| 40 | export default class CreateLoopModal extends React.Component { |
| 41 | constructor(props, context) { |
| 42 | super(props, context); |
| 43 | |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 44 | this.getAllLoopTemplates = this.getAllLoopTemplates.bind(this); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 45 | this.handleCreate = this.handleCreate.bind(this); |
| 46 | this.handleModelName = this.handleModelName.bind(this); |
| 47 | this.handleClose = this.handleClose.bind(this); |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 48 | this.handleDropDownListChange = this.handleDropDownListChange.bind(this); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 49 | this.state = { |
| 50 | show: true, |
| 51 | chosenTemplateName: '', |
| 52 | modelName: '', |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 53 | templateNames: [], |
| 54 | fakeLoopCacheWithTemplate: new LoopCache({}) |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 55 | }; |
| 56 | } |
| 57 | |
| 58 | componentWillMount() { |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 59 | this.getAllLoopTemplates(); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | handleClose() { |
| 63 | this.setState({ show: false }); |
| 64 | this.props.history.push('/'); |
| 65 | } |
| 66 | |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 67 | handleDropDownListChange(e) { |
| 68 | if (typeof e.value !== "undefined") { |
| 69 | this.setState({ |
| 70 | fakeLoopCacheWithTemplate: |
| 71 | new LoopCache({ |
| 72 | "loopTemplate":e.templateObject, |
| 73 | "name": "fakeLoop" |
| 74 | }), |
| 75 | chosenTemplateName: e.value |
| 76 | }) |
| 77 | } else { |
| 78 | this.setState({ fakeLoopCacheWithTemplate: new LoopCache({}) }) |
| 79 | } |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 80 | } |
| 81 | |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 82 | getAllLoopTemplates() { |
| 83 | TemplateService.getAllLoopTemplates().then(templatesData => { |
| 84 | const templateOptions = templatesData.map((templateData) => { return { label: templateData.name, value: templateData.name, templateObject: templateData } }); |
| 85 | this.setState({ |
| 86 | templateNames: templateOptions }) |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 87 | }); |
| 88 | } |
| 89 | |
| 90 | handleCreate() { |
| 91 | if (!this.state.modelName) { |
| 92 | alert("A model name is required"); |
| 93 | return; |
| 94 | } |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 95 | console.debug("Create Model " + this.state.modelName + ", Template " + this.state.chosenTemplateName + " is chosen"); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 96 | this.setState({ show: false }); |
| 97 | LoopService.createLoop("LOOP_" + this.state.modelName, this.state.chosenTemplateName).then(text => { |
| 98 | console.debug("CreateLoop response received: ", text); |
| 99 | try { |
| 100 | this.props.history.push('/'); |
| 101 | this.props.loadLoopFunction("LOOP_" + this.state.modelName); |
| 102 | } catch(err) { |
| 103 | alert(text); |
| 104 | this.props.history.push('/'); |
| 105 | } |
| 106 | }) |
| 107 | .catch(error => { |
| 108 | console.debug("Create Loop failed"); |
| 109 | }); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | handleModelName = event => { |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 113 | this.setState({ |
| 114 | modelName: event.target.value |
| 115 | }) |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | render() { |
| 119 | return ( |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 120 | <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] | 121 | <Modal.Header closeButton> |
| 122 | <Modal.Title>Create Model</Modal.Title> |
| 123 | </Modal.Header> |
| 124 | <Modal.Body> |
| 125 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 126 | <Form.Label column sm="2">Template Name:</Form.Label> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 127 | <Col sm="10"> |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 128 | <Select onChange={this.handleDropDownListChange} options={this.state.templateNames} /> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 129 | </Col> |
| 130 | </Form.Group> |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 131 | <Form.Group as={Row} style={{alignItems: 'center'}} controlId="formSvgPreview"> |
| 132 | <Form.Label column sm="2">Model Preview:</Form.Label> |
| 133 | <Col sm="10"> |
| 134 | <SvgGenerator loopCache={this.state.fakeLoopCacheWithTemplate} clickable={false} generatedFrom={SvgGenerator.GENERATED_FROM_TEMPLATE}/> |
| 135 | </Col> |
| 136 | </Form.Group> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 137 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 138 | <Form.Label column sm="2">Model Name:</Form.Label> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 139 | <input type="text" style={{width: '50%', marginLeft: '1em' }} |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 140 | value={this.state.modelName} |
| 141 | onChange={this.handleModelName} |
| 142 | /> |
| 143 | </Form.Group> |
| 144 | </Modal.Body> |
| 145 | <Modal.Footer> |
| 146 | <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> |
| 147 | <Button variant="primary" type="submit" onClick={this.handleCreate}>Create</Button> |
| 148 | </Modal.Footer> |
| 149 | </ModalStyled> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 150 | ); |
| 151 | } |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 152 | } |