Add data-provider
[ccsdk/features.git] / sdnr / wt / odlux / apps / maintenanceApp / src / components / editMaintenenceEntryDialog.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 {
31   addOrUpdateMaintenenceEntryAsyncActionCreator,
32   removeFromMaintenenceEntrysAsyncActionCreator
33 } from '../actions/maintenenceActions';
34
35 import { MaintenenceEntry } from '../models/maintenenceEntryType';
36 import { FormControl, InputLabel, Select, MenuItem } from '@material-ui/core';
37
38 export enum EditMaintenenceEntryDialogMode {
39   None = "none",
40   AddMaintenenceEntry = "addMaintenenceEntry",
41   EditMaintenenceEntry = "editMaintenenceEntry",
42   RemoveMaintenenceEntry = "removeMaintenenceEntry"
43 }
44
45 const mapDispatch = (dispatcher: IDispatcher) => ({
46   addOrUpdateMaintenenceEntry: (entry: MaintenenceEntry) => {
47     dispatcher.dispatch(addOrUpdateMaintenenceEntryAsyncActionCreator(entry));
48   },
49   removeMaintenenceEntry: (entry: MaintenenceEntry) => {
50     dispatcher.dispatch(removeFromMaintenenceEntrysAsyncActionCreator(entry));
51   },
52 });
53
54 type DialogSettings = {
55   dialogTitle: string,
56   dialogDescription: string,
57   applyButtonText: string,
58   cancelButtonText: string,
59   enableMountIdEditor: boolean,
60   enableTimeEditor: boolean,
61 }
62
63 const settings: { [key: string]: DialogSettings } = {
64   [EditMaintenenceEntryDialogMode.None]: {
65     dialogTitle: "",
66     dialogDescription: "",
67     applyButtonText: "",
68     cancelButtonText: "",
69     enableMountIdEditor: false,
70     enableTimeEditor: false,
71   },
72   [EditMaintenenceEntryDialogMode.AddMaintenenceEntry]: {
73     dialogTitle: "Add new maintenence entry",
74     dialogDescription: "",
75     applyButtonText: "Add",
76     cancelButtonText: "Cancel",
77     enableMountIdEditor: true,
78     enableTimeEditor: true,
79   },
80   [EditMaintenenceEntryDialogMode.EditMaintenenceEntry]: {
81     dialogTitle: "Edit maintenence entry",
82     dialogDescription: "",
83     applyButtonText: "Save",
84     cancelButtonText: "Cancel",
85     enableMountIdEditor: false,
86     enableTimeEditor: true,
87   },
88   [EditMaintenenceEntryDialogMode.RemoveMaintenenceEntry]: {
89     dialogTitle: "Remove maintenence entry",
90     dialogDescription: "",
91     applyButtonText: "Remove",
92     cancelButtonText: "Cancel",
93     enableMountIdEditor: false,
94     enableTimeEditor: false,
95   },
96 }
97
98 type EditMaintenenceEntryDIalogComponentProps = Connect<undefined, typeof mapDispatch> & {
99   mode: EditMaintenenceEntryDialogMode;
100   initialMaintenenceEntry: MaintenenceEntry;
101   onClose: () => void;
102 };
103
104 type EditMaintenenceEntryDIalogComponentState = MaintenenceEntry;
105
106 class EditMaintenenceEntryDIalogComponent extends React.Component<EditMaintenenceEntryDIalogComponentProps, EditMaintenenceEntryDIalogComponentState> {
107   constructor (props: EditMaintenenceEntryDIalogComponentProps) {
108     super(props);
109
110     this.state = {
111       ...this.props.initialMaintenenceEntry
112     };
113   }
114
115   render(): JSX.Element {
116     const setting = settings[this.props.mode];
117     return (
118       <Dialog open={this.props.mode !== EditMaintenenceEntryDialogMode.None}>
119         <DialogTitle id="form-dialog-title">{setting.dialogTitle}</DialogTitle>
120         <DialogContent>
121           <DialogContentText>
122             {setting.dialogDescription}
123           </DialogContentText>
124           <TextField disabled={!setting.enableMountIdEditor} spellCheck={false} autoFocus margin="dense" id="name" label="Name" type="text" fullWidth value={this.state.mountId} onChange={(event) => { this.setState({ mountId: event.target.value }); }} />
125           <TextField disabled={!setting.enableTimeEditor} spellCheck={false} autoFocus margin="dense" id="start" label="Start" type="datetime-local" fullWidth value={this.state.start} onChange={(event) => { this.setState({ start: event.target.value }); }} />
126           <TextField disabled={!setting.enableTimeEditor} spellCheck={false} autoFocus margin="dense" id="end" label="End" type="datetime-local" fullWidth value={this.state.end} onChange={(event) => { this.setState({ end: event.target.value }); }} />
127           <FormControl fullWidth disabled={!setting.enableTimeEditor}>
128             <InputLabel htmlFor="active">Active</InputLabel>
129             <Select value={ this.state.active || false } onChange={(event) => {
130               this.setState({ active: event.target.value as any as boolean });
131             }} inputProps={{ name: 'active', id: 'active' }} fullWidth >
132               <MenuItem value={true as any as string}>active</MenuItem>
133               <MenuItem value={false as any as string}>not active</MenuItem>
134             </Select>
135           </FormControl>
136         </DialogContent>
137         <DialogActions>
138           <Button onClick={(event) => {
139             this.onApply({
140               _id: this.state._id || this.state.mountId,
141               mountId: this.state.mountId,
142               description: this.state.description,
143               start: this.state.start,
144               end: this.state.end,
145               active: this.state.active
146             });
147             event.preventDefault();
148             event.stopPropagation();
149           }} > {setting.applyButtonText} </Button>
150           <Button onClick={(event) => {
151             this.onCancel();
152             event.preventDefault();
153             event.stopPropagation();
154           }} color="secondary"> {setting.cancelButtonText} </Button>
155         </DialogActions>
156       </Dialog>
157     )
158   }
159
160   private onApply = (entry: MaintenenceEntry) => {
161     this.props.onClose && this.props.onClose();
162     switch (this.props.mode) {
163       case EditMaintenenceEntryDialogMode.AddMaintenenceEntry:
164         entry && this.props.addOrUpdateMaintenenceEntry(entry);
165         break;
166       case EditMaintenenceEntryDialogMode.EditMaintenenceEntry:
167         entry && this.props.addOrUpdateMaintenenceEntry(entry);
168         break;
169       case EditMaintenenceEntryDialogMode.RemoveMaintenenceEntry:
170         entry && this.props.removeMaintenenceEntry(entry);
171         break;
172     }
173   };
174
175
176   private onCancel = () => {
177     this.props.onClose && this.props.onClose();
178   }
179
180   static getDerivedStateFromProps(props: EditMaintenenceEntryDIalogComponentProps, state: EditMaintenenceEntryDIalogComponentState & { _initialMaintenenceEntry: MaintenenceEntry }): EditMaintenenceEntryDIalogComponentState & { _initialMaintenenceEntry: MaintenenceEntry } {
181     if (props.initialMaintenenceEntry !== state._initialMaintenenceEntry) {
182       state = {
183         ...state,
184         ...props.initialMaintenenceEntry,
185         _initialMaintenenceEntry: props.initialMaintenenceEntry,
186       };
187     }
188     return state;
189   }
190
191 }
192
193 export const EditMaintenenceEntryDIalog = connect(undefined, mapDispatch)(EditMaintenenceEntryDIalogComponent);
194 export default EditMaintenenceEntryDIalog;