DeletePaymentMethodModal.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { useParams } from 'common'
  2. import { toast } from 'sonner'
  3. import { Button, Modal } from 'ui'
  4. import { Admonition } from 'ui-patterns'
  5. import { useOrganizationPaymentMethodDeleteMutation } from '@/data/organizations/organization-payment-method-delete-mutation'
  6. import type { OrganizationPaymentMethod } from '@/data/organizations/organization-payment-methods-query'
  7. export interface DeletePaymentMethodModalProps {
  8. selectedPaymentMethod?: OrganizationPaymentMethod
  9. onClose: () => void
  10. }
  11. const DeletePaymentMethodModal = ({
  12. selectedPaymentMethod,
  13. onClose,
  14. }: DeletePaymentMethodModalProps) => {
  15. const { slug } = useParams()
  16. const { mutate: deletePayment, isPending: isDeleting } =
  17. useOrganizationPaymentMethodDeleteMutation({
  18. onSuccess: () => {
  19. toast.success(
  20. `Successfully removed payment method ending with ${selectedPaymentMethod?.card?.last4}`
  21. )
  22. onClose()
  23. },
  24. })
  25. const onConfirmDelete = async () => {
  26. if (!slug) return console.error('Slug is required')
  27. if (!selectedPaymentMethod) return console.error('Card ID is required')
  28. deletePayment({ slug, cardId: selectedPaymentMethod.id })
  29. }
  30. return (
  31. <Modal
  32. visible={selectedPaymentMethod !== undefined}
  33. size="medium"
  34. header={`Confirm to delete payment method ending with ${selectedPaymentMethod?.card?.last4}`}
  35. onCancel={() => onClose()}
  36. customFooter={
  37. <div className="flex items-center gap-2">
  38. <Button type="default" disabled={isDeleting} onClick={() => onClose()}>
  39. Cancel
  40. </Button>
  41. <Button
  42. type="primary"
  43. disabled={isDeleting}
  44. loading={isDeleting}
  45. onClick={onConfirmDelete}
  46. >
  47. Confirm
  48. </Button>
  49. </div>
  50. }
  51. >
  52. <Modal.Content>
  53. <Admonition
  54. type="default"
  55. title="This will permanently delete your payment method."
  56. description="You can re-add the payment method any time."
  57. />
  58. </Modal.Content>
  59. </Modal>
  60. )
  61. }
  62. export default DeletePaymentMethodModal