-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add component for downloading the static report
Signed-off-by: ibolton336 <ibolton@redhat.com>
- Loading branch information
1 parent
f8e2b7e
commit bb4ca97
Showing
4 changed files
with
143 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...pp/pages/applications/components/application-detail-drawer/components/download-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React, { useState } from "react"; | ||
import { APPLICATIONS } from "@app/api/rest"; | ||
import { Button } from "@patternfly/react-core"; | ||
import spacing from "@patternfly/react-styles/css/utilities/Spacing/spacing"; | ||
import { Application } from "@app/api/models"; | ||
import { Spinner } from "@patternfly/react-core"; | ||
|
||
export enum MimeType { | ||
TAR = "tar", | ||
YAML = "yaml", | ||
} | ||
function DownloadButton({ | ||
application, | ||
mimeType, | ||
}: { | ||
application: Application; | ||
mimeType: MimeType; | ||
}) { | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const handleDownload = async () => { | ||
let acceptHeader = "application/x-tar"; | ||
|
||
switch (mimeType) { | ||
case MimeType.YAML: | ||
acceptHeader = "application/x-yaml"; | ||
break; | ||
case MimeType.TAR: | ||
default: | ||
acceptHeader = "application/x-tar"; | ||
} | ||
setIsLoading(true); | ||
|
||
try { | ||
const response = await fetch( | ||
`${APPLICATIONS}/${application?.id}/analysis/report`, | ||
{ | ||
headers: { | ||
Accept: acceptHeader, | ||
}, | ||
} | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error("Network response was not ok when downloading file."); | ||
} | ||
|
||
const blob = await response.blob(); | ||
const url = window.URL.createObjectURL(blob); | ||
const a = document.createElement("a"); | ||
a.style.display = "none"; | ||
a.href = url; | ||
a.download = `analysis-${application.name}.` + mimeType; | ||
|
||
document.body.appendChild(a); | ||
a.click(); | ||
|
||
window.URL.revokeObjectURL(url); | ||
} catch (error) { | ||
console.error("There was an error downloading the file:", error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
{isLoading ? ( | ||
<Spinner size="sm" /> | ||
) : ( | ||
<Button | ||
onClick={handleDownload} | ||
id={`download-${mimeType}-button`} | ||
variant="link" | ||
className={spacing.pXs} | ||
> | ||
{mimeType === MimeType.YAML ? "YAML" : "Report"} | ||
</Button> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export default DownloadButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters