blob: 5663360a078f78f4d324604f84e03968e9621b0b [file] [log] [blame]
sebdetd2a4df02020-02-26 15:47:30 -08001/*-
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
23import React from 'react'
24import Select from 'react-select';
25import Button from 'react-bootstrap/Button';
26import Modal from 'react-bootstrap/Modal';
27import Form from 'react-bootstrap/Form';
28import Row from 'react-bootstrap/Row';
29import Col from 'react-bootstrap/Col';
30import styled from 'styled-components';
31import LoopService from '../../../api/LoopService';
32import TemplateService from '../../../api/TemplateService';
sebdetc0ec0fc2020-05-18 12:31:11 +020033import LoopCache from '../../../api/LoopCache';
34import SvgGenerator from '../../loop_viewer/svg/SvgGenerator';
sebdetd2a4df02020-02-26 15:47:30 -080035
36const ModalStyled = styled(Modal)`
37 background-color: transparent;
38`
39
Ted Humphrey083e5a22020-07-08 16:48:40 -040040const ErrMsgStyled = styled.div`
41 color: red;
42`
43
sebdetd2a4df02020-02-26 15:47:30 -080044export default class CreateLoopModal extends React.Component {
45 constructor(props, context) {
46 super(props, context);
47
sebdetc0ec0fc2020-05-18 12:31:11 +020048 this.getAllLoopTemplates = this.getAllLoopTemplates.bind(this);
sebdetd2a4df02020-02-26 15:47:30 -080049 this.handleCreate = this.handleCreate.bind(this);
50 this.handleModelName = this.handleModelName.bind(this);
51 this.handleClose = this.handleClose.bind(this);
sebdetc0ec0fc2020-05-18 12:31:11 +020052 this.handleDropDownListChange = this.handleDropDownListChange.bind(this);
Ted Humphrey083e5a22020-07-08 16:48:40 -040053 this.renderSvg = this.renderSvg.bind(this);
sebdetd2a4df02020-02-26 15:47:30 -080054 this.state = {
55 show: true,
56 chosenTemplateName: '',
Ted Humphrey083e5a22020-07-08 16:48:40 -040057 modelInputErrMsg: '',
sebdetd2a4df02020-02-26 15:47:30 -080058 modelName: '',
sebdetc0ec0fc2020-05-18 12:31:11 +020059 templateNames: [],
60 fakeLoopCacheWithTemplate: new LoopCache({})
sebdetd2a4df02020-02-26 15:47:30 -080061 };
62 }
63
Ted Humphrey083e5a22020-07-08 16:48:40 -040064 async componentDidMount() {
65 await this.getAllLoopTemplates();
66 await this.getModelNames();
sebdetd2a4df02020-02-26 15:47:30 -080067 }
68
69 handleClose() {
70 this.setState({ show: false });
71 this.props.history.push('/');
72 }
73
sebdetc0ec0fc2020-05-18 12:31:11 +020074 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 }
sebdetd2a4df02020-02-26 15:47:30 -080087 }
88
sebdetc0ec0fc2020-05-18 12:31:11 +020089 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 })
sebdetd2a4df02020-02-26 15:47:30 -080094 });
95 }
96
Ted Humphrey083e5a22020-07-08 16:48:40 -040097 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
sebdetd2a4df02020-02-26 15:47:30 -0800108 handleCreate() {
109 if (!this.state.modelName) {
110 alert("A model name is required");
111 return;
112 }
jingjincs3173d552020-03-04 09:11:10 +0100113 console.debug("Create Model " + this.state.modelName + ", Template " + this.state.chosenTemplateName + " is chosen");
sebdetd2a4df02020-02-26 15:47:30 -0800114 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 });
sebdetd2a4df02020-02-26 15:47:30 -0800128 }
129
Ted Humphrey083e5a22020-07-08 16:48:40 -0400130 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 );
sebdetd2a4df02020-02-26 15:47:30 -0800149 }
150
151 render() {
152 return (
Ted Humphreydf68db22020-04-16 17:36:17 +0000153 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} >
sebdetd2a4df02020-02-26 15:47:30 -0800154 <Modal.Header closeButton>
155 <Modal.Title>Create Model</Modal.Title>
156 </Modal.Header>
157 <Modal.Body>
158 <Form.Group as={Row} controlId="formPlaintextEmail">
Ted Humphreydf68db22020-04-16 17:36:17 +0000159 <Form.Label column sm="2">Template Name:</Form.Label>
sebdetd2a4df02020-02-26 15:47:30 -0800160 <Col sm="10">
sebdetc0ec0fc2020-05-18 12:31:11 +0200161 <Select onChange={this.handleDropDownListChange} options={this.state.templateNames} />
sebdetd2a4df02020-02-26 15:47:30 -0800162 </Col>
163 </Form.Group>
sebdetc0ec0fc2020-05-18 12:31:11 +0200164 <Form.Group as={Row} style={{alignItems: 'center'}} controlId="formSvgPreview">
165 <Form.Label column sm="2">Model Preview:</Form.Label>
166 <Col sm="10">
Ted Humphrey083e5a22020-07-08 16:48:40 -0400167 {this.renderSvg()}
sebdetc0ec0fc2020-05-18 12:31:11 +0200168 </Col>
169 </Form.Group>
Ted Humphreydf68db22020-04-16 17:36:17 +0000170 <Form.Group as={Row} controlId="formPlaintextEmail">
sebdetd2a4df02020-02-26 15:47:30 -0800171 <Form.Label column sm="2">Model Name:</Form.Label>
Ted Humphrey083e5a22020-07-08 16:48:40 -0400172 <input sm="5" type="text" style={{width: '50%', marginLeft: '1em' }}
sebdetd2a4df02020-02-26 15:47:30 -0800173 value={this.state.modelName}
174 onChange={this.handleModelName}
175 />
Ted Humphrey083e5a22020-07-08 16:48:40 -0400176 <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>
sebdetd2a4df02020-02-26 15:47:30 -0800181 </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>
sebdetd2a4df02020-02-26 15:47:30 -0800188 );
189 }
Ted Humphreydf68db22020-04-16 17:36:17 +0000190}