Skip to content

Commit

Permalink
fix: Fix user / usergroup delete/soft delete
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveGT96 committed Nov 12, 2024
1 parent 89e6419 commit 55f5bc6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ export const UserGroupsTable = ({ headerActions, onEdit }: IOwnProps) => {
dispatch(deleteUserGroup(row.code));
};

const handleRestore = (row: UserGroupDTO) => {
dispatch(updateUserGroup({ ...row, deleted: false }));
};
const handleUpdate = useCallback(
(deleted: boolean) => (row: UserGroupDTO) => {
dispatch(updateUserGroup({ ...row, deleted }));
},
[updateUserGroup]
);

const header = ["code", "desc", "deleted"];

Expand Down Expand Up @@ -87,7 +90,11 @@ export const UserGroupsTable = ({ headerActions, onEdit }: IOwnProps) => {

const displayRowAction = useCallback(
(row: UserGroupDTO, action: TActions) =>
action === "restore" ? !!row.deleted : true,
action === "restore"
? !!row.deleted
: action === "softDelete"
? !row.deleted
: true,
[]
);

Expand Down Expand Up @@ -132,7 +139,8 @@ export const UserGroupsTable = ({ headerActions, onEdit }: IOwnProps) => {
rawData={data}
rowKey="userName"
headerActions={headerActions}
onRestore={canUpdate ? handleRestore : undefined}
onRestore={canUpdate ? handleUpdate(false) : undefined}
onSoftDelete={canUpdate ? handleUpdate(true) : undefined}
displayRowAction={displayRowAction}
labels={{
delete: { message: t("user.confirmUserGroupDeletion") },
Expand Down
32 changes: 20 additions & 12 deletions src/components/accessories/admin/users/usersTable/UsersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,27 @@ export const UsersTable = ({ headerActions, onEdit, onDelete }: IOwnProps) => {
});
};

const handleRestore = (row: UserDTO) => {
dispatch(
updateUser({
...row,
userGroupName: data!.find((user) => user.userName === row.userName)!
.userGroupName,
deleted: false,
})
);
};
const handleUpdate = useCallback(
(deleted: boolean) => (row: UserDTO) => {
dispatch(
updateUser({
...row,
userGroupName: data!.find((user) => user.userName === row.userName)!
.userGroupName,
deleted,
})
);
},
[updateUser, data]
);

const displayRowAction = useCallback(
(row: UserDTO, action: TActions) =>
action === "restore" ? !!row.deleted : true,
action === "restore"
? !!row.deleted
: action === "softDelete"
? !row.deleted
: true,
[]
);

Expand Down Expand Up @@ -169,7 +176,8 @@ export const UsersTable = ({ headerActions, onEdit, onDelete }: IOwnProps) => {
headerActions={headerActions}
onEdit={canUpdate ? onEdit : undefined}
onDelete={canDelete ? onDelete : undefined}
onRestore={canUpdate ? handleRestore : undefined}
onRestore={canUpdate ? handleUpdate(false) : undefined}
onSoftDelete={canUpdate ? handleUpdate(true) : undefined}
labels={{
delete: { message: t("user.confirmUserDeletion") },
}}
Expand Down
53 changes: 52 additions & 1 deletion src/components/accessories/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Close,
Delete,
Edit,
HighlightOff,
InfoOutlined,
MonetizationOn,
Print,
Expand Down Expand Up @@ -60,6 +61,7 @@ const Table: FunctionComponent<IProps> = ({
onView,
onAdd,
onRestore,
onSoftDelete,
addTitle,
showEmptyCell = true,
renderItemDetails,
Expand Down Expand Up @@ -229,6 +231,20 @@ const Table: FunctionComponent<IProps> = ({
<Restore />
</IconButton>
);
case "softDelete":
return (
<IconButton
data-cy="table-softDelete-action"
size="small"
disabled={disableAction(row, "softDelete")}
title={labels?.softDelete?.tooltip ?? t("common.softDelete")}
onClick={handleOpenConfirmation(row, "softDelete")}
>
<HighlightOff
color={disableAction(row, "softDelete") ? "inherit" : "primary"}
/>
</IconButton>
);
}
};

Expand All @@ -245,7 +261,15 @@ const Table: FunctionComponent<IProps> = ({
}, []);

const renderActions = (row: any) => {
if (onEdit || onDelete || onPrint || onView || onCancel) {
if (
onEdit ||
onDelete ||
onPrint ||
onView ||
onCancel ||
onRestore ||
onSoftDelete
) {
return (
<TableCell
scope="row"
Expand Down Expand Up @@ -283,6 +307,10 @@ const Table: FunctionComponent<IProps> = ({
(displayRowAction ? displayRowAction(row, "restore") : true)
? renderIcon("restore", row)
: ""}
{onSoftDelete &&
(displayRowAction ? displayRowAction(row, "softDelete") : true)
? renderIcon("softDelete", row)
: ""}
</TableCell>
);
}
Expand All @@ -302,6 +330,11 @@ const Table: FunctionComponent<IProps> = ({
closeConfirmationDialog();
};

const handleSoftDelete = () => {
if (onSoftDelete) onSoftDelete(currentRow);
closeConfirmationDialog();
};

const handleExpand = () => {
setExpanded(!expanded);
};
Expand Down Expand Up @@ -450,6 +483,24 @@ const Table: FunctionComponent<IProps> = ({
handleSecondaryButtonClick={closeConfirmationDialog}
/>

<ConfirmationDialog
isOpen={
openConfirmation.open && openConfirmation.action === "softDelete"
}
title={labels?.softDelete?.title ?? t("common.softDelete")}
info={
labels?.softDelete?.message ??
t("common.softDeleteConfirmation", {
code: currentRow.code,
})
}
icon={warningIcon}
primaryButtonLabel={t("common.ok")}
secondaryButtonLabel={t("common.discard")}
handlePrimaryButtonClick={handleSoftDelete}
handleSecondaryButtonClick={closeConfirmationDialog}
/>

<ConfirmationDialog
isOpen={openConfirmation.open && openConfirmation.action === "restore"}
title={labels?.restore?.title ?? t("common.restore")}
Expand Down
4 changes: 3 additions & 1 deletion src/components/accessories/table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface IProps {
onView?: (row: any) => void;
onAdd?: (row: any) => void;
onRestore?: (row: any) => void;
onSoftDelete?: (row: any) => void;
addTitle?: string;
showEmptyCell?: boolean;
rowClassNames?: <T>(row: T) => string;
Expand Down Expand Up @@ -90,4 +91,5 @@ export type TActions =
| "close"
| "cancel"
| "add"
| "restore";
| "restore"
| "softDelete";
4 changes: 3 additions & 1 deletion src/resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,9 @@
"option": "Option",
"deleted": "Deleted",
"restore": "Restore",
"restoreConfirmation": "Are you sure to restore this item ?"
"restoreConfirmation": "Are you sure to restore this item ?",
"softDelete": "Mark as deleted",
"softDeleteConfirmation": "Are you sure to mark this item as deleted ?"
},
"permission": {
"denied": "Permission denied",
Expand Down

0 comments on commit 55f5bc6

Please sign in to comment.