Skip to content

Commit

Permalink
fix(clusters-ui): issues with deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril committed Mar 22, 2024
1 parent 13907ba commit 53b4832
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions webapp/public/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"global.error.failedtoretrievejobs": "Failed to retrieve job information",
"global.error.failedtoretrievelogs": "Failed to retrieve job logs",
"global.error.failedtoretrievedownloads": "Failed to retrieve downloads list",
"global.error.deleteFailed": "Unable to delete",
"global.area.add": "Add an area",
"login.error": "Failed to authenticate",
"tasks.title": "Tasks",
Expand Down
1 change: 1 addition & 0 deletions webapp/public/locales/fr/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"global.error.failedtoretrievejobs": "Échec de la récupération des tâches",
"global.error.failedtoretrievelogs": "Échec de la récupération des logs",
"global.error.failedtoretrievedownloads": "Échec de la récupération des exports",
"global.error.deleteFailed": "Impossible d'effectuer la suppression",
"global.area.add": "Ajouter une zone",
"login.error": "Échec de l'authentification",
"tasks.title": "Tâches",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ function Thermal() {
return createThermalCluster(study.id, areaId, cluster);
};

const handleDeleteSelection = (ids: string[]) => {
const handleDeleteSelection = (rows: ThermalClusterWithCapacity[]) => {
const ids = rows.map((row) => row.id);
return deleteThermalClusters(study.id, areaId, ids);
};

Expand Down
33 changes: 22 additions & 11 deletions webapp/src/components/common/GroupedDataTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ import * as R from "ramda";
import * as RA from "ramda-adjunct";
import { usePrevious } from "react-use";
import useUpdateEffectOnce from "../../../hooks/useUpdateEffectOnce";
import { PromiseAny } from "../../../utils/tsUtils";
import useEnqueueErrorSnackbar from "../../../hooks/useEnqueueErrorSnackbar";
import { toError } from "../../../utils/fnUtils";

export interface GroupedDataTableProps<TData extends TRow> {
data: TData[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
columns: Array<MRT_ColumnDef<TData, any>>;
groups: string[] | readonly string[];
onCreate?: (values: TData) => Promise<TData>;
onDelete?: (ids: string[]) => void;
onDelete?: (rows: TData[]) => PromiseAny | void;
onNameClick?: (row: MRT_Row<TData>) => void;
isLoading?: boolean;
deleteConfirmationMessage?: string | ((count: number) => string);
Expand Down Expand Up @@ -60,6 +63,8 @@ function GroupedDataTable<TData extends TRow>({
>("");
const [tableData, setTableData] = useState(data);
const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({});
const [isSaving, setIsSaving] = useState(false);
const enqueueErrorSnackbar = useEnqueueErrorSnackbar();
const callbacksRef = useAutoUpdateRef({ onNameClick });
const prevData = usePrevious(data);

Expand Down Expand Up @@ -126,7 +131,7 @@ function GroupedDataTable<TData extends TRow>({
expanded: true,
columnPinning: { left: [GROUP_COLUMN_ID] },
},
state: { isLoading, rowSelection },
state: { isLoading, isSaving, rowSelection },
enableGrouping: true,
enableStickyFooter: true,
enableStickyHeader: true,
Expand Down Expand Up @@ -231,24 +236,30 @@ function GroupedDataTable<TData extends TRow>({
}
};

const handleDelete = () => {
const handleDelete = async () => {
if (!onDelete) {
return;
}

const rowIndexes = Object.keys(rowSelection)
.map(Number)
// ignore groups names
.filter(Number.isInteger);
const rowsToDelete = selectedRows;

const rowIdsToDelete = rowIndexes.map((index) => tableData[index].id);

onDelete(rowIdsToDelete);
setTableData((prevTableData) =>
prevTableData.filter((row) => !rowIdsToDelete.includes(row.id)),
prevTableData.filter((row) => !rowsToDelete.includes(row)),
);

setRowSelection({});
closeDialog();

setIsSaving(true);

try {
await onDelete(rowsToDelete);
} catch (error) {
enqueueErrorSnackbar(t("global.error.deleteFailed"), toError(error));
setTableData((prevTableData) => [...prevTableData, ...rowsToDelete]);
}

setIsSaving(false);
};

const handleDuplicate = async (name: string) => {
Expand Down

0 comments on commit 53b4832

Please sign in to comment.