Skip to content

Commit ac9582d

Browse files
committed
feat(frontend): make edited table changes persist
1 parent b7b34c2 commit ac9582d

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

frontend/src/components/input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const InputWithlabel: FC<InputWithLabelProps> = ({ value, setValue, label
5353
<div className="relative">
5454
<Input type={inputType} value={value} setValue={setValue} inputProps={inputProps} placeholder={placeholder} />
5555
{type === "password" && cloneElement(hide ? Icons.Show : Icons.Hide, {
56-
className: "w-4 h-4 absolute right-2 top-1/2 -translate-y-1/2 cursor-pointer transition-all hover:scale-110",
56+
className: "w-4 h-4 absolute right-2 top-1/2 -translate-y-1/2 cursor-pointer transition-all hover:scale-110 dark:stroke-neutral-300",
5757
onClick: handleShow,
5858
})}
5959
</div>

frontend/src/components/table.tsx

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useExportToCSV, useLongPress } from "./hooks";
1212
import { Icons } from "./icons";
1313
import { SearchInput } from "./search";
1414
import { Loading } from "./loading";
15+
import { clone } from "lodash";
1516

1617
type IPaginationProps = {
1718
pageCount: number;
@@ -169,6 +170,10 @@ const TData: FC<ITDataProps> = ({ cell, onCellUpdate, disableEdit }) => {
169170
}
170171
}, [editedData]);
171172

173+
useEffect(() => {
174+
setEditedData(cell.value);
175+
}, [cell.value]);
176+
172177
return <div ref={cellRef} {...cell.getCellProps()}
173178
className={classNames("relative group/data cursor-pointer transition-all text-xs table-cell border-t border-l last:border-r group-last/row:border-b group-last/row:first:rounded-bl-lg group-last/row:last:rounded-br-lg border-gray-200 dark:border-white/5 p-0", {
174179
"bg-gray-200 dark:bg-white/10 blur-[2px]": editable || preview,
@@ -281,9 +286,6 @@ const TableRow: FC<ITableRow> = ({ row, style, onRowUpdate, disableEdit }) => {
281286
return Promise.reject();
282287
}
283288
const updatedRow = row.cells.reduce((all, one) => {
284-
if (one.column.id === "#") {
285-
return all;
286-
}
287289
all[one.column.id] = one.value;
288290
return all;
289291
}, {} as Record<string, string>);
@@ -324,6 +326,7 @@ export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows
324326
const [searchIndex, setSearchIndex] = useState(0);
325327
const [height, setHeight] = useState(0);
326328
const [width, setWidth] = useState(0);
329+
const [data, setData] = useState<Record<string, string>[]>([]);
327330

328331
const columns = useMemo(() => {
329332
let colWidth = 150;
@@ -345,13 +348,13 @@ export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows
345348
return cols;
346349
}, [actualColumns, width]);
347350

348-
const data = useMemo(() => {
349-
return actualRows.map((row, rowIndex) => {
351+
useEffect(() => {
352+
setData(actualRows.map((row, rowIndex) => {
350353
return row.reduce((all, one, colIndex) => {
351354
all[actualColumns[colIndex]] = one;
352355
return all;
353356
}, { "#": (rowIndex+1).toString() } as Record<string, string>);
354-
});
357+
}));
355358
}, [actualColumns, actualRows]);
356359

357360
const sortedRows = useMemo(() => {
@@ -463,11 +466,24 @@ export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows
463466
setDirection("dsc");
464467
}, [sortedColumn, direction]);
465468

469+
const handleRowUpdate = useCallback((index: number, row: Record<string, string>) => {
470+
if (onRowUpdate == null) {
471+
return Promise.resolve();
472+
}
473+
setData(value => {
474+
const newValue = clone(value);
475+
newValue[index] = clone(row);
476+
return newValue;
477+
});
478+
delete row["#"];
479+
return onRowUpdate(row);
480+
}, [onRowUpdate]);
481+
466482
const handleRenderRow = useCallback(({ index, style }: ListChildComponentProps) => {
467483
const row = rows[index];
468484
prepareRow(row);
469-
return <TableRow key={`row-${row.values[actualColumns[0]]}`} row={row} style={style} onRowUpdate={onRowUpdate} disableEdit={disableEdit} />;
470-
}, [rows, prepareRow, actualColumns, onRowUpdate, disableEdit]);
485+
return <TableRow key={`row-${row.values[actualColumns[0]]}`} row={row} style={style} onRowUpdate={(row) => handleRowUpdate(index, row)} disableEdit={disableEdit} />;
486+
}, [rows, prepareRow, actualColumns, handleRowUpdate, disableEdit]);
471487

472488
useEffect(() => {
473489
if (containerRef.current == null || operationsRef.current == null) {

0 commit comments

Comments
 (0)