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 modal #2146

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions client/src/app/api/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,24 @@ export function getTaskByIdAndFormat(
});
}

export function getTasksByIds(
ids: number[],
format: "json" | "yaml"
): Promise<Task[]> {
const isYaml = format === "yaml";
const headers = isYaml ? { ...yamlHeaders } : { ...jsonHeaders };
const responseType = isYaml ? "text" : "json";

return axios
.post<Task[]>(`${TASKS}/multiple`, ids, {
headers: headers,
responseType: responseType,
})
.then((response) => {
return response.data;
});
}

export const getTasksDashboard = () =>
axios
.get<TaskDashboard[]>(`${TASKS}/report/dashboard`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import {
DropdownItem,
Modal,
Tooltip,
FormGroup,
} from "@patternfly/react-core";
import {
CodeIcon,
PencilAltIcon,
TagIcon,
WarningTriangleIcon,
Expand Down Expand Up @@ -71,7 +73,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 +115,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 +152,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 +206,37 @@ 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);

const blob = new Blob([data], {
type:
selectedFormat === "json" ? "application/json" : "application/x-yaml",
});
const url = URL.createObjectURL(blob);
const downloadLink = document.createElement("a"); // שינוי שם למשתנה
downloadLink.href = url;
downloadLink.download = `logs - ${ids}.${selectedFormat}`;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
URL.revokeObjectURL(url);

setIsDownloadModalOpen(false);
} catch (error) {
console.error("Error fetching tasks:", error);
}
};

const failedCancelTask = () => {
pushNotification({
title: "Task",
Expand Down Expand Up @@ -575,6 +618,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 Expand Up @@ -1302,6 +1359,38 @@ export const ApplicationsTable: React.FC = () => {
}}
/>
</div>
<Modal
isOpen={isDownloadModalOpen}
variant="small"
title={t("actions.download", { what: "analysis details reports" })}
onClose={() => setIsDownloadModalOpen(false)}
>
<FormGroup label="Select Format" fieldId="format-select">
{" "}
<div>
{" "}
<Button
variant={selectedFormat === "json" ? "primary" : "secondary"}
onClick={() => setSelectedFormat("json")}
>
{" "}
{<CodeIcon />} JSON{" "}
</Button>{" "}
<Button
variant={selectedFormat === "yaml" ? "primary" : "secondary"}
onClick={() => setSelectedFormat("yaml")}
>
{" "}
{<CodeIcon />} YAML{" "}
</Button>{" "}
</div>{" "}
<p>Selected Format: {selectedFormat}</p>{" "}
</FormGroup>{" "}
<Button variant="primary" onClick={handleDownload}>
{" "}
{t("actions.download")}{" "}
</Button>{" "}
</Modal>
</ConditionalRender>
);
};
Loading