mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: apply enhancements
This commit is contained in:
@@ -31,6 +31,7 @@ export const DeleteDialog = <T extends any = string>({
|
||||
data: ids,
|
||||
callback,
|
||||
entity = EntityType.ATTACHMENT,
|
||||
setData,
|
||||
onDeleteError = () => {},
|
||||
onDeleteSuccess = () => {},
|
||||
}: DeleteDialogProps<T> & {
|
||||
@@ -39,13 +40,17 @@ export const DeleteDialog = <T extends any = string>({
|
||||
onDeleteSuccess?: (data?: unknown) => void;
|
||||
}) => {
|
||||
const { t } = useTranslate();
|
||||
const { mutateAsync: deleteEntity } = useDelete(entity, {
|
||||
const onSuccess = () => {
|
||||
setData?.(undefined);
|
||||
onDeleteSuccess();
|
||||
};
|
||||
const { mutateAsync: deleteOne } = useDelete(entity, {
|
||||
onError: onDeleteError,
|
||||
onSuccess: onDeleteSuccess,
|
||||
onSuccess,
|
||||
});
|
||||
const { mutateAsync: deleteEntities } = useDeleteMany(entity, {
|
||||
const { mutateAsync: deleteMany } = useDeleteMany(entity, {
|
||||
onError: onDeleteError,
|
||||
onSuccess: onDeleteSuccess,
|
||||
onSuccess,
|
||||
});
|
||||
|
||||
return (
|
||||
@@ -71,14 +76,13 @@ export const DeleteDialog = <T extends any = string>({
|
||||
} else {
|
||||
if (!Array.isArray(ids)) {
|
||||
throw new Error("IDs need to be an Array");
|
||||
}
|
||||
|
||||
if (ids.length === 0) {
|
||||
} else if (ids.length === 0) {
|
||||
throw new Error("IDs cannot be empty");
|
||||
} else if (ids.length === 1) {
|
||||
await deleteOne(ids[0]);
|
||||
} else if (ids.length > 1) {
|
||||
await deleteMany(ids);
|
||||
}
|
||||
|
||||
if (ids.length === 1) await deleteEntity(ids[0]);
|
||||
else if (ids.length > 1) await deleteEntities(ids);
|
||||
}
|
||||
}}
|
||||
autoFocus
|
||||
|
||||
@@ -115,7 +115,7 @@ export const Categories = () => {
|
||||
toast.error(error.message || t("message.internal_server_error"));
|
||||
}}
|
||||
onDeleteSuccess={() => {
|
||||
deleteDialogCtl.closeDialog(undefined, "postDelete");
|
||||
deleteDialogCtl.closeDialog();
|
||||
toast.success(t("message.item_delete_success"));
|
||||
}}
|
||||
/>
|
||||
@@ -144,18 +144,17 @@ export const Categories = () => {
|
||||
</Button>
|
||||
</Grid>
|
||||
) : null}
|
||||
{deleteDialogCtl.data?.length ? (
|
||||
<Grid item>
|
||||
<Button
|
||||
startIcon={<DeleteIcon />}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={() => deleteDialogCtl.openDialog()}
|
||||
>
|
||||
{t("button.delete")}
|
||||
</Button>
|
||||
</Grid>
|
||||
) : null}
|
||||
<Grid item>
|
||||
<Button
|
||||
startIcon={<DeleteIcon />}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={() => deleteDialogCtl.openDialog()}
|
||||
disabled={!deleteDialogCtl.data?.length}
|
||||
>
|
||||
{t("button.delete")}
|
||||
</Button>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</PageHeader>
|
||||
</Grid>
|
||||
|
||||
@@ -148,7 +148,7 @@ export const ContextVars = () => {
|
||||
toast.error(error);
|
||||
}}
|
||||
onDeleteSuccess={() => {
|
||||
deleteDialogCtl.closeDialog(undefined, "postDelete");
|
||||
deleteDialogCtl.closeDialog();
|
||||
toast.success(t("message.item_delete_success"));
|
||||
}}
|
||||
/>
|
||||
@@ -176,18 +176,17 @@ export const ContextVars = () => {
|
||||
</Button>
|
||||
</Grid>
|
||||
) : null}
|
||||
{deleteDialogCtl.data?.length ? (
|
||||
<Grid item>
|
||||
<Button
|
||||
startIcon={<DeleteIcon />}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={() => deleteDialogCtl.openDialog()}
|
||||
>
|
||||
{t("button.delete")}
|
||||
</Button>
|
||||
</Grid>
|
||||
) : null}
|
||||
<Grid item>
|
||||
<Button
|
||||
startIcon={<DeleteIcon />}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={() => deleteDialogCtl.openDialog()}
|
||||
disabled={!deleteDialogCtl.data?.length}
|
||||
>
|
||||
{t("button.delete")}
|
||||
</Button>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</PageHeader>
|
||||
<Grid item xs={12}>
|
||||
|
||||
@@ -159,7 +159,7 @@ const NlpEntity = () => {
|
||||
toast.error(t("message.internal_server_error"));
|
||||
}}
|
||||
onDeleteSuccess={() => {
|
||||
deleteDialogCtl.closeDialog(undefined, "postDelete");
|
||||
deleteDialogCtl.closeDialog();
|
||||
toast.success(t("message.item_delete_success"));
|
||||
}}
|
||||
/>
|
||||
@@ -185,18 +185,17 @@ const NlpEntity = () => {
|
||||
</Button>
|
||||
</Grid>
|
||||
) : null}
|
||||
{deleteDialogCtl.data?.length ? (
|
||||
<Grid item>
|
||||
<Button
|
||||
startIcon={<DeleteIcon />}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={() => deleteDialogCtl.openDialog()}
|
||||
>
|
||||
{t("button.delete")}
|
||||
</Button>
|
||||
</Grid>
|
||||
) : null}
|
||||
<Grid item>
|
||||
<Button
|
||||
startIcon={<DeleteIcon />}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={() => deleteDialogCtl.openDialog()}
|
||||
disabled={!deleteDialogCtl.data?.length}
|
||||
>
|
||||
{t("button.delete")}
|
||||
</Button>
|
||||
</Grid>
|
||||
</Grid>
|
||||
<Grid mt={3}>
|
||||
<DataGrid
|
||||
|
||||
@@ -282,7 +282,7 @@ export default function NlpSample() {
|
||||
toast.error(error);
|
||||
}}
|
||||
onDeleteSuccess={() => {
|
||||
deleteDialogCtl.closeDialog(undefined, "postDelete");
|
||||
deleteDialogCtl.closeDialog();
|
||||
toast.success(t("message.item_delete_success"));
|
||||
}}
|
||||
/>
|
||||
@@ -379,18 +379,17 @@ export default function NlpSample() {
|
||||
{t("button.export")}
|
||||
</Button>
|
||||
) : null}
|
||||
{deleteDialogCtl.data?.length ? (
|
||||
<Grid item>
|
||||
<Button
|
||||
startIcon={<DeleteIcon />}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={() => deleteDialogCtl.openDialog()}
|
||||
>
|
||||
{t("button.delete")}
|
||||
</Button>
|
||||
</Grid>
|
||||
) : null}
|
||||
<Grid item>
|
||||
<Button
|
||||
startIcon={<DeleteIcon />}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={() => deleteDialogCtl.openDialog()}
|
||||
disabled={!deleteDialogCtl.data?.length}
|
||||
>
|
||||
{t("button.delete")}
|
||||
</Button>
|
||||
</Grid>
|
||||
</ButtonGroup>
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
||||
@@ -180,18 +180,17 @@ export const NlpValues = ({ entityId }: { entityId: string }) => {
|
||||
{t("button.add")}
|
||||
</Button>
|
||||
) : null}
|
||||
{deleteDialogCtl.data?.length ? (
|
||||
<Grid item>
|
||||
<Button
|
||||
startIcon={<DeleteIcon />}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={() => deleteDialogCtl.openDialog()}
|
||||
>
|
||||
{t("button.delete")}
|
||||
</Button>
|
||||
</Grid>
|
||||
) : null}
|
||||
<Grid item>
|
||||
<Button
|
||||
startIcon={<DeleteIcon />}
|
||||
variant="contained"
|
||||
color="error"
|
||||
onClick={() => deleteDialogCtl.openDialog()}
|
||||
disabled={!deleteDialogCtl.data?.length}
|
||||
>
|
||||
{t("button.delete")}
|
||||
</Button>
|
||||
</Grid>
|
||||
</ButtonGroup>
|
||||
</Grid>
|
||||
</PageHeader>
|
||||
@@ -209,7 +208,7 @@ export const NlpValues = ({ entityId }: { entityId: string }) => {
|
||||
toast.error(error);
|
||||
}}
|
||||
onDeleteSuccess={() => {
|
||||
deleteDialogCtl.closeDialog(undefined, "postDelete");
|
||||
deleteDialogCtl.closeDialog();
|
||||
toast.success(t("message.item_delete_success"));
|
||||
}}
|
||||
/>
|
||||
|
||||
@@ -6,14 +6,13 @@
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
*/
|
||||
|
||||
|
||||
import { DialogProps } from "@mui/material";
|
||||
import { Dispatch, SetStateAction, useState } from "react";
|
||||
|
||||
export type DialogControlProps<T> = Omit<DialogControl<T>, "openDialog">;
|
||||
type TCloseDialog = <E extends React.MouseEvent | Event | Object>(
|
||||
e?: E,
|
||||
reason?: "backdropClick" | "escapeKeyDown" | "postDelete",
|
||||
reason?: "backdropClick" | "escapeKeyDown",
|
||||
) => void;
|
||||
type TFnVoid<T> = (data?: T) => void;
|
||||
export type DialogControl<T = null> = DialogProps & {
|
||||
@@ -32,9 +31,6 @@ export const useDialog = <T,>(initialState: boolean): DialogControl<T> => {
|
||||
setOpen(true);
|
||||
};
|
||||
const closeDialog: TCloseDialog = (event, reason) => {
|
||||
if (reason === "postDelete") {
|
||||
setData(undefined);
|
||||
}
|
||||
if (reason !== "backdropClick") {
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user