11import { css } from '@emotion/css' ;
2- import React , { useState } from 'react' ;
2+ import React , { JSX } from 'react' ;
3+ import { connect , ConnectedProps } from 'react-redux' ;
34
45import { GrafanaTheme2 } from '@grafana/data' ;
5- import { getBackendSrv } from '@grafana/runtime' ;
66import { Button , Drawer , Text , TextLink , Switch , useStyles2 } from '@grafana/ui' ;
7+ import { useAppNotification } from 'app/core/copy/appNotification' ;
8+ import { StoreState } from 'app/types' ;
79
8- export interface Props {
10+ import { loadSettings , saveSettings } from './state/actions' ;
11+
12+ interface OwnProps {
913 onClose : ( ) => void ;
1014}
1115
12- const SETTINGS_URL = '/api/admin/settings' ;
13-
14- export const AuthDrawer = ( { onClose } : Props ) => {
15- const [ isOauthAllowInsecureEmailLookup , setOauthAllowInsecureEmailLookup ] = useState ( false ) ;
16+ export type Props = OwnProps & ConnectedProps < typeof connector > ;
1617
17- const getSettings = async ( ) => {
18- try {
19- const response = await getBackendSrv ( ) . get ( SETTINGS_URL ) ;
20- setOauthAllowInsecureEmailLookup ( response . auth . oauth_allow_insecure_email_lookup ?. toLowerCase ?. ( ) === 'true' ) ;
21- } catch ( error ) { }
18+ const mapStateToProps = ( state : StoreState ) => {
19+ const allowInsecureEmail =
20+ state . authConfig . settings ?. auth ?. oauth_allow_insecure_email_lookup . toLowerCase ( ) === 'true' ;
21+ return {
22+ allowInsecureEmail ,
2223 } ;
23- const updateSettings = async ( property : boolean ) => {
24+ } ;
25+
26+ const mapActionsToProps = {
27+ loadSettings,
28+ saveSettings,
29+ } ;
30+
31+ const connector = connect ( mapStateToProps , mapActionsToProps ) ;
32+
33+ export const AuthDrawerUnconnected = ( {
34+ allowInsecureEmail,
35+ loadSettings,
36+ onClose,
37+ saveSettings,
38+ } : Props ) : JSX . Element => {
39+ const notifyApp = useAppNotification ( ) ;
40+
41+ const oauthAllowInsecureEmailLookupOnChange = async ( ) => {
2442 try {
25- const body = {
43+ await saveSettings ( {
2644 updates : {
2745 auth : {
28- oauth_allow_insecure_email_lookup : '' + property ,
46+ oauth_allow_insecure_email_lookup : '' + ! allowInsecureEmail ,
2947 } ,
3048 } ,
31- } ;
32- await getBackendSrv ( ) . put ( SETTINGS_URL , body ) ;
33- } catch ( error ) { }
49+ } ) ;
50+ await loadSettings ( false ) ;
51+ notifyApp . success ( 'Settings saved' ) ;
52+ } catch ( error ) {
53+ notifyApp . error ( 'Failed to save settings' ) ;
54+ }
3455 } ;
3556
3657 const resetButtonOnClick = async ( ) => {
3758 try {
38- const body = {
59+ await saveSettings ( {
3960 removals : {
4061 auth : [ 'oauth_allow_insecure_email_lookup' ] ,
4162 } ,
42- } ;
43- await getBackendSrv ( ) . put ( SETTINGS_URL , body ) ;
44- getSettings ( ) ;
45- } catch ( error ) { }
46- } ;
47-
48- const oauthAllowInsecureEmailLookupOnChange = async ( ) => {
49- updateSettings ( ! isOauthAllowInsecureEmailLookup ) ;
50- setOauthAllowInsecureEmailLookup ( ! isOauthAllowInsecureEmailLookup ) ;
63+ } ) ;
64+ await loadSettings ( false ) ;
65+ notifyApp . success ( 'Settings saved' ) ;
66+ } catch ( error ) {
67+ notifyApp . error ( 'Failed to save settings' ) ;
68+ }
5169 } ;
5270
5371 const subtitle = (
@@ -65,8 +83,6 @@ export const AuthDrawer = ({ onClose }: Props) => {
6583
6684 const styles = useStyles2 ( getStyles ) ;
6785
68- getSettings ( ) ;
69-
7086 return (
7187 < Drawer title = "Auth Settings" subtitle = { subtitle } size = "md" onClose = { onClose } >
7288 < div className = { styles . advancedAuth } >
@@ -75,7 +91,7 @@ export const AuthDrawer = ({ onClose }: Props) => {
7591 < Text variant = "body" color = "secondary" >
7692 Allow users to use the same email address to log into Grafana with different identity providers.
7793 </ Text >
78- < Switch value = { isOauthAllowInsecureEmailLookup } onChange = { oauthAllowInsecureEmailLookupOnChange } />
94+ < Switch value = { allowInsecureEmail } onChange = { oauthAllowInsecureEmailLookupOnChange } />
7995 </ div >
8096 < Button
8197 size = "md"
@@ -90,6 +106,8 @@ export const AuthDrawer = ({ onClose }: Props) => {
90106 ) ;
91107} ;
92108
109+ export default connector ( AuthDrawerUnconnected ) ;
110+
93111const getStyles = ( theme : GrafanaTheme2 ) => {
94112 return {
95113 advancedAuth : css ( {
0 commit comments