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 '@mui/material/Button';
21 import TextField from '@mui/material/TextField';
22 import Dialog from '@mui/material/Dialog';
23 import DialogActions from '@mui/material/DialogActions';
24 import DialogContent from '@mui/material/DialogContent';
25 import DialogContentText from '@mui/material/DialogContentText';
26 import DialogTitle from '@mui/material/DialogTitle';
28 import { IDispatcher, connect, Connect } from '../../../../framework/src/flux/connect';
30 import { addAvaliableMediatorServerAsyncActionCreator, removeAvaliableMediatorServerAsyncActionCreator, updateAvaliableMediatorServerAsyncActionCreator } from '../actions/avaliableMediatorServersActions';
31 import { MediatorServer } from '../models/mediatorServer';
32 import { Typography } from '@mui/material';
34 export enum EditMediatorServerDialogMode {
36 AddMediatorServer = "addMediatorServer",
37 EditMediatorServer = "editMediatorServer",
38 RemoveMediatorServer = "removeMediatorServer",
41 const mapDispatch = (dispatcher: IDispatcher) => ({
42 addMediatorServer: (element: MediatorServer) => {
43 dispatcher.dispatch(addAvaliableMediatorServerAsyncActionCreator(element));
45 updateMediatorServer: (element: MediatorServer) => {
46 dispatcher.dispatch(updateAvaliableMediatorServerAsyncActionCreator(element));
48 removeMediatorServer: (element: MediatorServer) => {
49 dispatcher.dispatch(removeAvaliableMediatorServerAsyncActionCreator(element));
53 type DialogSettings = {
55 dialogDescription: string;
56 applyButtonText: string;
57 cancelButtonText: string;
61 const settings: { [key: string]: DialogSettings } = {
62 [EditMediatorServerDialogMode.None]: {
64 dialogDescription: "",
69 [EditMediatorServerDialogMode.AddMediatorServer]: {
70 dialogTitle: "Add Mediator Server",
71 dialogDescription: "",
72 applyButtonText: "Add",
73 cancelButtonText: "Cancel",
76 [EditMediatorServerDialogMode.EditMediatorServer]: {
77 dialogTitle: "Edit Mediator Server",
78 dialogDescription: "",
79 applyButtonText: "Update",
80 cancelButtonText: "Cancel",
83 [EditMediatorServerDialogMode.RemoveMediatorServer]: {
84 dialogTitle: "Remove Mediator Server",
85 dialogDescription: "",
86 applyButtonText: "Remove",
87 cancelButtonText: "Cancel",
92 type EditMediatorServerDialogComponentProps = Connect<undefined, typeof mapDispatch> & {
93 mode: EditMediatorServerDialogMode;
94 mediatorServer: MediatorServer;
98 const urlRegex = RegExp("^https?://");
100 type EditMediatorServerDialogComponentState = MediatorServer & { errorMessage: string[] };
102 class EditMediatorServerDialogComponent extends React.Component<EditMediatorServerDialogComponentProps, EditMediatorServerDialogComponentState> {
103 constructor(props: EditMediatorServerDialogComponentProps) {
107 ...this.props.mediatorServer,
112 areFieldsValid = () => {
113 return this.state.name.trim().length > 0 && this.state.url.trim().length > 0 && urlRegex.test(this.state.url);
116 createErrorMessages = () => {
119 if (this.state.name.trim().length === 0 && this.state.url.trim().length === 0) {
120 messages.push("The server name and the url must not be empty.")
123 if (this.state.url.trim().length === 0) {
124 messages.push("The server url must not be empty.")
126 } else if (this.state.name.trim().length === 0) {
127 messages.push("The server name must not be empty.")
130 if (!urlRegex.test(this.state.url)) {
131 if (messages.length > 0) {
132 return messages.concat(["The server url must start with 'http(s)://'."])
134 return ["The server url must start with 'http(s)://'."]
143 render(): JSX.Element {
144 const setting = settings[this.props.mode];
147 <Dialog open={this.props.mode !== EditMediatorServerDialogMode.None}>
148 <DialogTitle id="form-dialog-title">{setting.dialogTitle}</DialogTitle>
151 {setting.dialogDescription}
153 {/* <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}); } } /> */}
154 <TextField variant="standard" 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 }); }} />
155 <TextField variant="standard" 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 }); }} />
157 <Typography id="errorMessage" component={"div"} color="error">{this.state.errorMessage.map((error, index) => <div key={index}>{error}</div>)}</Typography>
161 <Button onClick={(event) => {
163 if (this.areFieldsValid()) {
164 this.setState({ errorMessage: [] });
167 name: this.state.name,
171 const errorMessage = this.createErrorMessages()
172 this.setState({ errorMessage: errorMessage })
175 event.preventDefault();
176 event.stopPropagation();
177 }} color="inherit" > {setting.applyButtonText} </Button>
178 <Button onClick={(event) => {
180 this.setState({ errorMessage: [] });
181 event.preventDefault();
182 event.stopPropagation();
183 }} color="secondary"> {setting.cancelButtonText} </Button>
189 private onApply = (element: MediatorServer) => {
190 this.props.onClose && this.props.onClose();
191 switch (this.props.mode) {
192 case EditMediatorServerDialogMode.AddMediatorServer:
193 element && this.props.addMediatorServer(element);
195 case EditMediatorServerDialogMode.EditMediatorServer:
196 element && this.props.updateMediatorServer(element);
198 case EditMediatorServerDialogMode.RemoveMediatorServer:
199 element && this.props.removeMediatorServer(element);
204 private onCancel = () => {
205 this.props.onClose && this.props.onClose();
208 static getDerivedStateFromProps(props: EditMediatorServerDialogComponentProps, state: EditMediatorServerDialogComponentState & { _initialMediatorServer: MediatorServer }): EditMediatorServerDialogComponentState & { _initialMediatorServer: MediatorServer } {
209 if (props.mediatorServer !== state._initialMediatorServer) {
212 ...props.mediatorServer,
213 _initialMediatorServer: props.mediatorServer,
220 export const EditMediatorServerDialog = connect(undefined, mapDispatch)(EditMediatorServerDialogComponent);
221 export default EditMediatorServerDialog;