Files
supabase/apps/studio/data/auth/user-delete-mutation.ts
T
Joshen LimandAlaister Young 536dc37d58 Chore/user management v2 panel part 2 (#29515)
* Set up banning and unbanning user, although untested due to API CORs issue

* Update search filter UI for users management V2

* Update API types

* Minoir

* Small fix

* Update UI

* Add support for resizing and re-ordering columns

* Add google profile image url to csp

* Revert test button

* Implement toggling of columns

* Fix loading

* Fully implement banning/unbanning user

* Fix

* Update apps/studio/components/interfaces/Auth/Users/UserOverview.tsx

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>

* Fallback non CSP supported avatar urls to user icon

* Fix some bugs

* Remove prism-react-renderer from studio, add to ui patterns

* Migrate users query from API to studio

* Address some feedback

---------

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
2024-10-01 14:32:46 +08:00

68 lines
1.9 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { del, handleError } from 'data/fetchers'
import { sqlKeys } from 'data/sql/keys'
import { useFlag } from 'hooks/ui/useFlag'
import type { ResponseError } from 'types'
import { authKeys } from './keys'
import type { User } from './users-query'
export type UserDeleteVariables = {
projectRef: string
user: User
}
export async function deleteUser({ projectRef, user }: UserDeleteVariables) {
const { data, error } = await del('/platform/auth/{ref}/users', {
params: { path: { ref: projectRef } },
body: user,
})
if (error) handleError(error)
return data
}
type UserDeleteData = Awaited<ReturnType<typeof deleteUser>>
export const useUserDeleteMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<UserDeleteData, ResponseError, UserDeleteVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
const userManagementV2 = useFlag('userManagementV2')
return useMutation<UserDeleteData, ResponseError, UserDeleteVariables>(
(vars) => deleteUser(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
if (userManagementV2) {
await Promise.all([
queryClient.invalidateQueries(authKeys.usersInfinite(projectRef)),
queryClient.invalidateQueries(
sqlKeys.query(projectRef, authKeys.usersCount(projectRef))
),
])
} else {
await queryClient.invalidateQueries(authKeys.users(projectRef))
}
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to delete user: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}