Add 'download' option for resources
parent
234760c1f4
commit
3029df96a2
|
@ -11,9 +11,14 @@ import SecondaryActionsGroup from "./SecondaryActionsGroup";
|
||||||
import {
|
import {
|
||||||
EditOutlined,
|
EditOutlined,
|
||||||
FileCopyOutlined,
|
FileCopyOutlined,
|
||||||
OpenInNewOutlined
|
OpenInNewOutlined,
|
||||||
|
CloudDownloadOutlined
|
||||||
} from "@material-ui/icons";
|
} from "@material-ui/icons";
|
||||||
import { useToast, useResourceSecurity } from "../../../../hooks";
|
import {
|
||||||
|
useToast,
|
||||||
|
useResourceSecurity,
|
||||||
|
useFileDownload
|
||||||
|
} from "../../../../hooks";
|
||||||
import { useHistory } from "react-router-dom";
|
import { useHistory } from "react-router-dom";
|
||||||
|
|
||||||
const __ROWS_PER_PAGE_OPTIONS = [10, 20, 50, 100];
|
const __ROWS_PER_PAGE_OPTIONS = [10, 20, 50, 100];
|
||||||
|
@ -34,6 +39,7 @@ const ResourcesContainer = () => {
|
||||||
const { getResources } = useResourcesApi();
|
const { getResources } = useResourcesApi();
|
||||||
const { getResourceCategories } = useDictionariesApi();
|
const { getResourceCategories } = useDictionariesApi();
|
||||||
const { secureUrl } = useResourceSecurity();
|
const { secureUrl } = useResourceSecurity();
|
||||||
|
const { download } = useFileDownload();
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -72,6 +78,21 @@ const ResourcesContainer = () => {
|
||||||
[resourceCategories]
|
[resourceCategories]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const prepareUrl = useCallback(
|
||||||
|
(url, download, secure) => {
|
||||||
|
if (download) {
|
||||||
|
url = `${url}?download=${download}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (secure) {
|
||||||
|
url = secureUrl(url, download);
|
||||||
|
}
|
||||||
|
|
||||||
|
return url;
|
||||||
|
},
|
||||||
|
[secureUrl]
|
||||||
|
);
|
||||||
|
|
||||||
const actions = useMemo(
|
const actions = useMemo(
|
||||||
() => [
|
() => [
|
||||||
{
|
{
|
||||||
|
@ -96,16 +117,27 @@ const ResourcesContainer = () => {
|
||||||
{
|
{
|
||||||
code: "open-in-new-tab",
|
code: "open-in-new-tab",
|
||||||
effect: (event, resource) => {
|
effect: (event, resource) => {
|
||||||
const url = resource.secured ? secureUrl(resource.url) : resource.url;
|
const url = prepareUrl(resource.url, false, resource.secured);
|
||||||
window.open(url, "_blank");
|
window.open(url, "_blank");
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
},
|
},
|
||||||
icon: OpenInNewOutlined,
|
icon: OpenInNewOutlined,
|
||||||
tooltip: t("Generic.OpenInNewTab"),
|
tooltip: t("Generic.OpenInNewTab"),
|
||||||
top: false
|
top: false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "download",
|
||||||
|
effect: (event, resource) => {
|
||||||
|
const url = prepareUrl(resource.url, true, resource.secured);
|
||||||
|
download(url);
|
||||||
|
event.preventDefault();
|
||||||
|
},
|
||||||
|
icon: CloudDownloadOutlined,
|
||||||
|
tooltip: t("Generic.Download"),
|
||||||
|
top: false
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
[t, info, secureUrl, history]
|
[t, info, secureUrl, history, download, prepareUrl]
|
||||||
);
|
);
|
||||||
|
|
||||||
const columns = useMemo(
|
const columns = useMemo(
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { useToast } from "../contexts/ToastContext";
|
import { useToast } from "../contexts/ToastContext";
|
||||||
import useHttpRequest from "./useHttpRequest";
|
import useHttpRequest from "./useHttpRequest";
|
||||||
import useResourceSecurity from "./useResourceSecurity";
|
import useResourceSecurity from "./useResourceSecurity";
|
||||||
|
import useFileDownload from "./useFileDownload";
|
||||||
|
|
||||||
export { useToast, useHttpRequest, useResourceSecurity };
|
export { useToast, useHttpRequest, useResourceSecurity, useFileDownload };
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
const useFileDownload = () => {
|
||||||
|
const download = (uri, name = "") => {
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.setAttribute("download", name);
|
||||||
|
link.href = uri;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
link.remove();
|
||||||
|
};
|
||||||
|
|
||||||
|
return { download };
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useFileDownload;
|
|
@ -3,9 +3,14 @@ import { useCallback } from "react";
|
||||||
|
|
||||||
const useResourceSecurity = () => {
|
const useResourceSecurity = () => {
|
||||||
const { token } = useUserState();
|
const { token } = useUserState();
|
||||||
const secureUrl = useCallback((url) => `${url}?token=${token.raw}`, [
|
const secureUrl = useCallback(
|
||||||
token.raw
|
(url, join) => {
|
||||||
]);
|
const separator = join ? "&" : "?";
|
||||||
|
const securedUrl = `${url}${separator}token=${token.raw}`;
|
||||||
|
return securedUrl;
|
||||||
|
},
|
||||||
|
[token.raw]
|
||||||
|
);
|
||||||
|
|
||||||
return { secureUrl };
|
return { secureUrl };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue