Files
supabase/apps/studio/data/tables/table-update-mutation.ts
T
aa90e578ca Chore/show unlock icon next to view entities (#23238)
* Show unlock icon next to views and foreign tables

* Temp header actions for all entities

* Add warnings for views and foreign tables

* Add labels for each entity type

* Cleanup

* Unneeded comma

* Remove unneeded useEffect

* Check lints on the entities menu too

* Pass exposed schemas to lint query

* Type cleanup

* Update

* Update lint, add 0016

* Fix materialized view logic

* Cleanup

* Grab lint count

* Update apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx

Co-authored-by: Inian <inian1234@gmail.com>

* Update apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx

Co-authored-by: Inian <inian1234@gmail.com>

* Update apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx

Co-authored-by: Inian <inian1234@gmail.com>

* Update apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx

Co-authored-by: Inian <inian1234@gmail.com>

* Language changes

* Use lints for gridheaderactions

* Types cleanup

---------

Co-authored-by: Inian <inian1234@gmail.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2024-05-23 16:52:03 -02:30

78 lines
2.1 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-hot-toast'
import type { components } from 'data/api'
import { handleError, patch } from 'data/fetchers'
import type { ResponseError } from 'types'
import { tableKeys } from './keys'
import { lintKeys } from 'data/lint/keys'
export type UpdateTableBody = components['schemas']['UpdateTableBody']
export type TableUpdateVariables = {
projectRef: string
connectionString?: string
id: number
schema: string
payload: UpdateTableBody
}
export async function updateTable({
projectRef,
connectionString,
id,
payload,
}: TableUpdateVariables) {
let headers = new Headers()
if (connectionString) headers.set('x-connection-encrypted', connectionString)
const { data, error } = await patch('/platform/pg-meta/{ref}/tables', {
params: {
header: { 'x-connection-encrypted': connectionString! },
path: { ref: projectRef },
query: { id },
},
body: payload,
headers,
})
if (error) handleError(error)
return data
}
type TableUpdateData = Awaited<ReturnType<typeof updateTable>>
export const useTableUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<TableUpdateData, ResponseError, TableUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<TableUpdateData, ResponseError, TableUpdateVariables>(
(vars) => updateTable(vars),
{
async onSuccess(data, variables, context) {
const { projectRef, schema, id } = variables
await Promise.all([
queryClient.invalidateQueries(tableKeys.list(projectRef, schema)),
queryClient.invalidateQueries(tableKeys.table(projectRef, id)),
queryClient.invalidateQueries(lintKeys.lint(projectRef)),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update database table: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}