Files
supabase/apps/studio/data/auth/user-update-mutation.ts
Ivan Vasilov da4a40e308 chore: Migrate RQ functions to use object syntax style (#39895)
* Migrate all uses of invalidateQueries to use object syntax.

* Migrate the remainder of useInfiniteQuery.

* Migrate all setQueriesData.

* Migrate all fetchQuery uses.

* Migrate some leftover functions from RQ.

* Fix issues found by Charis.
2025-10-28 10:43:14 +01:00

54 lines
1.7 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { handleError, patch } from 'data/fetchers'
import type { ResponseError } from 'types'
import { authKeys } from './keys'
export type UserUpdateVariables = {
projectRef: string
userId: string
// For now just support updating banning the user
banDuration: number | 'none' // In hours, "none" to unban, otherwise a string in hours e.g "24h"
}
export async function updateUser({ projectRef, userId, banDuration }: UserUpdateVariables) {
const { data, error } = await patch('/platform/auth/{ref}/users/{id}', {
params: { path: { ref: projectRef, id: userId } },
body: { ban_duration: typeof banDuration === 'number' ? `${banDuration}h` : banDuration },
})
if (error) handleError(error)
return data
}
type UserUpdateData = Awaited<ReturnType<typeof updateUser>>
export const useUserUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<UserUpdateData, ResponseError, UserUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<UserUpdateData, ResponseError, UserUpdateVariables>({
mutationFn: (vars) => updateUser(vars),
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries({ queryKey: authKeys.usersInfinite(projectRef) })
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update user: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}