Skip to content

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

Refactor delete organization to RQ and update DeleteOrganizationButton to use it

Joshen Lim164cf087c273
diff --git a/studio/components/interfaces/Organization/GeneralSettings/DeleteOrganizationButton.tsx b/studio/components/interfaces/Organization/GeneralSettings/DeleteOrganizationButton.tsx
index cc5c66fec0b..c1804f245f3 100644
--- a/studio/components/interfaces/Organization/GeneralSettings/DeleteOrganizationButton.tsx
+++ b/studio/components/interfaces/Organization/GeneralSettings/DeleteOrganizationButton.tsx
@@ -1,17 +1,13 @@
import { PermissionAction } from '@supabase/shared-types/out/constants'
-import { useQueryClient } from '@tanstack/react-query'
import { useRouter } from 'next/router'
import { useState } from 'react'
import { Button, Form, Input, Modal } from 'ui'
-import { invalidateOrganizationsQuery } from 'data/organizations/organizations-query'
+import { useOrganizationDeleteMutation } from 'data/organizations/organization-delete-mutation'
import { useCheckPermissions, useSelectedOrganization, useStore } from 'hooks'
-import { delete_, isResponseOk } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
const DeleteOrganizationButton = () => {
const router = useRouter()
- const queryClient = useQueryClient()
const { ui } = useStore()
const selectedOrganization = useSelectedOrganization()
@@ -21,6 +17,7 @@ const DeleteOrganizationButton = () => {
const [value, setValue] = useState('')
const canDeleteOrganization = useCheckPermissions(PermissionAction.UPDATE, 'organizations')
+ const { mutateAsync: deleteOrganization, isLoading: isDeleting } = useOrganizationDeleteMutation()
const onValidate = (values: any) => {
const errors: any = {}
@@ -33,30 +30,23 @@ const DeleteOrganizationButton = () => {
return errors
}
- const onConfirmDelete = async (values: any, { setSubmitting }: any) => {
+ const onConfirmDelete = async (values: any) => {
if (!canDeleteOrganization) {
return ui.setNotification({
category: 'error',
message: 'You do not have the required permissions to delete this organization',
})
}
+ if (!orgSlug) return console.error('Org slug is required')
- setSubmitting(true)
- const response = await delete_<void>(`${API_URL}/organizations/${orgSlug}`)
- if (!isResponseOk(response)) {
- ui.setNotification({
- category: 'error',
- message: `Failed to delete organization: ${response.error.message}`,
- })
- setSubmitting(false)
- } else {
- invalidateOrganizationsQuery(queryClient)
- setSubmitting(false)
- router.push('/')
+ try {
+ await deleteOrganization({ slug: orgSlug })
+ } finally {
ui.setNotification({
category: 'success',
message: `Successfully deleted ${orgName}`,
})
+ router.push('/')
}
}
@@ -86,7 +76,7 @@ const DeleteOrganizationButton = () => {
onSubmit={onConfirmDelete}
validate={onValidate}
>
- {({ isSubmitting }: { isSubmitting: boolean }) => (
+ {() => (
<div className="space-y-4 py-3">
<Modal.Content>
<p className="text-sm text-scale-900">
@@ -112,7 +102,14 @@ const DeleteOrganizationButton = () => {
</Modal.Content>
<Modal.Separator />
<Modal.Content>
- <Button block size="small" type="danger" htmlType="submit" loading={isSubmitting}>
+ <Button
+ block
+ size="small"
+ type="danger"
+ htmlType="submit"
+ loading={isDeleting}
+ disabled={isDeleting}
+ >
I understand, delete this organization
</Button>
</Modal.Content>
diff --git a/studio/data/organizations/organization-delete-mutation.ts b/studio/data/organizations/organization-delete-mutation.ts
new file mode 100644
index 00000000000..3b0dcfa7e44
--- /dev/null
+++ b/studio/data/organizations/organization-delete-mutation.ts
@@ -0,0 +1,49 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { del } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { organizationKeys } from './keys'
+
+export type OrganizationDeleteVariables = {
+ slug: string
+}
+
+export async function deleteOrganization({ slug }: OrganizationDeleteVariables) {
+ const { data, error } = await del('/platform/organizations/{slug}', {
+ params: { path: { slug } },
+ })
+ if (error) throw error
+ return data
+}
+
+type OrganizationDeleteData = Awaited<ReturnType<typeof deleteOrganization>>
+
+export const useOrganizationDeleteMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<OrganizationDeleteData, ResponseError, OrganizationDeleteVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+
+ return useMutation<OrganizationDeleteData, ResponseError, OrganizationDeleteVariables>(
+ (vars) => deleteOrganization(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 delete organization: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}