import { Elements } from '@stripe/react-stripe-js' import { loadStripe, type StripeAddressElement, type StripeElementsOptions, } from '@stripe/stripe-js' import { useQueryClient } from '@tanstack/react-query' import { useParams } from 'common' import { useTheme } from 'next-themes' import { useMemo, useRef, useState, type FormEvent } from 'react' import { toast } from 'sonner' import { Button, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogSection, DialogTitle, Form, } from 'ui' import { formatBillingAddress, formatTaxId } from './PlanUpdatePayment.utils' import { getAddressElementAppearanceOptions, STRIPE_ELEMENT_FONTS, } from '@/components/interfaces/Billing/Payment/Payment.utils' import { BillingCustomerDataForm } from '@/components/interfaces/Organization/BillingSettings/BillingCustomerData/BillingCustomerDataForm' import { useBillingCustomerDataForm, type BillingAddressPayload, } from '@/components/interfaces/Organization/BillingSettings/BillingCustomerData/useBillingCustomerDataForm' import { organizationKeys } from '@/data/organizations/keys' import type { OrganizationCustomerProfileData } from '@/data/organizations/organization-customer-profile-query' import { useOrganizationCustomerProfileUpdateMutation } from '@/data/organizations/organization-customer-profile-update-mutation' import type { OrganizationTaxIdData } from '@/data/organizations/organization-tax-id-query' import type { CustomerAddress, CustomerTaxId } from '@/data/organizations/types' import { STRIPE_PUBLIC_KEY } from '@/lib/constants' const stripePromise = loadStripe(STRIPE_PUBLIC_KEY) interface BillingAddressEditFormProps { address: CustomerAddress billingName: string | undefined taxId: CustomerTaxId | null | undefined /** The form starts from an edit that failed to save, so there is something to retry. */ isRetry: boolean onSave: (data: BillingAddressPayload) => Promise onCancel: () => void } /** Mounted only while the dialog is open, so every edit starts from the saved address. */ const BillingAddressEditForm = ({ address, billingName, taxId, isRetry, onSave, onCancel, }: BillingAddressEditFormProps) => { const { resolvedTheme } = useTheme() const addressElementRef = useRef(null) const [isSaving, setIsSaving] = useState(false) // The form hook resets itself whenever this object changes, so a fresh literal on every // render would reset it on every render. const customerProfile = useMemo( () => ({ address, billing_name: billingName }), [address, billingName] ) const { form, handleSubmit, isDirty, resetKey, onAddressChange, applyAddressElementValue, addressCountry, addressOptions, } = useBillingCustomerDataForm({ customerProfile, taxId, onCustomerDataChange: onSave, }) const stripeElementsOptions: StripeElementsOptions = useMemo( () => ({ mode: 'setup', currency: 'usd', appearance: getAddressElementAppearanceOptions(resolvedTheme), fonts: STRIPE_ELEMENT_FONTS, }) as StripeElementsOptions, [resolvedTheme] ) const handleFormSubmit = async (event: FormEvent) => { event.preventDefault() event.stopPropagation() setIsSaving(true) try { if (addressElementRef.current) { applyAddressElementValue(await addressElementRef.current.getValue()) } const result = await handleSubmit() if (result.status === 'error') toast.error(result.message) } catch { // A failed save is reported by `onSave`. } finally { setIsSaving(false) } } return (
{ addressElementRef.current = element }} addressCountry={addressCountry} />
) } type BillingChange = Pick interface PaymentBillingAddressProps { address: CustomerAddress billingName: string | undefined taxId: CustomerTaxId | null | undefined /** * An unresolved tax ID is not an absent one: saving an address edit while it is unknown * would submit `tax_id: null` and wipe the real one, so editing waits for it. */ isTaxIdLoading?: boolean isTaxIdError?: boolean onRetryTaxId?: () => void disabled?: boolean /** * The address and tax ID the plan preview should price against: the edit as soon as it * is made, and the previous ones again if saving it fails. */ onChange?: (billing: BillingChange) => void } /** * The address the plan change is billed to, with an edit dialog that saves it to the * organization's customer profile. * * An edit shows up straight away: the query cache is updated before the request goes out * and the dialog closes, instead of waiting a second or two on the API. If the request * fails the cache is put back and the dialog reopens with what was entered. */ export const PaymentBillingAddress = ({ address, billingName, taxId, isTaxIdLoading = false, isTaxIdError = false, onRetryTaxId, disabled, onChange, }: PaymentBillingAddressProps) => { const { slug } = useParams() const queryClient = useQueryClient() const [isEditing, setIsEditing] = useState(false) const [failedEdit, setFailedEdit] = useState(null) const { mutateAsync: updateCustomerProfile } = useOrganizationCustomerProfileUpdateMutation({ onError: () => {}, }) const saveBillingAddress = async (data: BillingAddressPayload) => { const profileKey = organizationKeys.customerProfile(slug) const taxIdKey = organizationKeys.taxId(slug) // A refetch already in flight would land with the old address and overwrite the edit. await Promise.all([ queryClient.cancelQueries({ queryKey: profileKey }), queryClient.cancelQueries({ queryKey: taxIdKey }), ]) const previousProfile = queryClient.getQueryData(profileKey) const previousTaxId = queryClient.getQueryData(taxIdKey) queryClient.setQueryData(profileKey, (previous) => previous ? { ...previous, address: data.address ?? previous.address, billing_name: data.billing_name, } : previous ) queryClient.setQueryData(taxIdKey, data.tax_id) setFailedEdit(null) setIsEditing(false) onChange?.({ address: data.address, tax_id: data.tax_id }) try { await updateCustomerProfile({ slug, address: data.address, billing_name: data.billing_name, tax_id: data.tax_id, }) // The organization list carries the flags that nag about a missing address or tax ID. queryClient.invalidateQueries({ queryKey: organizationKeys.list() }) } catch (error) { queryClient.setQueryData(profileKey, previousProfile) queryClient.setQueryData(taxIdKey, previousTaxId) onChange?.({ address: previousProfile?.address ?? undefined, tax_id: previousTaxId ?? null }) toast.error( `Failed updating billing address: ${error instanceof Error ? error.message : 'Unknown error'}` ) setFailedEdit(data) setIsEditing(true) } } return ( <>

Billing address

{billingName && (

{billingName}

)}

{formatBillingAddress(address)}

{taxId && (

{formatTaxId(taxId, address.country)}

)}
{isTaxIdError ? ( ) : ( )}
{ setIsEditing(open) if (!open) setFailedEdit(null) }} > Edit billing address Applies to this payment and every upcoming invoice. Past invoices are not affected. { setIsEditing(false) setFailedEdit(null) }} /> ) }