aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/apps/maintenanceApp/src/components/editMaintenenceEntryDialog.tsx
blob: 82928922473b877d104ce69c7428f97d1864182b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt odlux
 * =================================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 * =================================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * ============LICENSE_END==========================================================================
 */
import * as React from 'react';

import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogTitle from '@mui/material/DialogTitle';

import { IDispatcher, connect, Connect } from '../../../../framework/src/flux/connect';

import {
  addOrUpdateMaintenenceEntryAsyncActionCreator,
  removeFromMaintenenceEntrysAsyncActionCreator
} from '../actions/maintenenceActions';

import { MaintenenceEntry } from '../models/maintenenceEntryType';
import { FormControl, InputLabel, Select, MenuItem, Typography } from '@mui/material';

export enum EditMaintenenceEntryDialogMode {
  None = "none",
  AddMaintenenceEntry = "addMaintenenceEntry",
  EditMaintenenceEntry = "editMaintenenceEntry",
  RemoveMaintenenceEntry = "removeMaintenenceEntry"
}

const mapDispatch = (dispatcher: IDispatcher) => ({
  addOrUpdateMaintenenceEntry: (entry: MaintenenceEntry) => {
    dispatcher.dispatch(addOrUpdateMaintenenceEntryAsyncActionCreator(entry));
  },
  removeMaintenenceEntry: (entry: MaintenenceEntry) => {
    dispatcher.dispatch(removeFromMaintenenceEntrysAsyncActionCreator(entry));
  },
});

type DialogSettings = {
  dialogTitle: string,
  dialogDescription: string,
  applyButtonText: string,
  cancelButtonText: string,
  enableMountIdEditor: boolean,
  enableTimeEditor: boolean,
}

const settings: { [key: string]: DialogSettings } = {
  [EditMaintenenceEntryDialogMode.None]: {
    dialogTitle: "",
    dialogDescription: "",
    applyButtonText: "",
    cancelButtonText: "",
    enableMountIdEditor: false,
    enableTimeEditor: false,
  },
  [EditMaintenenceEntryDialogMode.AddMaintenenceEntry]: {
    dialogTitle: "Add new maintenence entry",
    dialogDescription: "",
    applyButtonText: "Add",
    cancelButtonText: "Cancel",
    enableMountIdEditor: true,
    enableTimeEditor: true,
  },
  [EditMaintenenceEntryDialogMode.EditMaintenenceEntry]: {
    dialogTitle: "Edit maintenence entry",
    dialogDescription: "",
    applyButtonText: "Save",
    cancelButtonText: "Cancel",
    enableMountIdEditor: false,
    enableTimeEditor: true,
  },
  [EditMaintenenceEntryDialogMode.RemoveMaintenenceEntry]: {
    dialogTitle: "Remove maintenence entry",
    dialogDescription: "",
    applyButtonText: "Remove",
    cancelButtonText: "Cancel",
    enableMountIdEditor: false,
    enableTimeEditor: false,
  },
}

type EditMaintenenceEntryDIalogComponentProps = Connect<undefined, typeof mapDispatch> & {
  mode: EditMaintenenceEntryDialogMode;
  initialMaintenenceEntry: MaintenenceEntry;
  onClose: () => void;
};

type EditMaintenenceEntryDIalogComponentState = MaintenenceEntry & { isErrorVisible: boolean };

class EditMaintenenceEntryDIalogComponent extends React.Component<EditMaintenenceEntryDIalogComponentProps, EditMaintenenceEntryDIalogComponentState> {
  constructor(props: EditMaintenenceEntryDIalogComponentProps) {
    super(props);

    this.state = {
      ...this.props.initialMaintenenceEntry,
      isErrorVisible: false
    };
  }

