Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Convert set up intent in new-with-subscription to RQ mutation

Joshen Lim4427a20d1e99
diff --git a/studio/data/organizations/organization-payment-method-setup-intent-mutation.ts b/studio/data/organizations/organization-payment-method-setup-intent-mutation.ts
index 6e5749aab23..e442afa877f 100644
--- a/studio/data/organizations/organization-payment-method-setup-intent-mutation.ts
+++ b/studio/data/organizations/organization-payment-method-setup-intent-mutation.ts
@@ -1,4 +1,4 @@
-import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import { toast } from 'react-hot-toast'
import { post } from 'data/fetchers'
@@ -37,8 +37,6 @@ export const useOrganizationPaymentMethodSetupIntent = ({
>,
'mutationFn'
> = {}) => {
- const queryClient = useQueryClient()
-
return useMutation<
OrganizationPaymentMethodSetupIntentData,
ResponseError,
diff --git a/studio/data/stripe/setup-intent-mutation.ts b/studio/data/stripe/setup-intent-mutation.ts
new file mode 100644
index 00000000000..e6fab19727f
--- /dev/null
+++ b/studio/data/stripe/setup-intent-mutation.ts
@@ -0,0 +1,46 @@
+import { useMutation, UseMutationOptions } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { post } from 'data/fetchers'
+import { ResponseError } from 'types'
+
+export type SetupIntentVariables = {
+ hcaptchaToken: string
+}
+
+export async function setupIntent({ hcaptchaToken }: SetupIntentVariables) {
+ const { data, error } = await post('/platform/stripe/setup-intent', {
+ // @ts-ignore [Joshen] API seems to be having the wrong spec
+ body: { hcaptchaToken },
+ })
+ if (error) throw error
+ return data
+}
+
+type SetupIntentData = Awaited<ReturnType<typeof setupIntent>>
+
+export const useSetupIntent = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<SetupIntentData, ResponseError, SetupIntentVariables>,
+ 'mutationFn'
+> = {}) => {
+ return useMutation<SetupIntentData, ResponseError, SetupIntentVariables>(
+ (vars) => setupIntent(vars),
+ {
+ async onSuccess(data, variables, context) {
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to setup intent: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/pages/new-with-subscription.tsx b/studio/pages/new-with-subscription.tsx
index 48ac8eb952a..c2d7d982ee9 100644
--- a/studio/pages/new-with-subscription.tsx
+++ b/studio/pages/new-with-subscription.tsx
@@ -1,18 +1,18 @@
-import { useCallback, useEffect, useState } from 'react'
-import { observer } from 'mobx-react-lite'
-
-import { API_URL, STRIPE_PUBLIC_KEY } from 'lib/constants'
-import { useFlag, useStore } from 'hooks'
-import { post } from 'lib/common/fetch'
-import { WizardLayout } from 'components/layouts'
-import { NextPageWithLayout } from 'types'
+import HCaptcha from '@hcaptcha/react-hcaptcha'
import { Elements } from '@stripe/react-stripe-js'
import { loadStripe } from '@stripe/stripe-js'
-import { useIsHCaptchaLoaded } from 'stores/hcaptcha-loaded-store'
-import HCaptcha from '@hcaptcha/react-hcaptcha'
-import { NewOrgForm } from 'components/interfaces/Organization'
import { useTheme } from 'common'
+import { observer } from 'mobx-react-lite'
import { useRouter } from 'next/router'
+import { useCallback, useEffect, useState } from 'react'
+
+import { NewOrgForm } from 'components/interfaces/Organization'
+import { WizardLayout } from 'components/layouts'
+import { useSetupIntent } from 'data/stripe/setup-intent-mutation'
+import { useFlag, useStore } from 'hooks'
+import { STRIPE_PUBLIC_KEY } from 'lib/constants'
+import { useIsHCaptchaLoaded } from 'stores/hcaptcha-loaded-store'
+import { NextPageWithLayout } from 'types'
const stripePromise = loadStripe(STRIPE_PUBLIC_KEY)
@@ -22,16 +22,16 @@ const stripePromise = loadStripe(STRIPE_PUBLIC_KEY)
const Wizard: NextPageWithLayout = () => {
const { ui } = useStore()
const router = useRouter()
+ const { isDarkMode } = useTheme()
const [intent, setIntent] = useState<any>()
const captchaLoaded = useIsHCaptchaLoaded()
+ const orgCreationV2 = useFlag('orgcreationv2')
const [captchaToken, setCaptchaToken] = useState<string | null>(null)
const [captchaRef, setCaptchaRef] = useState<HCaptcha | null>(null)
- const orgCreationV2 = useFlag('orgcreationv2')
-
- const { isDarkMode } = useTheme()
+ const { mutate: setupIntent } = useSetupIntent({ onSuccess: (res) => setIntent(res) })
const captchaRefCallback = useCallback((node) => {
setCaptchaRef(node)
@@ -43,23 +43,12 @@ const Wizard: NextPageWithLayout = () => {
}
}, [orgCreationV2])
- const setupIntent = async (hcaptchaToken: string | undefined) => {
+ const initSetupIntent = async (hcaptchaToken: string | undefined) => {
+ if (!hcaptchaToken) return console.error('Hcaptcha token is required')
+
// Force a reload of Elements, necessary for Stripe
setIntent(undefined)
-
- const intent = await post(`${API_URL}/stripe/setup-intent`, {
- hcaptchaToken,
- })
-
- if (intent.error) {
- return ui.setNotification({
- category: 'error',
- message: intent.error.message,
- error: `Failed to retrieve intent: ${intent.error}`,
- })
- } else {
- setIntent(intent)
- }
+ setupIntent({ hcaptchaToken })
}
const options = {
@@ -80,7 +69,7 @@ const Wizard: NextPageWithLayout = () => {
return
}
- await setupIntent(token ?? undefined)
+ await initSetupIntent(token ?? undefined)
resetCaptcha()
}
}