BanUserButton.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Modal, Button } from 'antd';
  2. import { ExclamationCircleFilled, QuestionCircleFilled, StopTwoTone } from '@ant-design/icons';
  3. import { FC } from 'react';
  4. import { USER_ENABLED_TOGGLE, fetchData } from '../utils/apis';
  5. import { User } from '../types/chat';
  6. export type BanUserButtonProps = {
  7. user: User;
  8. isEnabled: Boolean; // = this user's current status
  9. label?: string;
  10. onClick?: () => void;
  11. };
  12. export const BanUserButton: FC<BanUserButtonProps> = ({ user, isEnabled, label, onClick }) => {
  13. async function buttonClicked({ id }): Promise<Boolean> {
  14. const data = {
  15. userId: id,
  16. enabled: !isEnabled, // set user to this value
  17. };
  18. try {
  19. const result = await fetchData(USER_ENABLED_TOGGLE, {
  20. data,
  21. method: 'POST',
  22. auth: true,
  23. });
  24. return result.success;
  25. } catch (e) {
  26. // eslint-disable-next-line no-console
  27. console.error(e);
  28. }
  29. return false;
  30. }
  31. const actionString = isEnabled ? 'ban' : 'unban';
  32. const icon = isEnabled ? (
  33. <ExclamationCircleFilled style={{ color: 'var(--ant-error)' }} />
  34. ) : (
  35. <QuestionCircleFilled style={{ color: 'var(--ant-warning)' }} />
  36. );
  37. const content = (
  38. <>
  39. Are you sure you want to {actionString} <strong>{user.displayName}</strong>
  40. {isEnabled ? ' and remove their messages?' : '?'}
  41. </>
  42. );
  43. const confirmBlockAction = () => {
  44. Modal.confirm({
  45. title: `Confirm ${actionString}`,
  46. content,
  47. onCancel: () => {},
  48. onOk: () =>
  49. new Promise((resolve, reject) => {
  50. const result = buttonClicked(user);
  51. if (result) {
  52. // wait a bit before closing so the user/client tables repopulate
  53. // GW: TODO: put users/clients data in global app context instead, then call a function here to update that state. (current in another branch)
  54. setTimeout(() => {
  55. resolve(result);
  56. onClick?.();
  57. }, 3000);
  58. } else {
  59. reject();
  60. }
  61. }),
  62. okType: 'danger',
  63. okText: isEnabled ? 'Absolutely' : null,
  64. icon,
  65. });
  66. };
  67. return (
  68. <Button
  69. onClick={confirmBlockAction}
  70. size="small"
  71. icon={isEnabled ? <StopTwoTone twoToneColor="#ff4d4f" /> : null}
  72. className="block-user-button"
  73. >
  74. {label || actionString}
  75. </Button>
  76. );
  77. };
  78. BanUserButton.defaultProps = {
  79. label: '',
  80. onClick: null,
  81. };