  render(): JSX.Element {
    const setting = settings[this.props.mode];
    return (
      <Dialog open={this.props.mode !== EditMaintenenceEntryDialogMode.None}>
        <DialogTitle id="form-dialog-title">{setting.dialogTitle}</DialogTitle>
        <DialogContent>
          <DialogContentText>
            {setting.dialogDescription}
          </DialogContentText>
          <TextField variant="standard"  disabled={!setting.enableMountIdEditor} spellCheck={false} autoFocus margin="dense" id="name" label="Name" type="text" fullWidth value={this.state.nodeId} onChange={(event) => { this.setState({ nodeId: event.target.value }); }} />
          {this.state.isErrorVisible && <Typography variant="body1" color="error" >Name must not be empty.</Typography>}
          <TextField variant="standard"  disabled={!setting.enableTimeEditor} spellCheck={false} autoFocus margin="dense" id="start" label="Start (Local DateTime)" type="datetime-local" fullWidth value={this.state.start} onChange={(event) => { this.setState({ start: event.target.value }); }} />
          <TextField variant="standard"  disabled={!setting.enableTimeEditor} spellCheck={false} autoFocus margin="dense" id="end" label="End (Local DateTime)" type="datetime-local" fullWidth value={this.state.end} onChange={(event) => { this.setState({ end: event.target.value }); }} />
          <FormControl variant="standard" fullWidth disabled={!setting.enableTimeEditor}>
            <InputLabel htmlFor="active">Active</InputLabel>
            <Select variant="standard" value={this.state.active || false} onChange={(event) => {
              this.setState({ active: event.target.value as any as boolean });
            }} inputProps={{ name: 'active', id: 'active' }} fullWidth >
              <MenuItem value={true as any as string}>active</MenuItem>
              <MenuItem value={false as any as string}>not active</MenuItem>
            </Select>
          </FormControl>
        </DialogContent>
        <DialogActions>
          <Button onClick={(event) => {

            if (this.props.mode === EditMaintenenceEntryDialogMode.AddMaintenenceEntry && this.state.nodeId.trim().length === 0) {
              this.setState({ isErrorVisible: true });
            } else {
              this.onApply({
                _id: this.state._id || this.state.nodeId,
                nodeId: this.state.nodeId,
                description: this.state.description,
                start: this.state.start,
                end: this.state.end,
                active: this.state.active
              });
              this.setState({ isErrorVisible: false });
            }

            event.preventDefault();
            event.stopPropagation();
          }} color="inherit" > {setting.applyButtonText} </Button>
          <Button onClick={(event) => {
            this.onCancel();
            event.preventDefault();
            event.stopPropagation();
            this.setState({ isErrorVisible: false });
          }} color="secondary"> {setting.cancelButtonText} </Button>
        </DialogActions>
      </Dialog>
    )
  }

  private onApply = (entry: MaintenenceEntry) => {
    this.props.onClose && this.props.onClose();
    switch (this.props.mode) {
      case EditMaintenenceEntryDialogMode.AddMaintenenceEntry:
        entry && this.props.addOrUpdateMaintenenceEntry(entry);
        break;
      case EditMaintenenceEntryDialogMode.EditMaintenenceEntry:
        entry && this.props.addOrUpdateMaintenenceEntry(entry);
        break;
      case EditMaintenenceEntryDialogMode.RemoveMaintenenceEntry:
        entry && this.props.removeMaintenenceEntry(entry);
        break;
    }
  };


  private onCancel = () => {
    this.props.onClose && this.props.onClose();
  }

  static getDerivedStateFromProps(props: EditMaintenenceEntryDIalogComponentProps, state: EditMaintenenceEntryDIalogComponentState & { _initialMaintenenceEntry: MaintenenceEntry }): EditMaintenenceEntryDIalogComponentState & { _initialMaintenenceEntry: MaintenenceEntry } {
    if (props.initialMaintenenceEntry !== state._initialMaintenenceEntry) {
      state = {
        ...state,
        ...props.initialMaintenenceEntry,
        _initialMaintenenceEntry: props.initialMaintenenceEntry,
      };
    }
    return state;
  }

}

export const EditMaintenenceEntryDIalog = connect(undefined, mapDispatch)(EditMaintenenceEntryDIalogComponent);
export default EditMaintenenceEntryDIalog;