YANG Model update for A1 Adapter
[ccsdk/features.git] / sdnr / wt / odlux / apps / mediatorApp / src / components / editMediatorServerDialog.tsx
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 import * as React from 'react';
19
20 import Button from '@material-ui/core/Button';
21 import TextField from '@material-ui/core/TextField';
22 import Dialog from '@material-ui/core/Dialog';
23 import DialogActions from '@material-ui/core/DialogActions';
24 import DialogContent from '@material-ui/core/DialogContent';
25 import DialogContentText from '@material-ui/core/DialogContentText';
26 import DialogTitle from '@material-ui/core/DialogTitle';
27
28 import { IDispatcher, connect, Connect } from '../../../../framework/src/flux/connect';
29
30 import { addAvaliableMediatorServerAsyncActionCreator, removeAvaliableMediatorServerAsyncActionCreator, updateAvaliableMediatorServerAsyncActionCreator } from '../actions/avaliableMediatorServersActions';
31 import { MediatorServer } from '../models/mediatorServer';
32
33 export enum EditMediatorServerDialogMode {
34   None = "none",
35   AddMediatorServer = "addMediatorServer",
36   EditMediatorServer = "editMediatorServer",
37   RemoveMediatorServer = "removeMediatorServer",
38 }
39
40 const mapDispatch = (dispatcher: IDispatcher) => ({
41   addMediatorServer: (element: MediatorServer) => {
42     dispatcher.dispatch(addAvaliableMediatorServerAsyncActionCreator(element));
43   },
44   updateMediatorServer: (element: MediatorServer) => {
45     dispatcher.dispatch(updateAvaliableMediatorServerAsyncActionCreator(element));
46   },
47   removeMediatorServer: (element: MediatorServer) => {
48     dispatcher.dispatch(removeAvaliableMediatorServerAsyncActionCreator(element));
49   },
50 });
51
52 type DialogSettings = {
53   dialogTitle: string;
54   dialogDescription: string;
55   applyButtonText: string;
56   cancelButtonText: string;
57   readonly: boolean;
58 };
59
60 const settings: { [key: string]: DialogSettings } = {
61   [EditMediatorServerDialogMode.None]: {
62     dialogTitle: "",
63     dialogDescription: "",
64     applyButtonText: "",
65     cancelButtonText: "",
66     readonly: true,
67   },
68   [EditMediatorServerDialogMode.AddMediatorServer]: {
69     dialogTitle: "Add Mediator Server",
70     dialogDescription: "",
71     applyButtonText: "Add",
72     cancelButtonText: "Cancel",
73     readonly: false,
74   },
75   [EditMediatorServerDialogMode.EditMediatorServer]: {
76     dialogTitle: "Edit Mediator Server",
77     dialogDescription: "",
78     applyButtonText: "Update",
79     cancelButtonText: "Cancel",
80     readonly: false,
81   },
82   [EditMediatorServerDialogMode.RemoveMediatorServer]: {
83     dialogTitle: "Remove Mediator Server",
84     dialogDescription: "",
85     applyButtonText: "Remove",
86     cancelButtonText: "Cancel",
87     readonly: true,
88   },
89 };
90
91 type EditMediatorServerDialogComponentProps = Connect<undefined, typeof mapDispatch> & {
92   mode: EditMediatorServerDialogMode;
93   mediatorServer: MediatorServer;
94   onClose: () => void;
95 };
96
97 type EditMediatorServerDialogComponentState = MediatorServer;
98
99 class EditMediatorServerDialogComponent extends React.Component<EditMediatorServerDialogComponentProps, EditMediatorServerDialogComponentState> {
100   constructor (props: EditMediatorServerDialogComponentProps) {
101     super(props);
102
103     this.state = {
104       ...this.props.mediatorServer
105     };
106   }
107
108   render(): JSX.Element {
109     const setting = settings[this.props.mode];
110     return (
111       <Dialog open={this.props.mode !== EditMediatorServerDialogMode.None}>
112         <DialogTitle id="form-dialog-title">{setting.dialogTitle}</DialogTitle>
113         <DialogContent>
114           <DialogContentText>
115             {setting.dialogDescription}
116           </DialogContentText>
117           {/* <TextField disabled spellCheck={false} autoFocus margin="dense" id="id" label="Id" type="text" fullWidth value={ this.state._id } onChange={(event)=>{ this.setState({_id: event.target.value}); } } /> */}
118           <TextField disabled={setting.readonly} spellCheck={false} margin="dense" id="name" label="Name" type="text" fullWidth value={this.state.name} onChange={(event) => { this.setState({ name: event.target.value }); }} />
119           <TextField disabled={setting.readonly} spellCheck={false} margin="dense" id="url" label="Url" type="text" fullWidth value={this.state.url} onChange={(event) => { this.setState({ url: event.target.value }); }} />
120         </DialogContent>
121         <DialogActions>
122           <Button onClick={(event) => {
123             this.onApply({
124               _id: this.state._id,
125               name: this.state.name,
126               url: this.state.url,
127             });
128             event.preventDefault();
129             event.stopPropagation();
130           }} > {setting.applyButtonText} </Button>
131           <Button onClick={(event) => {
132             this.onCancel();
133             event.preventDefault();
134             event.stopPropagation();
135           }} color="secondary"> {setting.cancelButtonText} </Button>
136         </DialogActions>
137       </Dialog>
138     )
139   }
140
141   private onApply = (element: MediatorServer) => {
142     this.props.onClose && this.props.onClose();
143     switch (this.props.mode) {
144       case EditMediatorServerDialogMode.AddMediatorServer:
145         element && this.props.addMediatorServer(element);
146         break;
147       case EditMediatorServerDialogMode.EditMediatorServer:
148         element && this.props.updateMediatorServer(element);
149         break;
150       case EditMediatorServerDialogMode.RemoveMediatorServer:
151         element && this.props.removeMediatorServer(element);
152         break;
153     }
154   };
155
156   private onCancel = () => {
157     this.props.onClose && this.props.onClose();
158   }
159
160   static getDerivedStateFromProps(props: EditMediatorServerDialogComponentProps, state: EditMediatorServerDialogComponentState & { _initialMediatorServer: MediatorServer }): EditMediatorServerDialogComponentState & { _initialMediatorServer: MediatorServer } {
161     if (props.mediatorServer !== state._initialMediatorServer) {
162       state = {
163         ...state,
164         ...props.mediatorServer,
165         _initialMediatorServer: props.mediatorServer,
166       };
167     }
168     return state;
169   }
170 }
171
172 export const EditMediatorServerDialog = connect(undefined, mapDispatch)(EditMediatorServerDialogComponent);
173 export default EditMediatorServerDialog;