From fb0ff2235375ae68f46ab17dab175c1e0e47b39a Mon Sep 17 00:00:00 2001 From: MiriSafra Date: Mon, 28 Oct 2024 13:05:34 +0200 Subject: [PATCH] add handle download function Signed-off-by: MiriSafra --- .../applications-table/applications-table.tsx | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/client/src/app/pages/applications/applications-table/applications-table.tsx b/client/src/app/pages/applications/applications-table/applications-table.tsx index d102d69a2a..b53078ff39 100644 --- a/client/src/app/pages/applications/applications-table/applications-table.tsx +++ b/client/src/app/pages/applications/applications-table/applications-table.tsx @@ -71,7 +71,11 @@ import { checkAccess } from "@app/utils/rbac-utils"; import { useLocalTableControls } from "@app/hooks/table-controls"; // Queries -import { getArchetypeById, getAssessmentsByItemId } from "@app/api/rest"; +import { + getArchetypeById, + getAssessmentsByItemId, + getTasksByIds, +} from "@app/api/rest"; import { Assessment, Ref } from "@app/api/models"; import { useBulkDeleteApplicationMutation, @@ -109,6 +113,7 @@ import { DecoratedApplication, useDecoratedApplications, } from "./useDecoratedApplications"; +import yaml from "js-yaml"; export const ApplicationsTable: React.FC = () => { const { t } = useTranslation(); @@ -145,8 +150,13 @@ export const ApplicationsTable: React.FC = () => { const [applicationDependenciesToManage, setApplicationDependenciesToManage] = useState(null); + const isDependenciesModalOpen = applicationDependenciesToManage !== null; + const [isDownloadModalOpen, setIsDownloadModalOpen] = useState(false); + + const [selectedFormat, setSelectedFormat] = useState<"json" | "yaml">("json"); + const [assessmentToEdit, setAssessmentToEdit] = useState( null ); @@ -194,6 +204,23 @@ export const ApplicationsTable: React.FC = () => { }); }; + const handleDownload = async () => { + const ids = selectedRows + .map((row) => row.tasks.currentAnalyzer?.id) + .filter((id): id is number => typeof id === "number"); + + try { + const tasks = await getTasksByIds(ids, selectedFormat); + const data = + selectedFormat === "yaml" + ? yaml.dump(tasks, { indent: 2 }) + : JSON.stringify(tasks, null, 2); + setIsDownloadModalOpen(false); + } catch (error) { + console.error("Error fetching tasks:", error); + } + }; + const failedCancelTask = () => { pushNotification({ title: "Task", @@ -575,6 +602,20 @@ export const ApplicationsTable: React.FC = () => { > {t("actions.delete")} , + + application.tasks.currentAnalyzer?.id !== undefined + ) + } + onClick={() => { + setIsDownloadModalOpen(true); + }} + > + {t("actions.download", { what: "analysis details" })} + , ...(credentialsReadAccess ? [