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