Skip to content

Commit f689e38

Browse files
anaisbergSimonClo
authored andcommitted
✨ edit cell in grist
1 parent 64fe874 commit f689e38

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { ColDef, ColSpanParams, ICellRendererParams } from 'ag-grid-community';
1+
import {
2+
CellEditingStoppedEvent,
3+
ColDef,
4+
ColSpanParams,
5+
ICellRendererParams,
6+
} from 'ag-grid-community';
27
import { AgGridReact } from 'ag-grid-react';
3-
import { useEffect } from 'react';
8+
import { useCallback, useEffect } from 'react';
49

510
import { Box } from '@/components';
611
import {
712
ColumnType,
813
useGristCrudColumns,
14+
useGristCrudRecords,
915
useGristTableData,
1016
} from '@/features/grist';
1117

@@ -34,6 +40,7 @@ export const DatabaseGrid = ({
3440
});
3541

3642
const { createColumns } = useGristCrudColumns();
43+
const { updateRecords } = useGristCrudRecords();
3744

3845
const { rowData, setRowData } = useRows();
3946
const { colDefs, setColDefs } = useColumns();
@@ -128,6 +135,28 @@ export const DatabaseGrid = ({
128135
]);
129136
};
130137

138+
const onCellEditingStopped = useCallback(
139+
(event: CellEditingStoppedEvent<DatabaseRow, DatabaseRow>) => {
140+
const { oldValue, newValue, data } = event;
141+
142+
if (data === undefined) {
143+
return;
144+
}
145+
const { id: rowId, ...updatedRow } = data;
146+
147+
if (!(typeof rowId === 'number') || oldValue === newValue) {
148+
return;
149+
}
150+
151+
void updateRecords(documentId, tableId, [
152+
{ id: rowId, fields: updatedRow },
153+
]);
154+
},
155+
// disable updateRecords
156+
// eslint-disable-next-line react-hooks/exhaustive-deps
157+
[documentId, tableId],
158+
);
159+
131160
return (
132161
<>
133162
<Box style={{ height: '100%', width: '100%' }}>
@@ -137,6 +166,7 @@ export const DatabaseGrid = ({
137166
defaultColDef={defaultColDef}
138167
domLayout="autoHeight"
139168
enableCellSpan={true}
169+
onCellEditingStopped={onCellEditingStopped}
140170
/>
141171
</Box>
142172
<AddButtonComponent addColumn={addColumn} />
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
export type DatabaseRow = Record<string, string | number | boolean | undefined>;
1+
type IdColumn = {
2+
id: number;
3+
};
4+
export type DatabaseRow =
5+
| Record<string, string | number | boolean | undefined>
6+
| IdColumn;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const useGristCrudRecords = () => {
6969
headers: {
7070
'Content-Type': 'application/json',
7171
},
72-
body: JSON.stringify(records),
72+
body: JSON.stringify({ records }),
7373
});
7474

7575
if (!response.ok) {

0 commit comments

Comments
 (0)