delete resource from list

master
Tudor Stanciu 2022-12-18 04:29:14 +02:00
parent 453dfa96ae
commit ad254682f0
7 changed files with 85 additions and 30 deletions

View File

@ -41,7 +41,8 @@
}, },
"Edit": "Edit", "Edit": "Edit",
"More": "More", "More": "More",
"OpenInNewTab": "Open in new tab" "OpenInNewTab": "Open in new tab",
"Delete": "Delete"
}, },
"Menu": { "Menu": {
"Dashboard": "Dashboard", "Dashboard": "Dashboard",

View File

@ -32,7 +32,8 @@
}, },
"Edit": "Editează", "Edit": "Editează",
"More": "Mai mult", "More": "Mai mult",
"OpenInNewTab": "Deschide într-un tab nou" "OpenInNewTab": "Deschide într-un tab nou",
"Delete": "Șterge"
}, },
"Menu": { "Menu": {
"Dashboard": "Dashboard", "Dashboard": "Dashboard",

View File

@ -15,8 +15,9 @@ import { makeStyles } from "@material-ui/core/styles";
import style from "../styles"; import style from "../styles";
import ResourcePreviewComponent from "./ResourcePreviewComponent"; import ResourcePreviewComponent from "./ResourcePreviewComponent";
import MimeTypeComponent from "./MimeTypeComponent"; import MimeTypeComponent from "./MimeTypeComponent";
import DeleteDialog from "./DeleteDialog"; import useResourceDeleteDialog from "../../hooks/useResourceDeleteDialog";
import { onTextFieldChange } from "../../../../utils/adapters"; import { onTextFieldChange } from "../../../../utils/adapters";
import { useHistory } from "react-router-dom";
const useStyles = makeStyles(style); const useStyles = makeStyles(style);
@ -25,10 +26,17 @@ const ResourceComponent = ({
mimeTypes, mimeTypes,
resourceCategories, resourceCategories,
onPropertyChange, onPropertyChange,
deleteProps, processing,
processing setProcessing
}) => { }) => {
const classes = useStyles(); const classes = useStyles();
const history = useHistory();
const {
component: ResourceDeleteDialog,
handleOpen: handleOpenDeleteDialog
} = useResourceDeleteDialog(resource, setProcessing, () =>
history.push("/resources")
);
return ( return (
<> <>
@ -138,7 +146,7 @@ const ResourceComponent = ({
color="secondary" color="secondary"
className={classes.button} className={classes.button}
startIcon={<DeleteIcon />} startIcon={<DeleteIcon />}
onClick={deleteProps.onOpen} onClick={handleOpenDeleteDialog}
disabled={!!processing} disabled={!!processing}
> >
{processing === "delete" ? "Deleting" : "Delete"} {processing === "delete" ? "Deleting" : "Delete"}
@ -156,7 +164,7 @@ const ResourceComponent = ({
</Grid> </Grid>
</Grid> </Grid>
</Paper> </Paper>
<DeleteDialog {...deleteProps} title={resource.resourceName} /> {ResourceDeleteDialog}
</> </>
); );
}; };
@ -166,8 +174,8 @@ ResourceComponent.propTypes = {
mimeTypes: PropTypes.array.isRequired, mimeTypes: PropTypes.array.isRequired,
resourceCategories: PropTypes.array.isRequired, resourceCategories: PropTypes.array.isRequired,
onPropertyChange: PropTypes.func.isRequired, onPropertyChange: PropTypes.func.isRequired,
deleteProps: PropTypes.object.isRequired, processing: PropTypes.string,
processing: PropTypes.bool.isRequired setProcessing: PropTypes.func.isRequired
}; };
export default ResourceComponent; export default ResourceComponent;

View File

@ -15,12 +15,11 @@ const ResourceContainer = () => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [mimeTypes, setMimeTypes] = useState(null); const [mimeTypes, setMimeTypes] = useState(null);
const [resourceCategories, setResourceCategories] = useState(null); const [resourceCategories, setResourceCategories] = useState(null);
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const [processing, setProcessing] = useState(null); const [processing, setProcessing] = useState(null);
const classes = useStyles(); const classes = useStyles();
const params = useParams(); const params = useParams();
const { getResource, deleteResource } = useResourcesApi(); const { getResource } = useResourcesApi();
const { getMimeTypes, getResourceCategories } = useDictionariesApi(); const { getMimeTypes, getResourceCategories } = useDictionariesApi();
const { success } = useToast(); const { success } = useToast();
@ -48,15 +47,6 @@ const ResourceContainer = () => {
setState((prev) => ({ ...prev, [prop]: value })); setState((prev) => ({ ...prev, [prop]: value }));
}; };
const handleDelete = async (resourceId) => {
setProcessing("delete");
const response = await deleteResource(resourceId);
if (response && response.resourceId === state.resourceId)
success(`Resource '${state.resourceName}' was successfully deleted.`);
setProcessing(null);
setDeleteDialogOpen(false);
};
if (loading || !state || !mimeTypes || !resourceCategories) if (loading || !state || !mimeTypes || !resourceCategories)
return <LoadingText lines={15} onPaper />; return <LoadingText lines={15} onPaper />;
@ -69,13 +59,8 @@ const ResourceContainer = () => {
mimeTypes={mimeTypes} mimeTypes={mimeTypes}
resourceCategories={resourceCategories} resourceCategories={resourceCategories}
onPropertyChange={handlePropertyChange} onPropertyChange={handlePropertyChange}
deleteProps={{
open: deleteDialogOpen,
onOpen: () => setDeleteDialogOpen(true),
onClose: () => setDeleteDialogOpen(false),
onConfirm: () => handleDelete(state.resourceId)
}}
processing={processing} processing={processing}
setProcessing={setProcessing}
/> />
</div> </div>
</> </>

View File

@ -37,7 +37,7 @@ DeleteDialog.propTypes = {
open: PropTypes.bool.isRequired, open: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired, onClose: PropTypes.func.isRequired,
onConfirm: PropTypes.func.isRequired, onConfirm: PropTypes.func.isRequired,
title: PropTypes.string.isRequired title: PropTypes.string
}; };
export default DeleteDialog; export default DeleteDialog;

View File

@ -0,0 +1,42 @@
import React, { useState } from "react";
import { useResourcesApi } from "../api";
import DeleteDialog from "./components/DeleteDialog";
import { useToast } from "../../../hooks";
const useResourceDeleteDialog = (resource, setProcessing, onComplete) => {
const [state, setState] = useState(
resource ?? { resourceId: null, resourceName: null }
);
const [deleteDialogOpen, setOpen] = useState(false);
const { deleteResource } = useResourcesApi();
const { success } = useToast();
const handleDelete = async (resourceId) => {
setProcessing && setProcessing("delete");
const response = await deleteResource(resourceId);
if (response && response.resourceId === state.resourceId) {
success(`Resource '${state.resourceName}' was successfully deleted.`);
setProcessing && setProcessing(null);
setOpen(false);
onComplete && onComplete(state);
}
};
return {
component: (
<DeleteDialog
title={state.resourceName}
open={deleteDialogOpen}
onClose={() => setOpen(false)}
onConfirm={() => handleDelete(state.resourceId)}
/>
),
handleOpen: (_event, resource) => {
resource && setState(resource);
setOpen(true);
}
};
};
export default useResourceDeleteDialog;

View File

@ -12,7 +12,8 @@ import {
EditOutlined, EditOutlined,
FileCopyOutlined, FileCopyOutlined,
OpenInNewOutlined, OpenInNewOutlined,
CloudDownloadOutlined CloudDownloadOutlined,
DeleteOutlined
} from "@material-ui/icons"; } from "@material-ui/icons";
import { import {
useToast, useToast,
@ -20,6 +21,7 @@ import {
useFileDownload useFileDownload
} from "../../../../hooks"; } from "../../../../hooks";
import { useHistory } from "react-router-dom"; import { useHistory } from "react-router-dom";
import useResourceDeleteDialog from "../../hooks/useResourceDeleteDialog";
const __ROWS_PER_PAGE_OPTIONS = [10, 20, 50, 100]; const __ROWS_PER_PAGE_OPTIONS = [10, 20, 50, 100];
const __RESOURCE_NAME_MAX_LENGTH = 35; const __RESOURCE_NAME_MAX_LENGTH = 35;
@ -41,6 +43,12 @@ const ResourcesContainer = () => {
const { secureUrl } = useResourceSecurity(); const { secureUrl } = useResourceSecurity();
const { download } = useFileDownload(); const { download } = useFileDownload();
const history = useHistory(); const history = useHistory();
const {
component: ResourceDeleteDialog,
handleOpen: handleOpenDeleteDialog
} = useResourceDeleteDialog(null, null, () =>
changeFilters(defaultResourcesFilters)
);
useEffect(() => { useEffect(() => {
getResourceCategories().then((r) => setResourceCategories(r)); getResourceCategories().then((r) => setResourceCategories(r));
@ -135,9 +143,19 @@ const ResourcesContainer = () => {
icon: CloudDownloadOutlined, icon: CloudDownloadOutlined,
tooltip: t("Generic.Download"), tooltip: t("Generic.Download"),
top: false top: false
},
{
code: "delete",
effect: (event, resource) => {
handleOpenDeleteDialog(event, resource);
event.preventDefault();
},
icon: DeleteOutlined,
tooltip: t("Generic.Delete"),
top: false
} }
], ],
[t, info, secureUrl, history, download, prepareUrl] [t, info, secureUrl, history, download, prepareUrl, handleOpenDeleteDialog]
); );
const columns = useMemo( const columns = useMemo(
@ -358,7 +376,6 @@ const ResourcesContainer = () => {
return ( return (
<> <>
<PageTitle title={t("Resource.List.Title")} /> <PageTitle title={t("Resource.List.Title")} />
<MUIDataTable <MUIDataTable
title={t("Resource.List.SubTitle")} title={t("Resource.List.SubTitle")}
columns={columns} columns={columns}
@ -424,6 +441,7 @@ const ResourcesContainer = () => {
} }
}} }}
/> />
{ResourceDeleteDialog}
</> </>
); );
}; };