|
| 1 | +import { Button } from '@openfun/cunningham-react'; |
1 | 2 | import React from 'react';
|
2 | 3 |
|
3 | 4 | import { Box, Text } from '@/components';
|
4 |
| -import { Button } from '@openfun/cunningham-react'; |
5 | 5 | import { DatabaseSourceSelector } from '@/docs/doc-editor/components/DatabaseSourceSelector';
|
| 6 | +import { useGristCreateDocAndTable } from '@/features/grist/useGristCreateTable'; |
| 7 | + |
| 8 | +import { useDocStore } from '../../doc-management'; |
6 | 9 |
|
7 | 10 | type DatabaseSelectorProps = {
|
8 | 11 | onDatabaseSelected: (args: { documentId: string; tableId: string }) => void;
|
9 | 12 | };
|
10 | 13 |
|
11 | 14 | export const DatabaseSelector = ({
|
12 | 15 | 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