Skip to content

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

Use organization create mutation in NewOrgForm

Joshen Limb849a0df7995
diff --git a/studio/components/interfaces/Organization/NewOrg/NewOrgForm.tsx b/studio/components/interfaces/Organization/NewOrg/NewOrgForm.tsx
index 09568fe8846..9bc561e046b 100644
--- a/studio/components/interfaces/Organization/NewOrg/NewOrgForm.tsx
+++ b/studio/components/interfaces/Organization/NewOrg/NewOrgForm.tsx
@@ -1,4 +1,7 @@
+import { PaymentElement, useElements, useStripe } from '@stripe/react-stripe-js'
+import type { PaymentMethod } from '@stripe/stripe-js'
import { useQueryClient } from '@tanstack/react-query'
+import Link from 'next/link'
import { useRouter } from 'next/router'
import { useState } from 'react'
import {
@@ -12,18 +15,14 @@ import {
Toggle,
} from 'ui'
-import { PaymentElement, useElements, useStripe } from '@stripe/react-stripe-js'
-import type { PaymentMethod } from '@stripe/stripe-js'
+import { SpendCapModal } from 'components/interfaces/BillingV2'
import InformationBox from 'components/ui/InformationBox'
import Panel from 'components/ui/Panel'
+import { useOrganizationCreateMutation } from 'data/organizations/organization-create-mutation'
import { invalidateOrganizationsQuery } from 'data/organizations/organizations-query'
import { useStore } from 'hooks'
-import { isResponseOk, post } from 'lib/common/fetch'
-import { API_URL, BASE_PATH, PRICING_TIER_LABELS_ORG } from 'lib/constants'
+import { BASE_PATH, PRICING_TIER_LABELS_ORG } from 'lib/constants'
import { getURL } from 'lib/helpers'
-import Link from 'next/link'
-import { SpendCapModal } from 'components/interfaces/BillingV2'
-import { Organization } from 'types'
const ORG_KIND_TYPES = {
PERSONAL: 'Personal',
@@ -61,6 +60,7 @@ const NewOrgForm = ({ onPaymentMethodReset }: NewOrgFormProps) => {
const [orgName, setOrgName] = useState('')
const [orgKind, setOrgKind] = useState(ORG_KIND_DEFAULT)
const [orgSize, setOrgSize] = useState(ORG_SIZE_DEFAULT)
+ // [Joshen] Separate loading state here as there's 2 async processes
const [newOrgLoading, setNewOrgLoading] = useState(false)
const [paymentMethod, setPaymentMethod] = useState<PaymentMethod>()
@@ -69,6 +69,13 @@ const NewOrgForm = ({ onPaymentMethodReset }: NewOrgFormProps) => {
const [showSpendCapHelperModal, setShowSpendCapHelperModal] = useState(false)
const [isSpendCapEnabled, setIsSpendCapEnabled] = useState(true)
+ const { mutateAsync: createOrganization } = useOrganizationCreateMutation({
+ onSuccess: async (org: any) => {
+ await invalidateOrganizationsQuery(queryClient)
+ router.push(`/new/${org.slug}`)
+ },
+ })
+
function validateOrgName(name: any) {
const value = name ? name.trim() : ''
return value.length >= 1
@@ -92,36 +99,18 @@ const NewOrgForm = ({ onPaymentMethodReset }: NewOrgFormProps) => {
async function createOrg(paymentMethodId?: string) {
const dbTier = dbPricingTierKey === 'PRO' && !isSpendCapEnabled ? 'PAYG' : dbPricingTierKey
-
- const response = await post<Organization>(
- `${API_URL}/organizations`,
- {
+ try {
+ await createOrganization({
name: orgName,
kind: orgKind,
- payment_method: paymentMethodId,
tier: 'tier_' + dbTier.toLowerCase(),
...(orgKind == 'COMPANY' ? { size: orgSize } : {}),
- },
- // Call new V2 endpoint from API
- {
- headers: {
- Version: '2',
- },
- }
- )
-
- if (!isResponseOk(response)) {
- ui.setNotification({
- error: response.error,
- category: 'error',
- message: `Failed to create organization: ${response.error?.message ?? response.error}`,
+ payment_method: paymentMethodId,
+ V2: true,
})
- } else {
- const org = response
- await invalidateOrganizationsQuery(queryClient)
- router.push(`/new/${org.slug}`)
+ } finally {
+ setNewOrgLoading(false)
}
- setNewOrgLoading(false)
}
const handleSubmit = async (event: any) => {
@@ -129,13 +118,11 @@ const NewOrgForm = ({ onPaymentMethodReset }: NewOrgFormProps) => {
const isOrgNameValid = validateOrgName(orgName)
if (!isOrgNameValid) {
- ui.setNotification({ category: 'error', message: 'Organization name is empty' })
- return
+ return ui.setNotification({ category: 'error', message: 'Organization name is empty' })
}
if (!stripe || !elements) {
- console.error('Stripe.js has not loaded')
- return
+ return console.error('Stripe.js has not loaded')
}
setNewOrgLoading(true)
diff --git a/studio/data/organizations/organization-create-mutation.ts b/studio/data/organizations/organization-create-mutation.ts
index d3aa1e97de7..f0594df216f 100644
--- a/studio/data/organizations/organization-create-mutation.ts
+++ b/studio/data/organizations/organization-create-mutation.ts
@@ -9,12 +9,29 @@ export type OrganizationCreateVariables = {
name: string
kind?: string
size?: string
+ tier?: string
+ payment_method?: string
+ V2?: boolean
}
-export async function createOrganization({ name, kind, size }: OrganizationCreateVariables) {
+export async function createOrganization({
+ name,
+ kind,
+ size,
+ tier,
+ payment_method,
+ V2 = false,
+}: OrganizationCreateVariables) {
const { data, error } = await post('/platform/organizations', {
// @ts-ignore [Joshen] Generated API spec is wrong?
- body: { name, kind, ...(kind == 'COMPANY' ? { size } : {}) },
+ body: {
+ name,
+ kind,
+ ...(kind == 'COMPANY' ? { size } : {}),
+ ...(tier !== undefined ? { tier } : {}),
+ ...(payment_method !== undefined ? { payment_method } : {}),
+ },
+ ...(V2 ? { headers: { Version: '2' } } : {}),
})
if (error) throw error