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 { cloneDeep } from 'lodash-es';
|
||||||
import { UseMutationResponse } from '@urql/vue';
|
import { computed, onMounted, ref } from 'vue';
|
||||||
import { useClientHandler } from './useClientHandler';
|
|
||||||
import { useToast } from './toast';
|
|
||||||
import { useI18n } from '~/composables/i18n';
|
import { useI18n } from '~/composables/i18n';
|
||||||
import {
|
import {
|
||||||
|
AllowedAuthProvidersDocument,
|
||||||
|
EnableAndDisableSsoArgs,
|
||||||
|
EnableAndDisableSsoMutation,
|
||||||
|
InfraConfigArgs,
|
||||||
InfraConfigEnum,
|
InfraConfigEnum,
|
||||||
InfraConfigsDocument,
|
InfraConfigsDocument,
|
||||||
AllowedAuthProvidersDocument,
|
|
||||||
EnableAndDisableSsoMutation,
|
|
||||||
UpdateInfraConfigsMutation,
|
|
||||||
ResetInfraConfigsMutation,
|
ResetInfraConfigsMutation,
|
||||||
EnableAndDisableSsoArgs,
|
|
||||||
ToggleAnalyticsCollectionMutation,
|
ToggleAnalyticsCollectionMutation,
|
||||||
InfraConfigArgs,
|
UpdateInfraConfigsMutation,
|
||||||
} from '~/helpers/backend/graphql';
|
} from '~/helpers/backend/graphql';
|
||||||
|
import { useToast } from './toast';
|
||||||
|
import { useClientHandler } from './useClientHandler';
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
export type SsoAuthProviders = 'google' | 'microsoft' | 'github';
|
export type SsoAuthProviders = 'google' | 'microsoft' | 'github';
|
||||||
@@ -278,15 +278,23 @@ export function useConfigHandler(updatedConfigs?: Config) {
|
|||||||
// Checking if any of the config fields are empty
|
// Checking if any of the config fields are empty
|
||||||
const isFieldEmpty = (field: string) => field.trim() === '';
|
const isFieldEmpty = (field: string) => field.trim() === '';
|
||||||
|
|
||||||
const AreAnyConfigFieldsEmpty = (config: Config): boolean => {
|
type ConfigSection = {
|
||||||
const providerFieldsEmpty = Object.values(config.providers).some(
|
enabled: boolean;
|
||||||
(provider) => Object.values(provider.fields).some(isFieldEmpty)
|
fields: Record<string, string>;
|
||||||
);
|
};
|
||||||
const mailFieldsEmpty = Object.values(config.mailConfigs.fields).some(
|
|
||||||
isFieldEmpty
|
|
||||||
);
|
|
||||||
|
|
||||||
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
|
// 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
|
// Generic function to handle mutation execution and error handling
|
||||||
const updateAuthProvider = async (
|
const executeMutation = async <T, V>(
|
||||||
updateProviderStatus: UseMutationResponse<EnableAndDisableSsoMutation>
|
mutation: UseMutationResponse<T>,
|
||||||
) => {
|
variables: AnyVariables = undefined,
|
||||||
const variables = {
|
errorMessage: string
|
||||||
providerInfo:
|
): Promise<boolean> => {
|
||||||
updatedAllowedAuthProviders.value as EnableAndDisableSsoArgs[],
|
const result = await mutation.executeMutation(variables);
|
||||||
};
|
|
||||||
|
|
||||||
const result = await updateProviderStatus.executeMutation(variables);
|
|
||||||
|
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
toast.error(t('configs.auth_providers.update_failure'));
|
toast.error(t(errorMessage));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
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
|
// Updating the infra configurations
|
||||||
const updateInfraConfigs = async (
|
const updateInfraConfigs = async (
|
||||||
updateInfraConfigsMutation: UseMutationResponse<UpdateInfraConfigsMutation>
|
updateInfraConfigsMutation: UseMutationResponse<UpdateInfraConfigsMutation>
|
||||||
) => {
|
) =>
|
||||||
const variables = {
|
executeMutation(
|
||||||
|
updateInfraConfigsMutation,
|
||||||
|
{
|
||||||
infraConfigs: updatedInfraConfigs.value as InfraConfigArgs[],
|
infraConfigs: updatedInfraConfigs.value as InfraConfigArgs[],
|
||||||
};
|
},
|
||||||
|
'configs.mail_configs.update_failure'
|
||||||
const result = await updateInfraConfigsMutation.executeMutation(variables);
|
);
|
||||||
|
|
||||||
if (result.error) {
|
|
||||||
toast.error(t('configs.mail_configs.update_failure'));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Resetting the infra configurations
|
// Resetting the infra configurations
|
||||||
const resetInfraConfigs = async (
|
const resetInfraConfigs = async (
|
||||||
resetInfraConfigsMutation: UseMutationResponse<ResetInfraConfigsMutation>
|
resetInfraConfigsMutation: UseMutationResponse<ResetInfraConfigsMutation>
|
||||||
) => {
|
) =>
|
||||||
const result = await resetInfraConfigsMutation.executeMutation();
|
executeMutation(
|
||||||
|
resetInfraConfigsMutation,
|
||||||
if (result.error) {
|
undefined,
|
||||||
toast.error(t('configs.reset.failure'));
|
'configs.reset.failure'
|
||||||
return false;
|
);
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Updating the data sharing configurations
|
// Updating the data sharing configurations
|
||||||
const updateDataSharingConfigs = async (
|
const updateDataSharingConfigs = async (
|
||||||
toggleDataSharingMutation: UseMutationResponse<ToggleAnalyticsCollectionMutation>
|
toggleDataSharingMutation: UseMutationResponse<ToggleAnalyticsCollectionMutation>
|
||||||
) => {
|
) =>
|
||||||
const variables = {
|
executeMutation(
|
||||||
status: updatedConfigs?.dataSharingConfigs.enabled ? 'ENABLE' : 'DISABLE',
|
toggleDataSharingMutation,
|
||||||
};
|
{
|
||||||
|
status: updatedConfigs?.dataSharingConfigs.enabled
|
||||||
const result = await toggleDataSharingMutation.executeMutation(variables);
|
? 'ENABLE'
|
||||||
|
: 'DISABLE',
|
||||||
if (result.error) {
|
},
|
||||||
toast.error(t('configs.data_sharing.update_failure'));
|
'configs.data_sharing.update_failure'
|
||||||
return false;
|
);
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
currentConfigs,
|
currentConfigs,
|
||||||
|
|||||||
Reference in New Issue
Block a user