Files
hoppscotch/packages/hoppscotch-sh-admin/src/helpers/userManagement.ts
jamesgeorge007 dee7864a08 chore: address CR comments
- Implicitly infer the action type (bulk/individual) from the supplied deleted users list.
- Display toast messages one after the other by relying on the native toast APIs refraining from the need to maintain timeouts separately.
- Ensure the toast message about user deletion success/failure with the count is displayed only when above `0`.
- Cleanup.

Co-authored-by: amk-dev <akash.k.mohan98@gmail.com>
Co-authored-by: nivedin <nivedinp@gmail.com>
2024-03-28 12:58:09 +05:30

134 lines
3.6 KiB
TypeScript

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';
type IndividualActionMetadata = null;
type BulkActionMetadata = {
areMultipleUsersSelected: boolean;
deletedUserIDs: string[];
};
type ActionMetadata = IndividualActionMetadata | BulkActionMetadata;
type HandleUserDeletion = {
(deletedUsersList: UserDeletionResult[], metadata: ActionMetadata): void;
};
type ToastMessage = {
message: string;
state: 'success' | 'error';
};
const t = getI18n();
const toast = useToast();
const displayToastMessages = (
toastMessages: ToastMessage[],
currentIndex: number
) => {
const { message, state } = toastMessages[currentIndex];
toast[state](message, {
duration: 2000,
onComplete: () => {
if (currentIndex < toastMessages.length - 1) {
displayToastMessages(toastMessages, currentIndex + 1);
}
},
});
};
export const handleUserDeletion: HandleUserDeletion = (
deletedUsersList,
metadata
) => {
const uniqueErrorMessages = new Set(
deletedUsersList.map(({ errorMessage }) => errorMessage).filter(Boolean)
) as Set<string>;
const isBulkAction = deletedUsersList.length > 1;
// Show the success toast based on the action type if there are no errors
if (uniqueErrorMessages.size === 0) {
if (isBulkAction) {
toast.success(
(metadata as BulkActionMetadata).areMultipleUsersSelected
? t('state.delete_user_success')
: t('state.delete_users_success')
);
return;
}
toast.success(t('state.delete_user_success'));
return;
}
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'),
};
const errMsgMapKeys = Object.keys(errMsgMap);
const toastMessages: ToastMessage[] = [];
if (isBulkAction) {
const { areMultipleUsersSelected, deletedUserIDs } =
metadata as BulkActionMetadata;
// Indicates the actual count of users deleted (filtered via the `isDeleted` field)
const deletedUsersCount = deletedUserIDs.length;
if (areMultipleUsersSelected && deletedUsersCount > 0) {
toastMessages.push({
message: t('state.delete_some_users_success', {
count: deletedUsersCount,
}),
state: 'success',
});
}
const remainingDeletionsCount = deletedUsersList.length - deletedUsersCount;
if (remainingDeletionsCount > 0) {
toastMessages.push({
message: t('state.delete_some_users_failure', {
count: remainingDeletionsCount,
}),
state: 'error',
});
}
}
uniqueErrorMessages.forEach((errorMessage) => {
if (errMsgMapKeys.includes(errorMessage)) {
toastMessages.push({
message: errMsgMap[errorMessage as keyof typeof errMsgMap],
state: 'error',
});
}
});
// Fallback for the case where the error message is not in the compiled list
if (
Array.from(uniqueErrorMessages).some(
(key) => !((key as string) in errMsgMap)
)
) {
const fallbackErrMsg =
isBulkAction && (metadata as BulkActionMetadata).areMultipleUsersSelected
? t('state.delete_users_failure')
: t('state.delete_user_failure');
toastMessages.push({
message: fallbackErrMsg,
state: 'error',
});
}
displayToastMessages(toastMessages, 0);
};