diff options
author | Viktor Vorona <viktor.vorona@sonarsource.com> | 2023-08-10 17:12:59 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-08-11 20:02:49 +0000 |
commit | 344e16be5060c96fceb39ee737dd7604ed6d717d (patch) | |
tree | 2899a2f35629019bab1dededb20e452fc12b4f38 /server/sonar-web/src/main/js/apps/settings | |
parent | fc14589ce7a24ca782b09ac3d34fb5f9bfe4bff1 (diff) | |
download | sonarqube-344e16be5060c96fceb39ee737dd7604ed6d717d.tar.gz sonarqube-344e16be5060c96fceb39ee737dd7604ed6d717d.zip |
SONAR-20122 Move react-queries to common folder
Diffstat (limited to 'server/sonar-web/src/main/js/apps/settings')
10 files changed, 22 insertions, 207 deletions
diff --git a/server/sonar-web/src/main/js/apps/settings/components/almIntegration/AlmIntegrationRenderer.tsx b/server/sonar-web/src/main/js/apps/settings/components/almIntegration/AlmIntegrationRenderer.tsx index 59965b85516..a1430ead8af 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/almIntegration/AlmIntegrationRenderer.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/almIntegration/AlmIntegrationRenderer.tsx @@ -24,6 +24,7 @@ import BoxedTabs from '../../../../components/controls/BoxedTabs'; import { Alert } from '../../../../components/ui/Alert'; import { translate } from '../../../../helpers/l10n'; import { getBaseUrl } from '../../../../helpers/system'; +import { useGetValuesQuery } from '../../../../queries/settings'; import { AlmKeys, AlmSettingsBindingDefinitions, @@ -31,7 +32,6 @@ import { } from '../../../../types/alm-settings'; import { SettingsKey } from '../../../../types/settings'; import { Dict } from '../../../../types/types'; -import { useGetValuesQuery } from '../../queries/settings'; import { AlmTabs } from './AlmIntegration'; import AlmTab from './AlmTab'; import DeleteModal from './DeleteModal'; diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/ConfigurationForm.tsx b/server/sonar-web/src/main/js/apps/settings/components/authentication/ConfigurationForm.tsx index ae6d06d1432..4c1a3081db6 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/ConfigurationForm.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/ConfigurationForm.tsx @@ -26,8 +26,8 @@ import { ResetButtonLink, SubmitButton } from '../../../../components/controls/b import { Alert } from '../../../../components/ui/Alert'; import DeferredSpinner from '../../../../components/ui/DeferredSpinner'; import { translate } from '../../../../helpers/l10n'; +import { useSaveValuesMutation } from '../../../../queries/settings'; import { Dict } from '../../../../types/types'; -import { useSaveValuesMutation } from '../../queries/settings'; import { AuthenticationTabs, DOCUMENTATION_LINK_SUFFIXES } from './Authentication'; import AuthenticationFormField from './AuthenticationFormField'; import { SettingValue } from './hook/useConfiguration'; diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/GitHubConfigurationValidity.tsx b/server/sonar-web/src/main/js/apps/settings/components/authentication/GitHubConfigurationValidity.tsx index 867d5c7e968..06331668136 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/GitHubConfigurationValidity.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/GitHubConfigurationValidity.tsx @@ -27,8 +27,8 @@ import ClearIcon from '../../../../components/icons/ClearIcon'; import HelpIcon from '../../../../components/icons/HelpIcon'; import { Alert, AlertVariant } from '../../../../components/ui/Alert'; import { translate, translateWithParameters } from '../../../../helpers/l10n'; +import { useCheckGitHubConfigQuery } from '../../../../queries/identity-provider'; import { GitHubProvisioningStatus } from '../../../../types/provisioning'; -import { useCheckGitHubConfigQuery } from './queries/identity-provider'; const intlPrefix = 'settings.authentication.github.configuration.validation'; diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx b/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx index 36bce8a1cb2..12f82b07647 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx @@ -29,7 +29,11 @@ import DeleteIcon from '../../../../components/icons/DeleteIcon'; import EditIcon from '../../../../components/icons/EditIcon'; import { Alert } from '../../../../components/ui/Alert'; import { translate, translateWithParameters } from '../../../../helpers/l10n'; -import { useSyncNow } from '../../../../queries/github-sync'; +import { + useCheckGitHubConfigQuery, + useIdentityProviderQuery, + useSyncWithGitHubNow, +} from '../../../../queries/identity-provider'; import { AlmKeys } from '../../../../types/alm-settings'; import { ExtendedSettingDefinition } from '../../../../types/settings'; import { DOCUMENTATION_LINK_SUFFIXES } from './Authentication'; @@ -37,7 +41,6 @@ import AuthenticationFormField from './AuthenticationFormField'; import ConfigurationForm from './ConfigurationForm'; import GitHubConfigurationValidity from './GitHubConfigurationValidity'; import useGithubConfiguration, { GITHUB_JIT_FIELDS } from './hook/useGithubConfiguration'; -import { useCheckGitHubConfigQuery, useIdentityProviderQuery } from './queries/identity-provider'; interface GithubAuthenticationProps { definitions: ExtendedSettingDefinition[]; @@ -79,7 +82,7 @@ export default function GithubAuthenticationTab(props: GithubAuthenticationProps } = useGithubConfiguration(definitions); const hasDifferentProvider = data?.provider !== undefined && data.provider !== Provider.Github; - const { canSyncNow, synchronizeNow } = useSyncNow(); + const { canSyncNow, synchronizeNow } = useSyncWithGitHubNow(); const { refetch } = useCheckGitHubConfigQuery(enabled); const handleCreateConfiguration = () => { diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/SamlAuthenticationTab.tsx b/server/sonar-web/src/main/js/apps/settings/components/authentication/SamlAuthenticationTab.tsx index 48448523693..edb9349490d 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/SamlAuthenticationTab.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/SamlAuthenticationTab.tsx @@ -30,8 +30,12 @@ import DeleteIcon from '../../../../components/icons/DeleteIcon'; import EditIcon from '../../../../components/icons/EditIcon'; import { Alert } from '../../../../components/ui/Alert'; import { translate } from '../../../../helpers/l10n'; +import { + useIdentityProviderQuery, + useToggleScimMutation, +} from '../../../../queries/identity-provider'; +import { useSaveValueMutation } from '../../../../queries/settings'; import { ExtendedSettingDefinition } from '../../../../types/settings'; -import { useSaveValueMutation } from '../../queries/settings'; import { getPropertyName } from '../../utils'; import DefinitionDescription from '../DefinitionDescription'; import ConfigurationForm from './ConfigurationForm'; @@ -40,7 +44,6 @@ import useSamlConfiguration, { SAML_GROUP_NAME, SAML_SCIM_DEPRECATED, } from './hook/useSamlConfiguration'; -import { useIdentityProviderQuery, useToggleScimMutation } from './queries/identity-provider'; interface SamlAuthenticationProps { definitions: ExtendedSettingDefinition[]; diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useConfiguration.ts b/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useConfiguration.ts index c3bf05a3b61..b88e1007257 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useConfiguration.ts +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useConfiguration.ts @@ -20,9 +20,9 @@ import { UseMutationResult } from '@tanstack/react-query'; import { every, isEmpty, keyBy, update } from 'lodash'; import { useCallback, useEffect, useState } from 'react'; +import { useGetValuesQuery, useResetSettingsMutation } from '../../../../../queries/settings'; import { ExtendedSettingDefinition } from '../../../../../types/settings'; import { Dict } from '../../../../../types/types'; -import { useGetValuesQuery, useResetSettingsMutation } from '../../../queries/settings'; export type SettingValue = | { diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useGithubConfiguration.ts b/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useGithubConfiguration.ts index b335b41f9ed..e8ca3a7fb62 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useGithubConfiguration.ts +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useGithubConfiguration.ts @@ -20,13 +20,13 @@ import { some } from 'lodash'; import { useContext, useState } from 'react'; import { AvailableFeaturesContext } from '../../../../../app/components/available-features/AvailableFeaturesContext'; -import { Feature } from '../../../../../types/features'; -import { ExtendedSettingDefinition } from '../../../../../types/settings'; -import { useSaveValueMutation, useSaveValuesMutation } from '../../../queries/settings'; import { - useGithubStatusQuery, + useGithubProvisioningEnabledQuery, useToggleGithubProvisioningMutation, -} from '../queries/identity-provider'; +} from '../../../../../queries/identity-provider'; +import { useSaveValueMutation, useSaveValuesMutation } from '../../../../../queries/settings'; +import { Feature } from '../../../../../types/features'; +import { ExtendedSettingDefinition } from '../../../../../types/settings'; import useConfiguration from './useConfiguration'; export const GITHUB_ENABLED_FIELD = 'sonar.auth.github.enabled'; @@ -59,7 +59,7 @@ export default function useGithubConfiguration(definitions: ExtendedSettingDefin const hasGithubProvisioning = useContext(AvailableFeaturesContext).includes( Feature.GithubProvisioning ); - const { data: githubProvisioningStatus } = useGithubStatusQuery(); + const { data: githubProvisioningStatus } = useGithubProvisioningEnabledQuery(); const toggleGithubProvisioning = useToggleGithubProvisioningMutation(); const [newGithubProvisioningStatus, setNewGithubProvisioningStatus] = useState<boolean>(); const hasGithubProvisioningTypeChange = diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useSamlConfiguration.ts b/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useSamlConfiguration.ts index 47b2a9c5207..cf720362be1 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useSamlConfiguration.ts +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useSamlConfiguration.ts @@ -19,9 +19,9 @@ */ import React from 'react'; import { AvailableFeaturesContext } from '../../../../../app/components/available-features/AvailableFeaturesContext'; +import { useScimStatusQuery } from '../../../../../queries/identity-provider'; import { Feature } from '../../../../../types/features'; import { ExtendedSettingDefinition } from '../../../../../types/settings'; -import { useScimStatusQuery } from '../queries/identity-provider'; import useConfiguration from './useConfiguration'; export const SAML_ENABLED_FIELD = 'sonar.auth.saml.enabled'; diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/queries/identity-provider.ts b/server/sonar-web/src/main/js/apps/settings/components/authentication/queries/identity-provider.ts deleted file mode 100644 index 9c02a99a80c..00000000000 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/queries/identity-provider.ts +++ /dev/null @@ -1,88 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; -import { useContext } from 'react'; -import { - activateGithubProvisioning, - activateScim, - checkConfigurationValidity, - deactivateGithubProvisioning, - deactivateScim, - fetchIsScimEnabled, -} from '../../../../../api/provisioning'; -import { getSystemInfo } from '../../../../../api/system'; -import { AvailableFeaturesContext } from '../../../../../app/components/available-features/AvailableFeaturesContext'; -import { mapReactQueryResult } from '../../../../../helpers/react-query'; -import { useSyncStatusQuery } from '../../../../../queries/github-sync'; -import { Feature } from '../../../../../types/features'; -import { SysInfoCluster } from '../../../../../types/types'; - -export function useIdentityProviderQuery() { - return useQuery(['identity_provider'], async () => { - const info = (await getSystemInfo()) as SysInfoCluster; - return { provider: info.System['External Users and Groups Provisioning'] }; - }); -} - -export function useScimStatusQuery() { - const hasScim = useContext(AvailableFeaturesContext).includes(Feature.Scim); - - return useQuery(['identity_provider', 'scim_status'], () => { - if (!hasScim) { - return false; - } - return fetchIsScimEnabled(); - }); -} - -export function useGithubStatusQuery() { - const res = useSyncStatusQuery({ noRefetch: true }); - - return mapReactQueryResult(res, (data) => data.enabled); -} - -export function useToggleScimMutation() { - const client = useQueryClient(); - return useMutation({ - mutationFn: (activate: boolean) => (activate ? activateScim() : deactivateScim()), - onSuccess: () => { - client.invalidateQueries({ queryKey: ['identity_provider'] }); - }, - }); -} - -export function useToggleGithubProvisioningMutation() { - const client = useQueryClient(); - return useMutation({ - mutationFn: (activate: boolean) => - activate ? activateGithubProvisioning() : deactivateGithubProvisioning(), - onSuccess: () => { - client.invalidateQueries({ queryKey: ['identity_provider'] }); - client.invalidateQueries({ queryKey: ['github_sync'] }); - }, - }); -} - -export const useCheckGitHubConfigQuery = (githubEnabled: boolean) => { - return useQuery(['identity_provider', 'github_check'], checkConfigurationValidity, { - enabled: githubEnabled, - }); -}; diff --git a/server/sonar-web/src/main/js/apps/settings/queries/settings.ts b/server/sonar-web/src/main/js/apps/settings/queries/settings.ts deleted file mode 100644 index 6c88431f664..00000000000 --- a/server/sonar-web/src/main/js/apps/settings/queries/settings.ts +++ /dev/null @@ -1,103 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; -import { getValues, resetSettingValue, setSettingValue } from '../../../api/settings'; -import { ExtendedSettingDefinition } from '../../../types/settings'; - -type SettingValue = string | boolean | string[]; - -export function useGetValuesQuery(keys: string[]) { - return useQuery(['settings', 'values', keys] as const, ({ queryKey: [_a, _b, keys] }) => { - return getValues({ keys }); - }); -} - -export function useResetSettingsMutation() { - const queryClient = useQueryClient(); - return useMutation({ - mutationFn: (keys: string[]) => resetSettingValue({ keys: keys.join(',') }), - onSuccess: () => { - queryClient.invalidateQueries(['settings']); - }, - }); -} - -export function useSaveValuesMutation() { - const queryClient = useQueryClient(); - return useMutation({ - mutationFn: ( - values: { - newValue?: SettingValue; - definition: ExtendedSettingDefinition; - }[] - ) => { - return Promise.all( - values - .filter((v) => v.newValue !== undefined) - .map(async ({ newValue, definition }) => { - try { - if (isDefaultValue(newValue as string | boolean | string[], definition)) { - await resetSettingValue({ keys: definition.key }); - } else { - await setSettingValue(definition, newValue); - } - return { key: definition.key, success: true }; - } catch (error) { - return { key: definition.key, success: false }; - } - }) - ); - }, - onSuccess: () => { - queryClient.invalidateQueries(['settings']); - }, - }); -} - -export function useSaveValueMutation() { - const queryClient = useQueryClient(); - return useMutation({ - mutationFn: async ({ - newValue, - definition, - }: { - newValue: SettingValue; - definition: ExtendedSettingDefinition; - }) => { - if (isDefaultValue(newValue, definition)) { - await resetSettingValue({ keys: definition.key }); - } else { - await setSettingValue(definition, newValue); - } - }, - onSuccess: () => { - queryClient.invalidateQueries(['settings']); - }, - }); -} - -function isDefaultValue(value: SettingValue, definition: ExtendedSettingDefinition) { - const defaultValue = definition.defaultValue ?? ''; - if (definition.multiValues) { - return defaultValue === (value as string[]).join(','); - } - - return defaultValue === String(value); -} |