Files
supabase/apps/studio/data/subscriptions/project-addon-update-mutation.ts
T
1fb755bfc7 chore: use data/fetchers and API spec (#20196)
* chore: use data/fetchers and API spec

* Remove unused imports

---------

Co-authored-by: Thomas Eckl <31189692+ecktoteckto@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-01-08 14:39:32 +08:00

72 lines
1.9 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-hot-toast'
import { post } from 'data/fetchers'
import { ResponseError } from 'types'
import { subscriptionKeys } from './keys'
import { AddonVariantId, ProjectAddonType } from './types'
export type ProjectAddonUpdateVariables = {
projectRef: string
variant: AddonVariantId
type: ProjectAddonType
}
export async function updateSubscriptionAddon({
projectRef,
variant,
type,
}: ProjectAddonUpdateVariables) {
if (!projectRef) throw new Error('projectRef is required')
if (!variant) throw new Error('variant is required')
if (!type) throw new Error('type is required')
const { data, error } = await post(`/platform/projects/{ref}/billing/addons`, {
params: {
path: {
ref: projectRef,
},
},
body: {
addon_type: type,
addon_variant: variant,
},
})
if (error) throw error
return data
}
type ProjectAddonUpdateData = Awaited<ReturnType<typeof updateSubscriptionAddon>>
export const useProjectAddonUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<ProjectAddonUpdateData, ResponseError, ProjectAddonUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<ProjectAddonUpdateData, ResponseError, ProjectAddonUpdateVariables>(
(vars) => updateSubscriptionAddon(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await Promise.all([queryClient.invalidateQueries(subscriptionKeys.addons(projectRef))])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update addon: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}