refactor(sh-admin): improved error handling and dynamic user actions in admin dashboard (#4044)
* feat: new helper functions for better error management * refactor: new i18n strings * refactor: better error handling in invite modal and members component * refactor: better user management * refactor: better error handling in config handler * refactor: updated logic of dynamic action row * refactor: better naming for computed properties * feat: new error message when an admin tries to invite himself * refactor: updated error message when user is already invited * refactor: reverted i18n string for user already invited back to the old string * refactor: removed show prop from invite modal * refactor: improved implementation for getting the compiled error messages * feat: new error message when email inputted is of an invalid format * refactor: minor optimization --------- Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
committed by
GitHub
parent
5fd7c28894
commit
5805826994
@@ -8,9 +8,14 @@ export const UNAUTHORIZED = 'Unauthorized' as const;
|
||||
// Sometimes the backend returns Unauthorized error message as follows:
|
||||
export const GRAPHQL_UNAUTHORIZED = '[GraphQL] Unauthorized' as const;
|
||||
|
||||
// When the email is invalid
|
||||
export const INVALID_EMAIL = '[GraphQL] invalid/email' as const;
|
||||
|
||||
// When trying to remove the only admin account
|
||||
export const ONLY_ONE_ADMIN_ACCOUNT_FOUND =
|
||||
'[GraphQL] admin/only_one_admin_account_found' as const;
|
||||
|
||||
// When trying to delete an admin account
|
||||
export const ADMIN_CANNOT_BE_DELETED =
|
||||
'admin/admin_can_not_be_deleted' as const;
|
||||
|
||||
@@ -19,4 +24,53 @@ export const USER_ALREADY_INVITED =
|
||||
'[GraphQL] admin/user_already_invited' as const;
|
||||
|
||||
// When attempting to delete a user who is an owner of a team
|
||||
export const USER_IS_OWNER = 'user/is_owner' as const;
|
||||
export const USER_IS_OWNER = 'user/is_owner';
|
||||
|
||||
// When attempting to delete a user who is the only owner of a team
|
||||
export const TEAM_ONLY_ONE_OWNER = '[GraphQL] team/only_one_owner';
|
||||
|
||||
// Even one auth provider is not specified
|
||||
export const AUTH_PROVIDER_NOT_SPECIFIED =
|
||||
'[GraphQL] auth/provider_not_specified' as const;
|
||||
|
||||
export const BOTH_EMAILS_CANNOT_BE_SAME =
|
||||
'[GraphQL] email/both_emails_cannot_be_same' as const;
|
||||
|
||||
type ErrorMessages = {
|
||||
message: string;
|
||||
alternateMessage?: string;
|
||||
};
|
||||
|
||||
const ERROR_MESSAGES: Record<string, ErrorMessages> = {
|
||||
[INVALID_EMAIL]: {
|
||||
message: 'state.invalid_email',
|
||||
},
|
||||
[ONLY_ONE_ADMIN_ACCOUNT_FOUND]: {
|
||||
message: 'state.remove_admin_failure_only_one_admin',
|
||||
},
|
||||
[ADMIN_CANNOT_BE_DELETED]: {
|
||||
message: 'state.remove_admin_to_delete_user',
|
||||
alternateMessage: 'state.remove_admin_for_deletion',
|
||||
},
|
||||
[USER_ALREADY_INVITED]: {
|
||||
message: 'state.user_already_invited',
|
||||
},
|
||||
[USER_IS_OWNER]: {
|
||||
message: 'state.remove_owner_to_delete_user',
|
||||
alternateMessage: 'state.remove_owner_for_deletion',
|
||||
},
|
||||
[TEAM_ONLY_ONE_OWNER]: {
|
||||
message: 'state.remove_owner_failure_only_one_owner',
|
||||
},
|
||||
[AUTH_PROVIDER_NOT_SPECIFIED]: {
|
||||
message: 'configs.auth_providers.provider_not_specified',
|
||||
},
|
||||
[BOTH_EMAILS_CANNOT_BE_SAME]: {
|
||||
message: 'state.emails_cannot_be_same',
|
||||
},
|
||||
};
|
||||
|
||||
export const getCompiledErrorMessage = (name: string, altMessage = false) => {
|
||||
const error = ERROR_MESSAGES[name];
|
||||
return altMessage ? error?.alternateMessage ?? '' : error?.message ?? '';
|
||||
};
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { useToast } from '~/composables/toast';
|
||||
import { getI18n } from '~/modules/i18n';
|
||||
import { UserDeletionResult } from './backend/graphql';
|
||||
import { ADMIN_CANNOT_BE_DELETED, USER_IS_OWNER } from './errors';
|
||||
import {
|
||||
ADMIN_CANNOT_BE_DELETED,
|
||||
USER_IS_OWNER,
|
||||
getCompiledErrorMessage,
|
||||
} from './errors';
|
||||
|
||||
type ToastMessage = {
|
||||
message: string;
|
||||
@@ -49,14 +53,12 @@ export const handleUserDeletion = (deletedUsersList: UserDeletionResult[]) => {
|
||||
}
|
||||
|
||||
const errMsgMap = {
|
||||
[ADMIN_CANNOT_BE_DELETED]: isBulkAction
|
||||
? t('state.remove_admin_for_deletion')
|
||||
: t('state.remove_admin_to_delete_user'),
|
||||
|
||||
[USER_IS_OWNER]: isBulkAction
|
||||
? t('state.remove_owner_for_deletion')
|
||||
: t('state.remove_owner_to_delete_user'),
|
||||
[ADMIN_CANNOT_BE_DELETED]: t(
|
||||
getCompiledErrorMessage(ADMIN_CANNOT_BE_DELETED, isBulkAction)
|
||||
),
|
||||
[USER_IS_OWNER]: t(getCompiledErrorMessage(USER_IS_OWNER, isBulkAction)),
|
||||
};
|
||||
|
||||
const errMsgMapKeys = Object.keys(errMsgMap);
|
||||
|
||||
const toastMessages: ToastMessage[] = [];
|
||||
|
||||
Reference in New Issue
Block a user