Files
supabase/apps/studio/data/profile/profile-create-mutation.ts
T
Joshen Lim 669a96bed8 Chore/deprecate lib common fetch (#36503)
* Deprecate use of getWithTimeout, refactor BuildingState and RestoringState to use RQ

* Refactor profile-create-mutation to use data/fetchers, and edge-function-status-query to use fetch

* Shift post from lib/common/fetch, refactor bucket-object-download-mutation

* Address feedback

* Minor fix

* Smol fix
2025-06-18 18:14:16 +08:00

48 lines
1.5 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { components } from 'api-types'
import { handleError, post } from 'data/fetchers'
import { organizationKeys } from 'data/organizations/keys'
import { permissionKeys } from 'data/permissions/keys'
import type { ResponseError } from 'types'
import { profileKeys } from './keys'
export type ProfileResponse = components['schemas']['ProfileResponse']
export async function createProfile() {
const { data, error } = await post('/platform/profile')
if (error) handleError(error)
return data
}
type ProfileCreateData = Awaited<ReturnType<typeof createProfile>>
export const useProfileCreateMutation = ({
onSuccess,
onError,
...options
}: Omit<UseMutationOptions<ProfileCreateData, ResponseError, void>, 'mutationFn'> = {}) => {
const queryClient = useQueryClient()
return useMutation<ProfileCreateData, ResponseError, void>(() => createProfile(), {
async onSuccess(data, variables, context) {
await Promise.all([
queryClient.invalidateQueries(profileKeys.profile()),
queryClient.invalidateQueries(organizationKeys.list()),
queryClient.invalidateQueries(permissionKeys.list()),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to create profile: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}