The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Convert create organization to RQ mutation
Joshen Limfa1620674b2d
diff --git a/studio/data/organizations/organization-create-mutation.ts b/studio/data/organizations/organization-create-mutation.tsnew file mode 100644index 00000000000..7e1fb5fe28d--- /dev/null+++ b/studio/data/organizations/organization-create-mutation.ts@@ -0,0 +1,53 @@+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'+import { toast } from 'react-hot-toast'++import { post } from 'data/fetchers'+import { ResponseError } from 'types'+import { organizationKeys } from './keys'++export type OrganizationCreateVariables = {+ name: string+ kind: string+ size?: string+}++export async function createOrganization({ name, kind, size }: OrganizationCreateVariables) {+ const { data, error } = await post('/platform/organizations', {+ // @ts-ignore [Joshen] Generated API spec is wrong?+ body: { name, kind, ...(kind == 'COMPANY' ? { size } : {}) },+ })++ if (error) throw error+ return data+}++type OrganizationCreateData = Awaited<ReturnType<typeof createOrganization>>++export const useOrganizationCreateMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<OrganizationCreateData, ResponseError, OrganizationCreateVariables>,+ 'mutationFn'+> = {}) => {+ const queryClient = useQueryClient()++ return useMutation<OrganizationCreateData, ResponseError, OrganizationCreateVariables>(+ (vars) => createOrganization(vars),+ {+ async onSuccess(data, variables, context) {+ await queryClient.invalidateQueries(organizationKeys.list())+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to create new organization: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}diff --git a/studio/pages/new/index.tsx b/studio/pages/new/index.tsxindex f89c1d4883f..be52ba18366 100644--- a/studio/pages/new/index.tsx+++ b/studio/pages/new/index.tsx@@ -1,4 +1,3 @@-import { useQueryClient } from '@tanstack/react-query'import { observer } from 'mobx-react-lite'import { useRouter } from 'next/router'import { useState } from 'react'@@ -6,10 +5,8 @@ import { Button, Input, Listbox } from 'ui'import { WizardLayout } from 'components/layouts'import Panel from 'components/ui/Panel'-import { invalidateOrganizationsQuery } from 'data/organizations/organizations-query'+import { useOrganizationCreateMutation } from 'data/organizations/organization-create-mutation'import { useStore } from 'hooks'-import { post } from 'lib/common/fetch'-import { API_URL } from 'lib/constants'import { NextPageWithLayout } from 'types'const ORG_KIND_TYPES = {@@ -35,14 +32,19 @@ const ORG_SIZE_DEFAULT = '1'* No org selected yet, create a new one*/const Wizard: NextPageWithLayout = () => {- const queryClient = useQueryClient()const { ui } = useStore()const router = useRouter()const [orgName, setOrgName] = useState('')const [orgKind, setOrgKind] = useState(ORG_KIND_DEFAULT)const [orgSize, setOrgSize] = useState(ORG_SIZE_DEFAULT)- const [newOrgLoading, setNewOrgLoading] = useState(false)++ const { mutate: createOrganization, isLoading: newOrgLoading } = useOrganizationCreateMutation({+ onSuccess: async (org: any) => {+ // [Joshen] API spec is wrong? its returning org type as only having id and name+ router.push(`/new/${org.slug}`)+ },+ })function validateOrgName(name: any) {return name.length >= 1@@ -65,29 +67,14 @@ const Wizard: NextPageWithLayout = () => {const trimmedOrgName = orgName ? orgName.trim() : ''const isOrgNameValid = validateOrgName(trimmedOrgName)if (!isOrgNameValid) {- ui.setNotification({ category: 'error', message: 'Organization name is empty' })- return+ return ui.setNotification({ category: 'error', message: 'Organization name is empty' })}- setNewOrgLoading(true)- const response = await post(`${API_URL}/organizations`, {+ createOrganization({name: trimmedOrgName,kind: orgKind,...(orgKind == 'COMPANY' ? { size: orgSize } : {}),})-- if (response.error) {- setNewOrgLoading(false)- ui.setNotification({- error: response.error,- category: 'error',- message: `Failed to create organization: ${response.error?.message ?? response.error}`,- })- } else {- const org = response- await invalidateOrganizationsQuery(queryClient)- router.push(`/new/${org.slug}`)- }}return (