Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / mediatorApp / src / views / mediatorServerSelection.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 React from 'react';
19 import { Theme, Tooltip } from '@mui/material';
20
21 import { WithStyles } from '@mui/styles';
22 import withStyles from '@mui/styles/withStyles';
23 import createStyles from '@mui/styles/createStyles';
24
25 import AddIcon from '@mui/icons-material/Add';
26 import IconButton from '@mui/material/IconButton';
27 import EditIcon from '@mui/icons-material/Edit';
28 import DeleteIcon from '@mui/icons-material/Delete';
29 import Refresh from '@mui/icons-material/Refresh';
30
31 import { IApplicationStoreState } from '../../../../framework/src/store/applicationStore';
32 import { connect, IDispatcher, Connect } from '../../../../framework/src/flux/connect';
33 import MaterialTable, { MaterialTableCtorType, ColumnType } from '../../../../framework/src/components/material-table';
34
35 import { createAvaliableMediatorServersProperties, createAvaliableMediatorServersActions } from '../handlers/avaliableMediatorServersHandler';
36
37 import { MediatorServer } from '../models/mediatorServer';
38 import EditMediatorServerDialog, { EditMediatorServerDialogMode } from '../components/editMediatorServerDialog';
39 import RefreshMediatorDialog, { RefreshMediatorDialogMode } from '../components/refreshMediatorDialog';
40 import { NavigateToApplication } from '../../../../framework/src/actions/navigationActions';
41
42 const MediatorServersTable = MaterialTable as MaterialTableCtorType<MediatorServer>;
43
44 const styles = (theme: Theme) => createStyles({
45   button: {
46     margin: 0,
47     padding: "6px 6px",
48     minWidth: 'unset',
49   },
50   spacer: {
51     marginLeft: theme.spacing(1),
52     marginRight: theme.spacing(1),
53     display: "inline",
54   },
55 });
56
57 const mapProps = (state: IApplicationStoreState) => ({
58   mediatorServersProperties: createAvaliableMediatorServersProperties(state),
59 });
60
61 const mapDispatch = (dispatcher: IDispatcher) => ({
62   mediatorServersActions: createAvaliableMediatorServersActions(dispatcher.dispatch),
63   selectMediatorServer: (mediatorServerId: string) => mediatorServerId && dispatcher.dispatch(new NavigateToApplication("mediator", mediatorServerId)),
64 });
65
66 const emptyMediatorServer: MediatorServer = {
67   id: "",
68   name: "",
69   url: ""
70 };
71
72 type MediatorServerSelectionComponentProps = Connect<typeof mapProps, typeof mapDispatch> & WithStyles<typeof styles>;
73
74 type MediatorServerSelectionComponentState = {
75   mediatorServerToEdit: MediatorServer,
76   mediatorServerEditorMode: EditMediatorServerDialogMode,
77   refreshMediatorEditorMode: RefreshMediatorDialogMode
78 }
79
80 let initialSorted = false;
81
82 class MediatorServerSelectionComponent extends React.Component<MediatorServerSelectionComponentProps, MediatorServerSelectionComponentState> {
83
84   constructor(props: MediatorServerSelectionComponentProps) {
85     super(props);
86
87     this.state = {
88       mediatorServerEditorMode: EditMediatorServerDialogMode.None,
89       mediatorServerToEdit: emptyMediatorServer,
90       refreshMediatorEditorMode: RefreshMediatorDialogMode.None
91     }
92   }
93
94   render() {
95     const { classes } = this.props;
96     const refreshMediatorAction = {
97       icon: Refresh, tooltip: 'Refresh Mediator Server Table', ariaLabel:'refresh', onClick: () => {
98         this.setState({
99           refreshMediatorEditorMode: RefreshMediatorDialogMode.RefreshMediatorTable
100         });
101       }
102     };
103
104     const addMediatorServerActionButton = {
105       icon: AddIcon, tooltip: 'Add', ariaLabel:'add-element', onClick: () => {
106         this.setState({
107           mediatorServerEditorMode: EditMediatorServerDialogMode.AddMediatorServer,
108           mediatorServerToEdit: emptyMediatorServer,
109         });
110       }
111     };
112     return <>
113       <MediatorServersTable stickyHeader title={"Mediator"} tableId={null} customActionButtons={[refreshMediatorAction, addMediatorServerActionButton]} idProperty={"id"}
114         {...this.props.mediatorServersActions} {...this.props.mediatorServersProperties} columns={[
115           { property: "name", title: "Name", type: ColumnType.text },
116           { property: "url", title: "Url", type: ColumnType.text },
117           {
118             property: "actions", title: "Actions", type: ColumnType.custom, customControl: ({ rowData }) => (
119               <div className={classes.spacer}>
120                 <Tooltip disableInteractive title={"Edit"} ><IconButton
121                   className={classes.button}
122                   onClick={event => { this.onEditMediatorServer(event, rowData); }}
123                   size="large"><EditIcon /></IconButton></Tooltip>
124                 <Tooltip disableInteractive title={"Remove"} ><IconButton
125                   className={classes.button}
126                   onClick={event => { this.onRemoveMediatorServer(event, rowData); }}
127                   size="large"><DeleteIcon /></IconButton></Tooltip>
128               </div>
129             )
130           }
131         ]} onHandleClick={this.onSelectMediatorServer} />
132       <EditMediatorServerDialog
133         mediatorServer={this.state.mediatorServerToEdit}
134         mode={this.state.mediatorServerEditorMode}
135         onClose={this.onCloseEditMediatorServerDialog} />
136       <RefreshMediatorDialog
137         mode={this.state.refreshMediatorEditorMode}
138         onClose={this.onCloseRefreshMediatorDialog}
139       />
140     </>;
141   }
142
143   public componentDidMount() {
144
145     if (!initialSorted) {
146       initialSorted = true;
147       this.props.mediatorServersActions.onHandleRequestSort("name");
148     } else {
149       this.props.mediatorServersActions.onRefresh();
150     }
151   }
152
153   private onSelectMediatorServer = (event: React.MouseEvent<HTMLElement>, server: MediatorServer) => {
154     event.preventDefault();
155     event.stopPropagation();
156     this.props.selectMediatorServer(server && server.id);
157
158   }
159
160   private onEditMediatorServer = (event: React.MouseEvent<HTMLElement>, server: MediatorServer) => {
161     event.preventDefault();
162     event.stopPropagation();
163     this.setState({
164       mediatorServerEditorMode: EditMediatorServerDialogMode.EditMediatorServer,
165       mediatorServerToEdit: server,
166     });
167   }
168
169   private onRemoveMediatorServer = (event: React.MouseEvent<HTMLElement>, server: MediatorServer) => {
170     event.preventDefault();
171     event.stopPropagation();
172     this.setState({
173       mediatorServerEditorMode: EditMediatorServerDialogMode.RemoveMediatorServer,
174       mediatorServerToEdit: server,
175     });
176   }
177
178   private onCloseEditMediatorServerDialog = () => {
179     this.setState({
180       mediatorServerEditorMode: EditMediatorServerDialogMode.None,
181       mediatorServerToEdit: emptyMediatorServer,
182     });
183   }
184   private onCloseRefreshMediatorDialog = () => {
185     this.setState({
186       refreshMediatorEditorMode: RefreshMediatorDialogMode.None
187     });
188   }
189 }
190
191
192 export const MediatorServerSelection = withStyles(styles)(connect(mapProps, mapDispatch)(MediatorServerSelectionComponent));
193 export default MediatorServerSelection;