-
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 b992f78
Showing
3 changed files
with
98 additions
and
5 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
80 changes: 80 additions & 0 deletions
80
...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,80 @@ | ||
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"; | ||
|
||
function DownloadButton({ | ||
application, | ||
mimeType, | ||
}: { | ||
application: Application; | ||
mimeType: string; | ||
}) { | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const handleDownload = async () => { | ||
let acceptHeader = "application/x-tar"; | ||
|
||
switch (mimeType) { | ||
case "yaml": | ||
acceptHeader = "application/x-yaml"; | ||
break; | ||
case "tar": | ||
default: | ||
acceptHeader = "application/x-tar"; | ||
} | ||
setIsLoading(true); | ||
|
||
try { | ||
const response = await fetch( | ||
`${APPLICATIONS}/${application?.id}/analysis`, | ||
{ | ||
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-html" | ||
variant="link" | ||
className={spacing.pXs} | ||
> | ||
{`Download ${mimeType.toUpperCase()}`} | ||
</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