From 9985a1e68712cc3089aead3d2c87504ff7f2d1f3 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Tue, 26 Mar 2024 16:40:42 +0100 Subject: [PATCH] [v11.0.x] Auth Drawer: Use redux store to load settings (#85172) Auth Drawer: Use redux store to load settings (#85110) * use actions instead of importing the backend service * Replace the test render for redux-rtl (cherry picked from commit 4e5bff0ada12b9155d96ed16f0097bea86624f8f) Co-authored-by: linoman <2051016+linoman@users.noreply.github.com> --- .../features/auth-config/AuthDrawer.test.tsx | 10 ++- .../app/features/auth-config/AuthDrawer.tsx | 80 ++++++++++++------- .../auth-config/AuthProvidersListPage.tsx | 2 +- .../app/features/auth-config/state/actions.ts | 10 ++- 4 files changed, 64 insertions(+), 38 deletions(-) diff --git a/public/app/features/auth-config/AuthDrawer.test.tsx b/public/app/features/auth-config/AuthDrawer.test.tsx index 2af62926c099836..35a0fc872870132 100644 --- a/public/app/features/auth-config/AuthDrawer.test.tsx +++ b/public/app/features/auth-config/AuthDrawer.test.tsx @@ -1,17 +1,21 @@ -import { render, screen } from '@testing-library/react'; +import { screen } from '@testing-library/react'; import React from 'react'; +import { render } from 'test/redux-rtl'; -import { AuthDrawer, Props } from './AuthDrawer'; +import { AuthDrawerUnconnected, Props } from './AuthDrawer'; const defaultProps: Props = { onClose: jest.fn(), + allowInsecureEmail: false, + loadSettings: jest.fn(), + saveSettings: jest.fn(), }; async function getTestContext(overrides: Partial = {}) { jest.clearAllMocks(); const props = { ...defaultProps, ...overrides }; - const { rerender } = render(); + const { rerender } = render(); return { rerender, props }; } diff --git a/public/app/features/auth-config/AuthDrawer.tsx b/public/app/features/auth-config/AuthDrawer.tsx index b2fbbf5f9f652d0..bd66222ee78df04 100644 --- a/public/app/features/auth-config/AuthDrawer.tsx +++ b/public/app/features/auth-config/AuthDrawer.tsx @@ -1,53 +1,71 @@ import { css } from '@emotion/css'; -import React, { useState } from 'react'; +import React, { JSX } from 'react'; +import { connect, ConnectedProps } from 'react-redux'; import { GrafanaTheme2 } from '@grafana/data'; -import { getBackendSrv } from '@grafana/runtime'; import { Button, Drawer, Text, TextLink, Switch, useStyles2 } from '@grafana/ui'; +import { useAppNotification } from 'app/core/copy/appNotification'; +import { StoreState } from 'app/types'; -export interface Props { +import { loadSettings, saveSettings } from './state/actions'; + +interface OwnProps { onClose: () => void; } -const SETTINGS_URL = '/api/admin/settings'; - -export const AuthDrawer = ({ onClose }: Props) => { - const [isOauthAllowInsecureEmailLookup, setOauthAllowInsecureEmailLookup] = useState(false); +export type Props = OwnProps & ConnectedProps; - const getSettings = async () => { - try { - const response = await getBackendSrv().get(SETTINGS_URL); - setOauthAllowInsecureEmailLookup(response.auth.oauth_allow_insecure_email_lookup?.toLowerCase?.() === 'true'); - } catch (error) {} +const mapStateToProps = (state: StoreState) => { + const allowInsecureEmail = + state.authConfig.settings?.auth?.oauth_allow_insecure_email_lookup.toLowerCase() === 'true'; + return { + allowInsecureEmail, }; - const updateSettings = async (property: boolean) => { +}; + +const mapActionsToProps = { + loadSettings, + saveSettings, +}; + +const connector = connect(mapStateToProps, mapActionsToProps); + +export const AuthDrawerUnconnected = ({ + allowInsecureEmail, + loadSettings, + onClose, + saveSettings, +}: Props): JSX.Element => { + const notifyApp = useAppNotification(); + + const oauthAllowInsecureEmailLookupOnChange = async () => { try { - const body = { + await saveSettings({ updates: { auth: { - oauth_allow_insecure_email_lookup: '' + property, + oauth_allow_insecure_email_lookup: '' + !allowInsecureEmail, }, }, - }; - await getBackendSrv().put(SETTINGS_URL, body); - } catch (error) {} + }); + await loadSettings(false); + notifyApp.success('Settings saved'); + } catch (error) { + notifyApp.error('Failed to save settings'); + } }; const resetButtonOnClick = async () => { try { - const body = { + await saveSettings({ removals: { auth: ['oauth_allow_insecure_email_lookup'], }, - }; - await getBackendSrv().put(SETTINGS_URL, body); - getSettings(); - } catch (error) {} - }; - - const oauthAllowInsecureEmailLookupOnChange = async () => { - updateSettings(!isOauthAllowInsecureEmailLookup); - setOauthAllowInsecureEmailLookup(!isOauthAllowInsecureEmailLookup); + }); + await loadSettings(false); + notifyApp.success('Settings saved'); + } catch (error) { + notifyApp.error('Failed to save settings'); + } }; const subtitle = ( @@ -65,8 +83,6 @@ export const AuthDrawer = ({ onClose }: Props) => { const styles = useStyles2(getStyles); - getSettings(); - return (
@@ -75,7 +91,7 @@ export const AuthDrawer = ({ onClose }: Props) => { Allow users to use the same email address to log into Grafana with different identity providers. - +