Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / framework / src / components / settings / general.tsx
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2021 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
19 import  React from 'react';
20 import { Button, FormControlLabel, Switch, Typography } from '@mui/material';
21 import makeStyles from '@mui/styles/makeStyles';
22 import { SettingsComponentProps } from '../../models/settings';
23 import { connect, Connect, IDispatcher } from '../../flux/connect';
24 import { IApplicationStoreState } from '../../store/applicationStore';
25 import { getGeneralSettingsAction, SetGeneralSettingsAction, updateGeneralSettingsAction } from '../../actions/settingsAction';
26 import { sendMessage, SettingsMessage } from '../../services/broadcastService';
27
28
29 type props = Connect<typeof mapProps, typeof mapDispatch> & SettingsComponentProps;
30
31 const mapProps = (state: IApplicationStoreState) => ({
32     settings: state.framework.applicationState.settings,
33     user: state.framework.authenticationState.user?.user
34     
35 });
36
37 const mapDispatch = (dispatcher: IDispatcher) => ({
38
39     updateSettings :(activateNotifications: boolean) => dispatcher.dispatch(updateGeneralSettingsAction(activateNotifications)),
40     getSettings: () =>dispatcher.dispatch(getGeneralSettingsAction()),
41   });
42
43 const styles = makeStyles({
44     sectionMargin: {
45       marginTop: "30px",
46       marginBottom: "15px"
47     },
48     elementMargin: {
49       marginLeft: "10px"
50     },
51     buttonPosition:{
52       position: "absolute",
53       right: "32%"
54     }
55   });
56
57 const General : React.FunctionComponent<props> = (props) =>{
58
59 const classes = styles();
60
61 const [areWebsocketsEnabled, setWebsocketsEnabled] = React.useState(props.settings.general.areNotificationsEnabled || false);
62
63 React.useEffect(()=>{
64   props.getSettings();
65 },[]);
66
67 React.useEffect(()=>{
68   if(props.settings.general.areNotificationsEnabled!==null)
69     setWebsocketsEnabled(props.settings.general.areNotificationsEnabled)
70 },[props.settings]);
71
72 const onWebsocketsChange = (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, newValue: boolean) =>{
73     setWebsocketsEnabled(newValue);
74   }
75
76 const onSave = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) =>{
77
78     e.preventDefault();
79     const message: SettingsMessage = {key: 'general', enableNotifications: areWebsocketsEnabled, user: props.user!};
80     sendMessage(message, "odlux_settings");
81     props.updateSettings(areWebsocketsEnabled);
82     props.onClose();
83 }
84
85 const onCancel = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) =>{
86   e.preventDefault();
87   props.onClose();
88
89 }
90
91
92     return <div>
93         <Typography className={classes.sectionMargin} variant="body1" style={{ fontWeight: "bold" }} gutterBottom>
94           Enable Notifications
95         </Typography>
96         <FormControlLabel style={{ padding:5}}
97         value="end"
98         control={<Switch color="secondary" aria-label="enable-notifications-button" aria-checked={areWebsocketsEnabled} checked={areWebsocketsEnabled} onChange={onWebsocketsChange} />}
99         label="Enable Notifications"
100         labelPlacement="end"
101       />
102       <div className={classes.buttonPosition}>
103        <Button aria-label="cancel-button" className={classes.elementMargin} variant="contained" color="primary" onClick={onCancel}>Cancel</Button>
104        <Button aria-label="save-button" className={classes.elementMargin} variant="contained" color="secondary" onClick={onSave}>Save</Button>
105     </div>
106     </div>
107 }
108
109 export const GeneralUserSettings = connect(mapProps, mapDispatch)(General);
110 export default GeneralUserSettings;