Support disabling of Loop Instance Properties "Save" button
[clamp.git] / ui-react / src / components / dialogs / Loop / LoopPropertiesModal.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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 Button from 'react-bootstrap/Button';
25 import Modal from 'react-bootstrap/Modal';
26 import Form from 'react-bootstrap/Form';
27 import styled from 'styled-components';
28 import LoopService from '../../../api/LoopService';
29
30 const ModalStyled = styled(Modal)`
31         background-color: transparent;
32 `
33 export default class LoopPropertiesModal extends React.Component {
34
35         state = {
36                 show: true,
37                 loopCache: this.props.loopCache,
38                 temporaryPropertiesJson: JSON.parse(JSON.stringify(this.props.loopCache.getGlobalProperties()))
39         };
40
41         constructor(props, context) {
42                 super(props, context);
43
44                 this.handleClose = this.handleClose.bind(this);
45                 this.handleSave = this.handleSave.bind(this);
46                 this.handleChange = this.handleChange.bind(this);
47
48                 this.renderDcaeParameters = this.renderDcaeParameters.bind(this);
49                 this.renderAllParameters = this.renderAllParameters.bind(this);
50                 this.getDcaeParameters = this.getDcaeParameters.bind(this);
51                 this.readOnly = props.readOnly !== undefined ? props.readOnly : false;
52         }
53
54         componentWillReceiveProps(newProps) {
55                 this.setState({
56                         loopCache: newProps.loopCache,
57                         temporaryPropertiesJson: JSON.parse(JSON.stringify(newProps.loopCache.getGlobalProperties()))
58                 });
59         }
60
61         handleClose() {
62                 this.props.history.push('/');
63         }
64
65         handleSave(event) {
66                 LoopService.updateGlobalProperties(this.state.loopCache.getLoopName(), this.state.temporaryPropertiesJson).then(resp => {
67                         this.setState({ show: false });
68                         this.props.history.push('/');
69                         this.props.loadLoopFunction(this.state.loopCache.getLoopName());
70                 });
71         }
72
73         handleChange(event) {
74                 this.setState({temporaryPropertiesJson:{[event.target.name]: JSON.parse(event.target.value)}});
75         }
76
77         renderAllParameters() {
78                 return (<Modal.Body>
79                         <Form>
80                                 {this.renderDcaeParameters()}
81                         </Form>
82                 </Modal.Body>
83                 );
84         }
85
86         getDcaeParameters() {
87                 if (typeof (this.state.temporaryPropertiesJson) !== "undefined") {
88                         return JSON.stringify(this.state.temporaryPropertiesJson["dcaeDeployParameters"]);
89                 } else {
90                         return "";
91                 }
92
93         }
94
95         renderDcaeParameters() {
96                 return (
97                         <Form.Group >
98                                 <Form.Label>Deploy Parameters</Form.Label>
99                                 <Form.Control as="textarea" rows="3" name="dcaeDeployParameters" onChange={this.handleChange} defaultValue={this.getDcaeParameters()}></Form.Control>
100                         </Form.Group>
101                 );
102         }
103
104         render() {
105                 return (
106                         <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} >
107                                 <Modal.Header closeButton>
108                                         <Modal.Title>Model Properties</Modal.Title>
109                                 </Modal.Header>
110                                         {this.renderAllParameters()}
111                                 <Modal.Footer>
112                                         <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
113                                         <Button variant="primary" type="submit" disabled={this.readOnly}  onClick={this.handleSave}>Save Changes</Button>
114                                 </Modal.Footer>
115                         </ModalStyled>
116                 );
117         }
118 }