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 | |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 40 | const ErrMsgStyled = styled.div` |
| 41 | color: red; |
| 42 | ` |
| 43 | |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 44 | export default class CreateLoopModal extends React.Component { |
| 45 | constructor(props, context) { |
| 46 | super(props, context); |
| 47 | |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 48 | this.getAllLoopTemplates = this.getAllLoopTemplates.bind(this); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 49 | this.handleCreate = this.handleCreate.bind(this); |
| 50 | this.handleModelName = this.handleModelName.bind(this); |
| 51 | this.handleClose = this.handleClose.bind(this); |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 52 | this.handleDropDownListChange = this.handleDropDownListChange.bind(this); |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 53 | this.renderSvg = this.renderSvg.bind(this); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 54 | this.state = { |
| 55 | show: true, |
| 56 | chosenTemplateName: '', |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 57 | modelInputErrMsg: '', |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 58 | modelName: '', |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 59 | templateNames: [], |
| 60 | fakeLoopCacheWithTemplate: new LoopCache({}) |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 61 | }; |
| 62 | } |
| 63 | |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 64 | async componentDidMount() { |
| 65 | await this.getAllLoopTemplates(); |
| 66 | await this.getModelNames(); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | handleClose() { |
| 70 | this.setState({ show: false }); |
| 71 | this.props.history.push('/'); |
| 72 | } |
| 73 | |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 74 | handleDropDownListChange(e) { |
| 75 | if (typeof e.value !== "undefined") { |
| 76 | this.setState({ |
| 77 | fakeLoopCacheWithTemplate: |
| 78 | new LoopCache({ |
| 79 | "loopTemplate":e.templateObject, |
| 80 | "name": "fakeLoop" |
| 81 | }), |
| 82 | chosenTemplateName: e.value |
| 83 | }) |
| 84 | } else { |
| 85 | this.setState({ fakeLoopCacheWithTemplate: new LoopCache({}) }) |
| 86 | } |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 87 | } |
| 88 | |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 89 | getAllLoopTemplates() { |
| 90 | TemplateService.getAllLoopTemplates().then(templatesData => { |
| 91 | const templateOptions = templatesData.map((templateData) => { return { label: templateData.name, value: templateData.name, templateObject: templateData } }); |
| 92 | this.setState({ |
| 93 | templateNames: templateOptions }) |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 94 | }); |
| 95 | } |
| 96 | |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 97 | getModelNames() { |
| 98 | TemplateService.getLoopNames().then(loopNames => { |
| 99 | if (!loopNames) { |
| 100 | loopNames = []; |
| 101 | } |
| 102 | // Remove LOOP_ prefix |
| 103 | let trimmedLoopNames = loopNames.map(str => str.replace('LOOP_', '')); |
| 104 | this.setState({ modelNames: trimmedLoopNames }); |
| 105 | }); |
| 106 | } |
| 107 | |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 108 | handleCreate() { |
| 109 | if (!this.state.modelName) { |
| 110 | alert("A model name is required"); |
| 111 | return; |
| 112 | } |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 113 | console.debug("Create Model " + this.state.modelName + ", Template " + this.state.chosenTemplateName + " is chosen"); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 114 | this.setState({ show: false }); |
| 115 | LoopService.createLoop("LOOP_" + this.state.modelName, this.state.chosenTemplateName).then(text => { |
| 116 | console.debug("CreateLoop response received: ", text); |
| 117 | try { |
| 118 | this.props.history.push('/'); |
| 119 | this.props.loadLoopFunction("LOOP_" + this.state.modelName); |
| 120 | } catch(err) { |
| 121 | alert(text); |
| 122 | this.props.history.push('/'); |
| 123 | } |
| 124 | }) |
| 125 | .catch(error => { |
| 126 | console.debug("Create Loop failed"); |
| 127 | }); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 130 | handleModelName(event) { |
| 131 | if (this.state.modelNames.includes(event.target.value)) { |
| 132 | this.setState({ |
| 133 | modelInputErrMsg: 'A model named "' + event.target.value + '" already exists. Please pick another name.', |
| 134 | modelName: event.target.value |
| 135 | }); |
| 136 | return; |
| 137 | } else { |
| 138 | this.setState({ |
| 139 | modelInputErrMsg: '', |
| 140 | modelName: event.target.value |
| 141 | }); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | renderSvg() { |
| 146 | return ( |
| 147 | <SvgGenerator loopCache={this.state.fakeLoopCacheWithTemplate} clickable={false} generatedFrom={SvgGenerator.GENERATED_FROM_TEMPLATE}/> |
| 148 | ); |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | render() { |
| 152 | return ( |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 153 | <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] | 154 | <Modal.Header closeButton> |
| 155 | <Modal.Title>Create Model</Modal.Title> |
| 156 | </Modal.Header> |
| 157 | <Modal.Body> |
| 158 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 159 | <Form.Label column sm="2">Template Name:</Form.Label> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 160 | <Col sm="10"> |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 161 | <Select onChange={this.handleDropDownListChange} options={this.state.templateNames} /> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 162 | </Col> |
| 163 | </Form.Group> |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 164 | <Form.Group as={Row} style={{alignItems: 'center'}} controlId="formSvgPreview"> |
| 165 | <Form.Label column sm="2">Model Preview:</Form.Label> |
| 166 | <Col sm="10"> |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 167 | {this.renderSvg()} |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 168 | </Col> |
| 169 | </Form.Group> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 170 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 171 | <Form.Label column sm="2">Model Name:</Form.Label> |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 172 | <input sm="5" type="text" style={{width: '50%', marginLeft: '1em' }} |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 173 | value={this.state.modelName} |
| 174 | onChange={this.handleModelName} |
| 175 | /> |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 176 | <span sm="5"/> |
| 177 | </Form.Group> |
| 178 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
| 179 | <Form.Label column sm="2"> </Form.Label> |
| 180 | <ErrMsgStyled>{this.state.modelInputErrMsg}</ErrMsgStyled> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 181 | </Form.Group> |
| 182 | </Modal.Body> |
| 183 | <Modal.Footer> |
| 184 | <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> |
| 185 | <Button variant="primary" type="submit" onClick={this.handleCreate}>Create</Button> |
| 186 | </Modal.Footer> |
| 187 | </ModalStyled> |
sebdet | d2a4df0 | 2020-02-26 15:47:30 -0800 | [diff] [blame] | 188 | ); |
| 189 | } |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 190 | } |