File tree Expand file tree Collapse file tree 2 files changed +61
-0
lines changed
src/frontend/apps/impress/src/features/grist Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -2,3 +2,4 @@ export * from './useGristTableData';
2
2
export * from './useListGristDocs' ;
3
3
export * from './useListGristTables' ;
4
4
export * from './useGristCrudRecords' ;
5
+ export * from './useGristCrudColumns' ;
Original file line number Diff line number Diff line change
1
+ import { gristFetchApi } from '@/api' ;
2
+
3
+ export const useGristCrudColumns = ( ) => {
4
+ const createColumns = async (
5
+ documentId : string ,
6
+ tableId : string ,
7
+ columns : { id : string ; fields : unknown } [ ] ,
8
+ ) => {
9
+ const url = `docs/${ documentId } /tables/${ tableId } /columns` ;
10
+ try {
11
+ const response = await gristFetchApi ( url , {
12
+ method : 'POST' ,
13
+ headers : {
14
+ 'Content-Type' : 'application/json' ,
15
+ } ,
16
+ body : JSON . stringify ( columns ) ,
17
+ } ) ;
18
+
19
+ if ( ! response . ok ) {
20
+ const errorBody = await response . text ( ) ;
21
+ throw new Error (
22
+ `Failed to create columns: ${ response . status } ${ response . statusText } - ${ errorBody } ` ,
23
+ ) ;
24
+ }
25
+
26
+ return ( await response . json ( ) ) as Promise < { records : { id : string } [ ] } > ;
27
+ } catch ( error ) {
28
+ console . error ( 'Error creating Grist record:' , error ) ;
29
+ throw error ;
30
+ }
31
+ } ;
32
+
33
+ const deleteColumns = async (
34
+ documentId : string ,
35
+ tableId : string ,
36
+ columnId : string ,
37
+ ) => {
38
+ const url = `docs/${ documentId } /tables/${ tableId } /columns/${ columnId } ` ;
39
+ try {
40
+ const response = await gristFetchApi ( url , {
41
+ method : 'DELETE' ,
42
+ headers : {
43
+ 'Content-Type' : 'application/json' ,
44
+ } ,
45
+ } ) ;
46
+
47
+ if ( ! response . ok ) {
48
+ const errorBody = await response . text ( ) ;
49
+ throw new Error (
50
+ `Failed to delete column: ${ response . status } ${ response . statusText } - ${ errorBody } ` ,
51
+ ) ;
52
+ }
53
+ } catch ( error ) {
54
+ console . error ( 'Error deleting Grist column:' , error ) ;
55
+ throw error ;
56
+ }
57
+ } ;
58
+
59
+ return { createColumns, deleteColumns } ;
60
+ } ;
You can’t perform that action at this time.
0 commit comments