refactor: improved handling of mutation execution in config handler
This commit is contained in:
@@ -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<string, string>;
|
||||
};
|
||||
|
||||
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<EnableAndDisableSsoMutation>
|
||||
) => {
|
||||
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 <T, V>(
|
||||
mutation: UseMutationResponse<T>,
|
||||
variables: AnyVariables = undefined,
|
||||
errorMessage: string
|
||||
): Promise<boolean> => {
|
||||
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<EnableAndDisableSsoMutation>
|
||||
) =>
|
||||
executeMutation(
|
||||
updateProviderStatus,
|
||||
{
|
||||
providerInfo:
|
||||
updatedAllowedAuthProviders.value as EnableAndDisableSsoArgs[],
|
||||
},
|
||||
'configs.auth_providers.update_failure'
|
||||
);
|
||||
|
||||
// Updating the infra configurations
|
||||
const updateInfraConfigs = async (
|
||||
updateInfraConfigsMutation: UseMutationResponse<UpdateInfraConfigsMutation>
|
||||
) => {
|
||||
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<ResetInfraConfigsMutation>
|
||||
) => {
|
||||
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<ToggleAnalyticsCollectionMutation>
|
||||
) => {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user