Skip to content

Commit 6afb6e1

Browse files
anaisbergSimonClo
authored andcommitted
✨ create row in grist
1 parent 4740c31 commit 6afb6e1

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-blocks/DatabaseBlock/AddRowButton.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import { Button } from '@openfun/cunningham-react';
22
import { Dispatch, SetStateAction } from 'react';
33

4+
import { useGristCrudRecords } from '@/features/grist/useGristCrudRecords';
5+
46
import { DatabaseRow } from './types';
57
import { createNewRow } from './utils';
68

79
export const AddRowButton = ({
810
columns,
911
setRowData,
12+
documentId,
13+
tableId,
1014
}: {
15+
documentId: string;
16+
tableId: string;
1117
columns: string[];
1218
setRowData: Dispatch<SetStateAction<DatabaseRow[] | undefined>>;
1319
}) => {
20+
const { createRecords } = useGristCrudRecords();
21+
1422
const addRow = () => {
1523
const newRow = createNewRow({ columnNames: columns });
1624
setRowData((prev: DatabaseRow[] | undefined) => {
@@ -22,6 +30,8 @@ export const AddRowButton = ({
2230
updatedRows.splice(updatedRows.length - 1, 0, newRow);
2331
return updatedRows;
2432
});
33+
34+
void createRecords(documentId, tableId, [{ fields: newRow }]);
2535
};
2636

2737
const color = '#817E77';

src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-blocks/DatabaseBlock/DatabaseGrid.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ export const DatabaseGrid = ({
6565
newRowColSpan(params, columnNames.length + 1),
6666
cellRendererSelector: (
6767
params: ICellRendererParams<Record<string, string>>,
68-
) => addRowCellRenderer(params, columnNames, setRowData),
68+
) =>
69+
addRowCellRenderer(params, columnNames, setRowData, {
70+
documentId,
71+
tableId,
72+
}),
6973
}));
7074

7175
setColDefs(columns);
@@ -95,7 +99,11 @@ export const DatabaseGrid = ({
9599
newRowColSpan(params, columnNames.length + 1),
96100
cellRendererSelector: (
97101
params: ICellRendererParams<Record<string, string>>,
98-
) => addRowCellRenderer(params, columnNames, setRowData),
102+
) =>
103+
addRowCellRenderer(params, columnNames, setRowData, {
104+
documentId,
105+
tableId,
106+
}),
99107
};
100108

101109
setColDefs((prev) => {

src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-blocks/DatabaseBlock/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ export const addRowCellRenderer = (
4444
params: ICellRendererParams<Record<string, string>>,
4545
columns: string[] | undefined,
4646
setRowData: Dispatch<SetStateAction<DatabaseRow[] | undefined>>,
47+
{
48+
documentId,
49+
tableId,
50+
}: {
51+
documentId: string;
52+
tableId: string;
53+
},
4754
) => {
4855
if (params.data) {
4956
const addRowButton = {
5057
component: AddRowButton,
51-
params: { columns, setRowData },
58+
params: { columns, setRowData, documentId, tableId },
5259
};
5360
if (Object.values(params.data)[0] === ADD_NEW_ROW) {
5461
return addRowButton;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const useGristCrudRecords = () => {
1313
headers: {
1414
'Content-Type': 'application/json',
1515
},
16-
body: JSON.stringify(records),
16+
body: JSON.stringify({ records }),
1717
});
1818

1919
if (!response.ok) {

0 commit comments

Comments
 (0)