Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨add handle download function #1

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -109,6 +113,7 @@ import {
DecoratedApplication,
useDecoratedApplications,
} from "./useDecoratedApplications";
import yaml from "js-yaml";

export const ApplicationsTable: React.FC = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -145,8 +150,13 @@ export const ApplicationsTable: React.FC = () => {

const [applicationDependenciesToManage, setApplicationDependenciesToManage] =
useState<DecoratedApplication | null>(null);

const isDependenciesModalOpen = applicationDependenciesToManage !== null;

const [isDownloadModalOpen, setIsDownloadModalOpen] = useState(false);

const [selectedFormat, setSelectedFormat] = useState<"json" | "yaml">("json");

const [assessmentToEdit, setAssessmentToEdit] = useState<Assessment | null>(
null
);
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -575,6 +602,20 @@ export const ApplicationsTable: React.FC = () => {
>
{t("actions.delete")}
</DropdownItem>,
<DropdownItem
key="applications-bulk-download"
isDisabled={
!selectedRows.some(
(application: DecoratedApplication) =>
application.tasks.currentAnalyzer?.id !== undefined
)
}
onClick={() => {
setIsDownloadModalOpen(true);
}}
>
{t("actions.download", { what: "analysis details" })}
</DropdownItem>,
...(credentialsReadAccess
? [
<DropdownItem
Expand Down
Loading