import { FC, useEffect, useState } from 'react' import { IconLoader, Modal } from '@supabase/ui' import { loadStripe } from '@stripe/stripe-js' import { Elements } from '@stripe/react-stripe-js' import { useStore } from 'hooks' import { post } from 'lib/common/fetch' import { API_URL, STRIPE_PUBLIC_KEY } from 'lib/constants' import AddPaymentMethodForm from './AddPaymentMethodForm' interface Props { visible: boolean returnUrl: string onCancel: () => void } const stripePromise = loadStripe(STRIPE_PUBLIC_KEY) const AddNewPaymentMethodModal: FC = ({ visible, returnUrl, onCancel }) => { const { ui } = useStore() const [intent, setIntent] = useState() useEffect(() => { if (visible) { setupIntent() } }, [visible]) const setupIntent = async () => { setIntent(undefined) const orgSlug = ui.selectedOrganization?.slug ?? '' const intent = await post(`${API_URL}/organizations/${orgSlug}/payments/setup-intent`, {}) if (intent.error) { return ui.setNotification({ category: 'error', message: intent.error.message, error: intent.error, }) } setIntent(intent) } const options = { clientSecret: intent ? intent.client_secret : '', appearance: { theme: 'night', labels: 'floating' }, } as any return (
{intent !== undefined ? ( ) : (
)}
) } export default AddNewPaymentMethodModal