Skip to content

Commit c87e1c8

Browse files
ClemsazertSimonClo
authored andcommitted
✨ create table when instanciating db block
1 parent 9059aef commit c87e1c8

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed
Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,51 @@
1+
import { Button } from '@openfun/cunningham-react';
12
import React from 'react';
23

34
import { Box, Text } from '@/components';
4-
import { Button } from '@openfun/cunningham-react';
55
import { DatabaseSourceSelector } from '@/docs/doc-editor/components/DatabaseSourceSelector';
6+
import { useGristCreateDocAndTable } from '@/features/grist/useGristCreateTable';
7+
8+
import { useDocStore } from '../../doc-management';
69

710
type DatabaseSelectorProps = {
811
onDatabaseSelected: (args: { documentId: string; tableId: string }) => void;
912
};
1013

1114
export const DatabaseSelector = ({
1215
onDatabaseSelected,
13-
}: DatabaseSelectorProps) => (
14-
<Box
15-
style={{
16-
flexDirection: 'column',
17-
gap: 10,
18-
alignItems: 'center',
19-
justifyContent: 'center',
20-
width: '100%',
21-
}}
22-
>
23-
<Button>Créer une nouvelle base de données vide</Button>
24-
<Text>ou</Text>
25-
<DatabaseSourceSelector onSourceSelected={onDatabaseSelected} />
26-
</Box>
27-
);
16+
}: DatabaseSelectorProps) => {
17+
const { createTable } = useGristCreateDocAndTable();
18+
const { currentDoc } = useDocStore();
19+
20+
const handleCreateNewDatabase = () => {
21+
if (!currentDoc) {
22+
console.error('No current document found to create a new database.');
23+
return;
24+
}
25+
createTable(currentDoc.title ?? currentDoc.id)
26+
.then(({ documentId, tableId }) => {
27+
onDatabaseSelected({ documentId, tableId });
28+
})
29+
.catch((error) => {
30+
console.error('Error creating new database:', error);
31+
});
32+
};
33+
34+
return (
35+
<Box
36+
style={{
37+
flexDirection: 'column',
38+
gap: 10,
39+
alignItems: 'center',
40+
justifyContent: 'center',
41+
width: '100%',
42+
}}
43+
>
44+
<Button onClick={handleCreateNewDatabase}>
45+
Créer une nouvelle base de données vide
46+
</Button>
47+
<Text>ou</Text>
48+
<DatabaseSourceSelector onSourceSelected={onDatabaseSelected} />
49+
</Box>
50+
);
51+
};

0 commit comments

Comments
 (0)