Skip to content

Commit 4e5bff0

Browse files
authored
Auth Drawer: Use redux store to load settings (#85110)
* use actions instead of importing the backend service * Replace the test render for redux-rtl
1 parent da1ef0e commit 4e5bff0

File tree

4 files changed

+64
-38
lines changed

4 files changed

+64
-38
lines changed

public/app/features/auth-config/AuthDrawer.test.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
import { render, screen } from '@testing-library/react';
1+
import { screen } from '@testing-library/react';
22
import React from 'react';
3+
import { render } from 'test/redux-rtl';
34

4-
import { AuthDrawer, Props } from './AuthDrawer';
5+
import { AuthDrawerUnconnected, Props } from './AuthDrawer';
56

67
const defaultProps: Props = {
78
onClose: jest.fn(),
9+
allowInsecureEmail: false,
10+
loadSettings: jest.fn(),
11+
saveSettings: jest.fn(),
812
};
913

1014
async function getTestContext(overrides: Partial<Props> = {}) {
1115
jest.clearAllMocks();
1216

1317
const props = { ...defaultProps, ...overrides };
14-
const { rerender } = render(<AuthDrawer {...props} />);
18+
const { rerender } = render(<AuthDrawerUnconnected {...props} />);
1519

1620
return { rerender, props };
1721
}

public/app/features/auth-config/AuthDrawer.tsx

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,71 @@
11
import { css } from '@emotion/css';
2-
import React, { useState } from 'react';
2+
import React, { JSX } from 'react';
3+
import { connect, ConnectedProps } from 'react-redux';
34

45
import { GrafanaTheme2 } from '@grafana/data';
5-
import { getBackendSrv } from '@grafana/runtime';
66
import { 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+
93111
const getStyles = (theme: GrafanaTheme2) => {
94112
return {
95113
advancedAuth: css({

public/app/features/auth-config/AuthProvidersListPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Page } from 'app/core/components/Page/Page';
88
import { config } from 'app/core/config';
99
import { StoreState } from 'app/types';
1010

11-
import { AuthDrawer } from './AuthDrawer';
11+
import AuthDrawer from './AuthDrawer';
1212
import ConfigureAuthCTA from './components/ConfigureAuthCTA';
1313
import { ProviderCard } from './components/ProviderCard';
1414
import { loadSettings } from './state/actions';

public/app/features/auth-config/state/actions.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ import {
1717
settingsUpdated,
1818
} from './reducers';
1919

20-
export function loadSettings(): ThunkResult<Promise<Settings>> {
20+
export function loadSettings(showSpinner = true): ThunkResult<Promise<Settings>> {
2121
return async (dispatch) => {
2222
if (contextSrv.hasPermission(AccessControlAction.SettingsRead)) {
23-
dispatch(loadingBegin());
23+
if (showSpinner) {
24+
dispatch(loadingBegin());
25+
}
2426
dispatch(loadProviders());
2527
const result = await getBackendSrv().get('/api/admin/settings');
2628
dispatch(settingsUpdated(result));
2729
await dispatch(loadProviderStatuses());
28-
dispatch(loadingEnd());
30+
if (showSpinner) {
31+
dispatch(loadingEnd());
32+
}
2933
return result;
3034
}
3135
};

0 commit comments

Comments
 (0)