Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions apps/sim/hooks/queries/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const tableKeys = {
rowsRoot: (tableId: string) => [...tableKeys.detail(tableId), 'rows'] as const,
infiniteRows: (tableId: string, paramsKey: string) =>
[...tableKeys.rowsRoot(tableId), 'infinite', paramsKey] as const,
rowWrites: (tableId: string) => [...tableKeys.rowsRoot(tableId), 'write'] as const,
}

type TableRowsParams = Omit<TableRowsQueryInput, 'filter' | 'sort'> &
Expand Down Expand Up @@ -543,14 +544,15 @@ export function useUpdateTableRow({ workspaceId, tableId }: RowMutationContext)
const queryClient = useQueryClient()

return useMutation({
mutationKey: tableKeys.rowWrites(tableId),
mutationFn: async ({ rowId, data }: UpdateTableRowParams) => {
return requestJson(updateTableRowContract, {
params: { tableId, rowId },
body: { workspaceId, data: data as RowData },
})
},
onMutate: ({ rowId, data }) => {
void queryClient.cancelQueries({ queryKey: tableKeys.rowsRoot(tableId) })
onMutate: async ({ rowId, data }) => {
await queryClient.cancelQueries({ queryKey: tableKeys.rowsRoot(tableId) })

const previousQueries = queryClient.getQueriesData<InfiniteData<TableRowsResponse, number>>({
queryKey: tableKeys.rowsRoot(tableId),
Expand All @@ -573,6 +575,24 @@ export function useUpdateTableRow({ workspaceId, tableId }: RowMutationContext)

return { previousQueries }
},
onSuccess: (response, { rowId, data: mutatedData }) => {
const serverRow = response.data.row
const mutatedKeys = Object.keys(mutatedData)
patchCachedRows(queryClient, tableId, (row) => {
if (row.id !== rowId) return row
const merged: RowData = { ...row.data }
for (const key of mutatedKeys) {
merged[key] = (serverRow.data as RowData)[key]
}
return {
...row,
data: merged,
position: serverRow.position,
createdAt: serverRow.createdAt,
updatedAt: serverRow.updatedAt,
}
})
},
Comment thread
waleedlatif1 marked this conversation as resolved.
onError: (error, _vars, context) => {
if (context?.previousQueries) {
for (const [queryKey, data] of context.previousQueries) {
Expand All @@ -583,7 +603,9 @@ export function useUpdateTableRow({ workspaceId, tableId }: RowMutationContext)
toast.error(error.message, { duration: 5000 })
},
onSettled: () => {
invalidateRowData(queryClient, tableId)
if (queryClient.isMutating({ mutationKey: tableKeys.rowWrites(tableId) }) === 1) {
invalidateRowData(queryClient, tableId)
}
Comment thread
waleedlatif1 marked this conversation as resolved.
Comment thread
waleedlatif1 marked this conversation as resolved.
},
})
}
Expand All @@ -599,6 +621,7 @@ export function useBatchUpdateTableRows({ workspaceId, tableId }: RowMutationCon
const queryClient = useQueryClient()

return useMutation({
mutationKey: tableKeys.rowWrites(tableId),
mutationFn: async ({ updates }: BatchUpdateTableRowsParams) => {
return requestJson(batchUpdateTableRowsContract, {
params: { tableId },
Expand All @@ -608,8 +631,8 @@ export function useBatchUpdateTableRows({ workspaceId, tableId }: RowMutationCon
},
})
},
onMutate: ({ updates }) => {
void queryClient.cancelQueries({ queryKey: tableKeys.rowsRoot(tableId) })
onMutate: async ({ updates }) => {
await queryClient.cancelQueries({ queryKey: tableKeys.rowsRoot(tableId) })

const previousQueries = queryClient.getQueriesData<InfiniteData<TableRowsResponse, number>>({
queryKey: tableKeys.rowsRoot(tableId),
Expand Down Expand Up @@ -644,7 +667,9 @@ export function useBatchUpdateTableRows({ workspaceId, tableId }: RowMutationCon
toast.error(error.message, { duration: 5000 })
},
onSettled: () => {
invalidateRowData(queryClient, tableId)
if (queryClient.isMutating({ mutationKey: tableKeys.rowWrites(tableId) }) === 1) {
invalidateRowData(queryClient, tableId)
}
Comment thread
waleedlatif1 marked this conversation as resolved.
},
})
}
Expand Down
Loading