Files
supabase/apps/studio/data/auth/user-update-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

83 lines
2.3 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { useFlag } from 'hooks/ui/useFlag'
import { put } from 'lib/common/fetch'
import type { ResponseError } from 'types'
import { authKeys } from './keys'
export type UserUpdateVariables = {
projectRef?: string
protocol: string
endpoint: string
serviceApiKey: 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({
protocol,
endpoint,
serviceApiKey,
userId,
banDuration,
}: UserUpdateVariables) {
// [Joshen] This is probably the only endpoint that needs the put method from lib/common/fetch
// as it's not our internal API.
const response = await put(
`${protocol}://${endpoint}/auth/v1/admin/users/${userId}`,
{ ban_duration: typeof banDuration === 'number' ? `${banDuration}h` : banDuration },
{
headers: {
apikey: serviceApiKey,
Authorization: `Bearer ${serviceApiKey}`,
},
credentials: undefined,
}
)
if (response.error) throw response.error
return response
}
type UserUpdateData = Awaited<ReturnType<typeof updateUser>>
export const useUserUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<UserUpdateData, ResponseError, UserUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
const userManagementV2 = useFlag('userManagementV2')
return useMutation<UserUpdateData, ResponseError, UserUpdateVariables>(
(vars) => updateUser(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
if (userManagementV2) {
await queryClient.invalidateQueries(authKeys.usersInfinite(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 update user: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}