-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathRemoveManagerButton.tsx
More file actions
57 lines (51 loc) · 1.95 KB
/
RemoveManagerButton.tsx
File metadata and controls
57 lines (51 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { IconButton } from '@rocket.chat/fuselage';
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
import { GenericModal, GenericTableCell } from '@rocket.chat/ui-client';
import { useSetModal, useToastMessageDispatch } from '@rocket.chat/ui-contexts';
import { useQueryClient } from '@tanstack/react-query';
import type { MouseEvent, ReactElement } from 'react';
import { useTranslation } from 'react-i18next';
import { useEndpointMutation } from '../../../hooks/useEndpointMutation';
import { omnichannelQueryKeys } from '../../../lib/queryKeys';
const RemoveManagerButton = ({ _id }: { _id: string }): ReactElement => {
const { t } = useTranslation();
const queryClient = useQueryClient();
const { mutateAsync: deleteAction } = useEndpointMutation('DELETE', '/v1/livechat/users/:type/:_id', {
keys: { type: 'manager', _id },
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: omnichannelQueryKeys.managers() });
},
});
const setModal = useSetModal();
const dispatchToastMessage = useToastMessageDispatch();
const handleRemoveClick = useEffectEvent(async () => {
await deleteAction();
});
const handleDelete = useEffectEvent((e: MouseEvent) => {
e.stopPropagation();
const onDeleteManager = async (): Promise<void> => {
try {
await handleRemoveClick();
dispatchToastMessage({ type: 'success', message: t('Manager_removed') });
} catch (error: unknown) {
(typeof error === 'string' || error instanceof Error) && dispatchToastMessage({ type: 'error', message: error });
}
setModal();
};
setModal(
<GenericModal
variant='danger'
onConfirm={onDeleteManager}
onCancel={(): void => setModal()}
onClose={(): void => setModal()}
confirmText={t('Delete')}
/>,
);
});
return (
<GenericTableCell fontScale='p2' color='hint' withTruncatedText>
<IconButton small icon='trash' title={t('Remove')} onClick={handleDelete} />
</GenericTableCell>
);
};
export default RemoveManagerButton;