ChangePaymentMethodModal.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { useParams } from 'common'
  2. import { toast } from 'sonner'
  3. import { Button, Modal } from 'ui'
  4. import { useOrganizationPaymentMethodMarkAsDefaultMutation } from '@/data/organizations/organization-payment-method-default-mutation'
  5. import type { OrganizationPaymentMethod } from '@/data/organizations/organization-payment-methods-query'
  6. export interface ChangePaymentMethodModalProps {
  7. selectedPaymentMethod?: OrganizationPaymentMethod
  8. onClose: () => void
  9. }
  10. const ChangePaymentMethodModal = ({
  11. selectedPaymentMethod,
  12. onClose,
  13. }: ChangePaymentMethodModalProps) => {
  14. const { slug } = useParams()
  15. const { mutate: markAsDefault, isPending: isUpdating } =
  16. useOrganizationPaymentMethodMarkAsDefaultMutation({
  17. onSuccess: () => {
  18. toast.success(
  19. `Successfully changed payment method to the card ending with ${
  20. selectedPaymentMethod!.card!.last4
  21. }`
  22. )
  23. onClose()
  24. },
  25. onError: (error) => {
  26. toast.error(`Failed to change payment method: ${error.message}`)
  27. },
  28. })
  29. const onConfirmUpdate = async () => {
  30. if (!slug) return console.error('Slug is required')
  31. if (!selectedPaymentMethod) return console.error('Card ID is required')
  32. markAsDefault({
  33. slug,
  34. paymentMethodId: selectedPaymentMethod.id,
  35. })
  36. }
  37. return (
  38. <Modal
  39. visible={selectedPaymentMethod !== undefined}
  40. size="medium"
  41. header={`Confirm to use payment method ending with ${selectedPaymentMethod?.card?.last4}`}
  42. onCancel={() => onClose()}
  43. customFooter={
  44. <div className="flex items-center gap-2">
  45. <Button type="default" disabled={isUpdating} onClick={() => onClose()}>
  46. Cancel
  47. </Button>
  48. <Button
  49. type="primary"
  50. disabled={isUpdating}
  51. loading={isUpdating}
  52. onClick={onConfirmUpdate}
  53. >
  54. Confirm
  55. </Button>
  56. </div>
  57. }
  58. >
  59. <Modal.Content>
  60. <p className="text-sm">
  61. Upon clicking confirm, all future charges will be deducted from the card ending with{' '}
  62. {selectedPaymentMethod?.card?.last4}. There are no immediate charges.
  63. </p>
  64. </Modal.Content>
  65. </Modal>
  66. )
  67. }
  68. export default ChangePaymentMethodModal