-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathuse-active-table-update.ts
More file actions
55 lines (47 loc) · 1.57 KB
/
use-active-table-update.ts
File metadata and controls
55 lines (47 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { useMutation } from "@tanstack/react-query";
import type {
AdapterUpdateDetails,
AdapterUpdateOptions,
} from "../../data/adapter";
import { useActiveTableRowsCollection } from "./use-active-table-rows-collection";
import { useFiltering } from "./use-filtering";
import { usePagination } from "./use-pagination";
import { useSorting } from "./use-sorting";
export interface UseActiveTableUpdateParams {
details: AdapterUpdateDetails;
options: AdapterUpdateOptions;
}
export function useActiveTableUpdate() {
const { paginationState } = usePagination();
const { sortingState } = useSorting();
const { appliedFilter } = useFiltering();
const { activeTable, collection } = useActiveTableRowsCollection({
pageIndex: paginationState.pageIndex,
pageSize: paginationState.pageSize,
sortOrder: sortingState,
filter: appliedFilter,
});
const queryKeyPrefix = [
"schema",
activeTable?.schema ?? null,
"table",
activeTable?.name ?? null,
] as const;
return useMutation({
mutationFn: async (params: UseActiveTableUpdateParams) => {
const rowId = String(params.details.row.__ps_rowid ?? "");
if (!collection || !activeTable || !rowId) {
throw new Error("Active table collection is not available");
}
const transaction = collection.update(rowId, (draft) => {
Object.assign(draft, params.details.changes);
});
await transaction.isPersisted.promise;
return {
row: collection.get(rowId),
};
},
mutationKey: [...queryKeyPrefix, "update"],
retry: false,
});
}