blob: 321304a6af6aa80d64066d99bacad284f7137aaf [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';
33
34const ModalStyled = styled(Modal)`
35 background-color: transparent;
36`
jingjincs3173d552020-03-04 09:11:10 +010037const LoopViewSvgDivStyled = styled.div`
38 overflow: hidden;
39 background-color: ${props => (props.theme.loopViewerBackgroundColor)};
40 border-color: ${props => (props.theme.loopViewerHeaderColor)};
41 margin-left: auto;
42 margin-right:auto;
43 text-align: center;
44`
sebdetd2a4df02020-02-26 15:47:30 -080045
46export default class CreateLoopModal extends React.Component {
47 constructor(props, context) {
48 super(props, context);
49
50 this.getTemplateNames = this.getTemplateNames.bind(this);
51 this.handleCreate = this.handleCreate.bind(this);
52 this.handleModelName = this.handleModelName.bind(this);
53 this.handleClose = this.handleClose.bind(this);
54 this.handleDropdownListChange = this.handleDropdownListChange.bind(this);
55 this.state = {
56 show: true,
jingjincs3173d552020-03-04 09:11:10 +010057 content: '',
sebdetd2a4df02020-02-26 15:47:30 -080058 chosenTemplateName: '',
59 modelName: '',
60 templateNames: []
61 };
62 }
63
64 componentWillMount() {
65 this.getTemplateNames();
66 }
67
68 handleClose() {
69 this.setState({ show: false });
70 this.props.history.push('/');
71 }
72
73 handleDropdownListChange(e) {
74 this.setState({ chosenTemplateName: e.value });
sebdet84242152020-04-02 15:31:34 +020075 TemplateService.getBlueprintMicroServiceTemplateSvg(e.value).then(svgXml => {
jingjincs3173d552020-03-04 09:11:10 +010076 if (svgXml.length !== 0) {
77 this.setState({ content: svgXml })
78 } else {
79 this.setState({ content: 'Error1' })
80 }
81 })
sebdetd2a4df02020-02-26 15:47:30 -080082 }
83
84 getTemplateNames() {
85 TemplateService.getTemplateNames().then(templateNames => {
86 const templateOptions = templateNames.map((templateName) => { return { label: templateName, value: templateName } });
87 this.setState({ templateNames: templateOptions })
88 });
89 }
90
91 handleCreate() {
92 if (!this.state.modelName) {
93 alert("A model name is required");
94 return;
95 }
jingjincs3173d552020-03-04 09:11:10 +010096 console.debug("Create Model " + this.state.modelName + ", Template " + this.state.chosenTemplateName + " is chosen");
sebdetd2a4df02020-02-26 15:47:30 -080097 this.setState({ show: false });
98 LoopService.createLoop("LOOP_" + this.state.modelName, this.state.chosenTemplateName).then(text => {
99 console.debug("CreateLoop response received: ", text);
100 try {
101 this.props.history.push('/');
102 this.props.loadLoopFunction("LOOP_" + this.state.modelName);
103 } catch(err) {
104 alert(text);
105 this.props.history.push('/');
106 }
107 })
108 .catch(error => {
109 console.debug("Create Loop failed");
110 });
sebdetd2a4df02020-02-26 15:47:30 -0800111 }
112
113 handleModelName = event => {
jingjincs3173d552020-03-04 09:11:10 +0100114 this.setState({
115 modelName: event.target.value
116 })
sebdetd2a4df02020-02-26 15:47:30 -0800117 }
118
119 render() {
120 return (
sebdetedaf4f92020-04-16 00:43:48 +0200121 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static">
sebdetd2a4df02020-02-26 15:47:30 -0800122 <Modal.Header closeButton>
123 <Modal.Title>Create Model</Modal.Title>
124 </Modal.Header>
125 <Modal.Body>
126 <Form.Group as={Row} controlId="formPlaintextEmail">
127 <Form.Label column sm="2">Template Name</Form.Label>
128 <Col sm="10">
129 <Select onChange={this.handleDropdownListChange} options={this.state.templateNames} />
130 </Col>
131 </Form.Group>
132 <Form.Group controlId="formPlaintextEmail">
jingjincs3173d552020-03-04 09:11:10 +0100133 <LoopViewSvgDivStyled dangerouslySetInnerHTML={{ __html: this.state.content }} value={this.state.content} >
134 </LoopViewSvgDivStyled>
135 </Form.Group>
136 <Form.Group controlId="formPlaintextEmail">
sebdetd2a4df02020-02-26 15:47:30 -0800137 <Form.Label column sm="2">Model Name:</Form.Label>
138 <input type="text" style={{width: '50%'}}
139 value={this.state.modelName}
140 onChange={this.handleModelName}
141 />
142 </Form.Group>
143 </Modal.Body>
144 <Modal.Footer>
145 <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
146 <Button variant="primary" type="submit" onClick={this.handleCreate}>Create</Button>
147 </Modal.Footer>
148 </ModalStyled>
sebdetd2a4df02020-02-26 15:47:30 -0800149 );
150 }
151}