remove.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import Icon from '../../../../../shared/components/icon'
  2. import Tooltip from '../../../../../shared/components/tooltip'
  3. import { Button } from 'react-bootstrap'
  4. import { useTranslation } from 'react-i18next'
  5. import { UserEmailData } from '../../../../../../../types/user-email'
  6. import { useUserEmailsContext } from '../../../context/user-email-context'
  7. import { postJSON } from '../../../../../infrastructure/fetch-json'
  8. import { UseAsyncReturnType } from '../../../../../shared/hooks/use-async'
  9. function DeleteButton({ children, disabled, onClick }: Button.ButtonProps) {
  10. return (
  11. <Button
  12. bsSize="small"
  13. bsStyle="danger"
  14. disabled={disabled}
  15. onClick={onClick}
  16. >
  17. {children}
  18. </Button>
  19. )
  20. }
  21. type RemoveProps = {
  22. userEmailData: UserEmailData
  23. deleteEmailAsync: UseAsyncReturnType
  24. }
  25. function Remove({ userEmailData, deleteEmailAsync }: RemoveProps) {
  26. const { t } = useTranslation()
  27. const { state, deleteEmail, resetLeaversSurveyExpiration } =
  28. useUserEmailsContext()
  29. const handleRemoveUserEmail = () => {
  30. deleteEmailAsync
  31. .runAsync(
  32. postJSON('/user/emails/delete', {
  33. body: {
  34. email: userEmailData.email,
  35. },
  36. })
  37. )
  38. .then(() => {
  39. deleteEmail(userEmailData.email)
  40. resetLeaversSurveyExpiration(userEmailData)
  41. })
  42. .catch(() => {})
  43. }
  44. if (deleteEmailAsync.isLoading) {
  45. return <DeleteButton disabled>{t('deleting')}...</DeleteButton>
  46. }
  47. return (
  48. <Tooltip
  49. id={userEmailData.email}
  50. description={
  51. userEmailData.default
  52. ? t('please_change_primary_to_remove')
  53. : t('remove')
  54. }
  55. overlayProps={{ placement: userEmailData.default ? 'left' : 'top' }}
  56. >
  57. <span>
  58. <DeleteButton
  59. disabled={state.isLoading || userEmailData.default}
  60. onClick={handleRemoveUserEmail}
  61. >
  62. <Icon
  63. type="trash"
  64. fw
  65. accessibilityLabel={
  66. userEmailData.default
  67. ? t('please_change_primary_to_remove')
  68. : t('remove')
  69. }
  70. />
  71. </DeleteButton>
  72. </span>
  73. </Tooltip>
  74. )
  75. }
  76. export default Remove