Skip to content

Commit 85cc506

Browse files
ClemsazertSimonClo
authored andcommitted
✨ create & delete columns
1 parent b405d01 commit 85cc506

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/frontend/apps/impress/src/features/grist/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './useGristTableData';
22
export * from './useListGristDocs';
33
export * from './useListGristTables';
44
export * from './useGristCrudRecords';
5+
export * from './useGristCrudColumns';
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
};

0 commit comments

Comments
 (0)