From 446d29f5196b4a5147bccf49af7b137d30d63819 Mon Sep 17 00:00:00 2001 From: Joel Jacob Stephen <70131076+JoelJacobStephen@users.noreply.github.com> Date: Fri, 1 Mar 2024 18:54:59 +0530 Subject: [PATCH] refactor: improved handling of mutation execution in config handler --- .../src/composables/useConfigHandler.ts | 135 +++++++++--------- 1 file changed, 70 insertions(+), 65 deletions(-) diff --git a/packages/hoppscotch-sh-admin/src/composables/useConfigHandler.ts b/packages/hoppscotch-sh-admin/src/composables/useConfigHandler.ts index 6a1a5f7bf..7aaaf18ea 100644 --- a/packages/hoppscotch-sh-admin/src/composables/useConfigHandler.ts +++ b/packages/hoppscotch-sh-admin/src/composables/useConfigHandler.ts @@ -1,20 +1,20 @@ -import { computed, onMounted, ref } from 'vue'; +import { AnyVariables, UseMutationResponse } from '@urql/vue'; import { cloneDeep } from 'lodash-es'; -import { UseMutationResponse } from '@urql/vue'; -import { useClientHandler } from './useClientHandler'; -import { useToast } from './toast'; +import { computed, onMounted, ref } from 'vue'; import { useI18n } from '~/composables/i18n'; import { + AllowedAuthProvidersDocument, + EnableAndDisableSsoArgs, + EnableAndDisableSsoMutation, + InfraConfigArgs, InfraConfigEnum, InfraConfigsDocument, - AllowedAuthProvidersDocument, - EnableAndDisableSsoMutation, - UpdateInfraConfigsMutation, ResetInfraConfigsMutation, - EnableAndDisableSsoArgs, ToggleAnalyticsCollectionMutation, - InfraConfigArgs, + UpdateInfraConfigsMutation, } from '~/helpers/backend/graphql'; +import { useToast } from './toast'; +import { useClientHandler } from './useClientHandler'; // Types export type SsoAuthProviders = 'google' | 'microsoft' | 'github'; @@ -278,15 +278,23 @@ export function useConfigHandler(updatedConfigs?: Config) { // Checking if any of the config fields are empty const isFieldEmpty = (field: string) => field.trim() === ''; - const AreAnyConfigFieldsEmpty = (config: Config): boolean => { - const providerFieldsEmpty = Object.values(config.providers).some( - (provider) => Object.values(provider.fields).some(isFieldEmpty) - ); - const mailFieldsEmpty = Object.values(config.mailConfigs.fields).some( - isFieldEmpty - ); + type ConfigSection = { + enabled: boolean; + fields: Record; + }; - return providerFieldsEmpty || mailFieldsEmpty; + const AreAnyConfigFieldsEmpty = (config: Config): boolean => { + const sections: Array<[ConfigSection, boolean]> = [ + [config.providers.github, config.providers.github.enabled], + [config.providers.google, config.providers.google.enabled], + [config.providers.microsoft, config.providers.microsoft.enabled], + [config.mailConfigs, config.mailConfigs.enabled], + ]; + + return sections.some( + ([section, enabled]) => + enabled && Object.values(section.fields).some(isFieldEmpty) + ); }; // Transforming the working configs back into the format required by the mutations @@ -313,73 +321,70 @@ export function useConfigHandler(updatedConfigs?: Config) { ]; }); - // Updating the auth provider configurations - const updateAuthProvider = async ( - updateProviderStatus: UseMutationResponse - ) => { - const variables = { - providerInfo: - updatedAllowedAuthProviders.value as EnableAndDisableSsoArgs[], - }; - - const result = await updateProviderStatus.executeMutation(variables); + // Generic function to handle mutation execution and error handling + const executeMutation = async ( + mutation: UseMutationResponse, + variables: AnyVariables = undefined, + errorMessage: string + ): Promise => { + const result = await mutation.executeMutation(variables); if (result.error) { - toast.error(t('configs.auth_providers.update_failure')); + toast.error(t(errorMessage)); return false; } return true; }; + // Updating the auth provider configurations + const updateAuthProvider = async ( + updateProviderStatus: UseMutationResponse + ) => + executeMutation( + updateProviderStatus, + { + providerInfo: + updatedAllowedAuthProviders.value as EnableAndDisableSsoArgs[], + }, + 'configs.auth_providers.update_failure' + ); + // Updating the infra configurations const updateInfraConfigs = async ( updateInfraConfigsMutation: UseMutationResponse - ) => { - const variables = { - infraConfigs: updatedInfraConfigs.value as InfraConfigArgs[], - }; - - const result = await updateInfraConfigsMutation.executeMutation(variables); - - if (result.error) { - toast.error(t('configs.mail_configs.update_failure')); - return false; - } - - return true; - }; + ) => + executeMutation( + updateInfraConfigsMutation, + { + infraConfigs: updatedInfraConfigs.value as InfraConfigArgs[], + }, + 'configs.mail_configs.update_failure' + ); // Resetting the infra configurations const resetInfraConfigs = async ( resetInfraConfigsMutation: UseMutationResponse - ) => { - const result = await resetInfraConfigsMutation.executeMutation(); - - if (result.error) { - toast.error(t('configs.reset.failure')); - return false; - } - return true; - }; + ) => + executeMutation( + resetInfraConfigsMutation, + undefined, + 'configs.reset.failure' + ); // Updating the data sharing configurations const updateDataSharingConfigs = async ( toggleDataSharingMutation: UseMutationResponse - ) => { - const variables = { - status: updatedConfigs?.dataSharingConfigs.enabled ? 'ENABLE' : 'DISABLE', - }; - - const result = await toggleDataSharingMutation.executeMutation(variables); - - if (result.error) { - toast.error(t('configs.data_sharing.update_failure')); - return false; - } - - return true; - }; + ) => + executeMutation( + toggleDataSharingMutation, + { + status: updatedConfigs?.dataSharingConfigs.enabled + ? 'ENABLE' + : 'DISABLE', + }, + 'configs.data_sharing.update_failure' + ); return { currentConfigs,