Skip to content

Commit 17a2dba

Browse files
anaisbergSimonClo
authored andcommitted
🐛 remove add row magic string
1 parent c2b9c2f commit 17a2dba

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { AddButtonComponent } from './AddColumnButton';
1313
import { useColumns, useRows } from './hooks';
1414
import { DatabaseRow } from './types';
1515
import {
16+
ADD_NEW_ROW,
1617
addRowCellRenderer,
1718
createNewRow,
1819
getColumnNames,
@@ -72,9 +73,17 @@ export const DatabaseGrid = ({
7273

7374
useEffect(() => {
7475
const columnNames = getColumnNames(colDefs);
75-
const addNewRow = createNewRow({ value: '+ new row', columnNames });
76+
const lastRow = rowData?.[rowData.length - 1];
77+
if (lastRow && Object.values(lastRow).length > 0) {
78+
const lastRowValue = Object.values(lastRow)[0];
79+
if (lastRowValue === ADD_NEW_ROW) {
80+
return;
81+
}
82+
}
83+
const addNewRow = createNewRow({ value: ADD_NEW_ROW, columnNames });
7684
setRowData((prev) => [...(prev ? prev : []), addNewRow]);
77-
}, [colDefs, setRowData]);
85+
// eslint-disable-next-line react-hooks/exhaustive-deps
86+
}, [colDefs]);
7887

7988
const defaultColDef = {
8089
flex: 1,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { Dispatch, SetStateAction } from 'react';
99
import { AddRowButton } from './AddRowButton';
1010
import { DatabaseRow } from './types';
1111

12+
export const ADD_NEW_ROW = 'add-new-row';
13+
1214
export const autoSizeStrategy: SizeColumnsToContentStrategy = {
1315
type: 'fitCellContents',
1416
};
@@ -40,7 +42,7 @@ export const addRowCellRenderer = (
4042
component: AddRowButton,
4143
params: { columns, setRowData },
4244
};
43-
if (Object.values(params.data)[0]?.includes('new')) {
45+
if (Object.values(params.data)[0] === ADD_NEW_ROW) {
4446
return addRowButton;
4547
}
4648
return undefined;
@@ -52,7 +54,7 @@ export const newRowColSpan = (
5254
params: ColSpanParams<Record<string, string>>,
5355
) => {
5456
const colsValues = params.data ?? {};
55-
const isNewRow = Object.values(colsValues)[0]?.includes('new');
57+
const isNewRow = Object.values(colsValues)[0] === ADD_NEW_ROW;
5658
if (isNewRow) {
5759
return Object.keys(colsValues).length;
5860
}

0 commit comments

Comments
 (0)