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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
18 import * as React from 'react';
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';
28 import { IDispatcher, connect, Connect } from '../../../../framework/src/flux/connect';
31 addOrUpdateMaintenenceEntryAsyncActionCreator,
32 removeFromMaintenenceEntrysAsyncActionCreator
33 } from '../actions/maintenenceActions';
35 import { MaintenenceEntry } from '../models/maintenenceEntryType';
36 import { FormControl, InputLabel, Select, MenuItem } from '@material-ui/core';
38 export enum EditMaintenenceEntryDialogMode {
40 AddMaintenenceEntry = "addMaintenenceEntry",
41 EditMaintenenceEntry = "editMaintenenceEntry",
42 RemoveMaintenenceEntry = "removeMaintenenceEntry"
45 const mapDispatch = (dispatcher: IDispatcher) => ({
46 addOrUpdateMaintenenceEntry: (entry: MaintenenceEntry) => {
47 dispatcher.dispatch(addOrUpdateMaintenenceEntryAsyncActionCreator(entry));
49 removeMaintenenceEntry: (entry: MaintenenceEntry) => {
50 dispatcher.dispatch(removeFromMaintenenceEntrysAsyncActionCreator(entry));
54 type DialogSettings = {
56 dialogDescription: string,
57 applyButtonText: string,
58 cancelButtonText: string,
59 enableMountIdEditor: boolean,
60 enableTimeEditor: boolean,
63 const settings: { [key: string]: DialogSettings } = {
64 [EditMaintenenceEntryDialogMode.None]: {
66 dialogDescription: "",
69 enableMountIdEditor: false,
70 enableTimeEditor: false,
72 [EditMaintenenceEntryDialogMode.AddMaintenenceEntry]: {
73 dialogTitle: "Add new maintenence entry",
74 dialogDescription: "",
75 applyButtonText: "Add",
76 cancelButtonText: "Cancel",
77 enableMountIdEditor: true,
78 enableTimeEditor: true,
80 [EditMaintenenceEntryDialogMode.EditMaintenenceEntry]: {
81 dialogTitle: "Edit maintenence entry",
82 dialogDescription: "",
83 applyButtonText: "Save",
84 cancelButtonText: "Cancel",
85 enableMountIdEditor: false,
86 enableTimeEditor: true,
88 [EditMaintenenceEntryDialogMode.RemoveMaintenenceEntry]: {
89 dialogTitle: "Remove maintenence entry",
90 dialogDescription: "",
91 applyButtonText: "Remove",
92 cancelButtonText: "Cancel",
93 enableMountIdEditor: false,
94 enableTimeEditor: false,
98 type EditMaintenenceEntryDIalogComponentProps = Connect<undefined, typeof mapDispatch> & {
99 mode: EditMaintenenceEntryDialogMode;
100 initialMaintenenceEntry: MaintenenceEntry;
104 type EditMaintenenceEntryDIalogComponentState = MaintenenceEntry;
106 class EditMaintenenceEntryDIalogComponent extends React.Component<EditMaintenenceEntryDIalogComponentProps, EditMaintenenceEntryDIalogComponentState> {
107 constructor (props: EditMaintenenceEntryDIalogComponentProps) {
111 ...this.props.initialMaintenenceEntry
115 render(): JSX.Element {
116 const setting = settings[this.props.mode];
118 <Dialog open={this.props.mode !== EditMaintenenceEntryDialogMode.None}>
119 <DialogTitle id="form-dialog-title">{setting.dialogTitle}</DialogTitle>
122 {setting.dialogDescription}
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>
138 <Button onClick={(event) => {
140 _id: this.state._id || this.state.mountId,
141 mountId: this.state.mountId,
142 description: this.state.description,
143 start: this.state.start,
145 active: this.state.active
147 event.preventDefault();
148 event.stopPropagation();
149 }} > {setting.applyButtonText} </Button>
150 <Button onClick={(event) => {
152 event.preventDefault();
153 event.stopPropagation();
154 }} color="secondary"> {setting.cancelButtonText} </Button>
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);
166 case EditMaintenenceEntryDialogMode.EditMaintenenceEntry:
167 entry && this.props.addOrUpdateMaintenenceEntry(entry);
169 case EditMaintenenceEntryDialogMode.RemoveMaintenenceEntry:
170 entry && this.props.removeMaintenenceEntry(entry);
176 private onCancel = () => {
177 this.props.onClose && this.props.onClose();
180 static getDerivedStateFromProps(props: EditMaintenenceEntryDIalogComponentProps, state: EditMaintenenceEntryDIalogComponentState & { _initialMaintenenceEntry: MaintenenceEntry }): EditMaintenenceEntryDIalogComponentState & { _initialMaintenenceEntry: MaintenenceEntry } {
181 if (props.initialMaintenenceEntry !== state._initialMaintenenceEntry) {
184 ...props.initialMaintenenceEntry,
185 _initialMaintenenceEntry: props.initialMaintenenceEntry,
193 export const EditMaintenenceEntryDIalog = connect(undefined, mapDispatch)(EditMaintenenceEntryDIalogComponent);
194 export default EditMaintenenceEntryDIalog;