-
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.
Move logic out of presentation layer and use file-saver
Signed-off-by: ibolton336 <ibolton@redhat.com> Update output Signed-off-by: ibolton336 <ibolton@redhat.com>
- Loading branch information
1 parent
1ba754e
commit 590c394
Showing
3 changed files
with
90 additions
and
53 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
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,55 @@ | ||
import axios from "axios"; | ||
import { saveAs } from "file-saver"; | ||
import { APPLICATIONS } from "@app/api/rest"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { MimeType } from "@app/pages/applications/components/application-detail-drawer/components/download-button"; | ||
|
||
interface DownloadOptions { | ||
applicationId: number; | ||
mimeType: MimeType; | ||
} | ||
|
||
export const downloadStaticReport = async ({ | ||
applicationId, | ||
mimeType, | ||
}: DownloadOptions): Promise<void> => { | ||
let acceptHeader = "application/x-tar"; | ||
|
||
switch (mimeType) { | ||
case MimeType.YAML: | ||
acceptHeader = "application/x-yaml"; | ||
break; | ||
case MimeType.TAR: | ||
default: | ||
acceptHeader = "application/x-tar"; | ||
} | ||
|
||
try { | ||
const response = await axios.get( | ||
`${APPLICATIONS}/${applicationId}/analysis/report`, | ||
{ | ||
responseType: "blob", | ||
headers: { | ||
Accept: acceptHeader, | ||
}, | ||
} | ||
); | ||
|
||
if (response.status !== 200) { | ||
throw new Error("Network response was not ok when downloading file."); | ||
} | ||
|
||
const blob = new Blob([response.data]); | ||
saveAs( | ||
blob, | ||
`analysis-report-app-${applicationId}.${acceptHeader.split("-")[1]}` | ||
); | ||
} catch (error) { | ||
console.error("There was an error downloading the file:", error); | ||
throw error; | ||
} | ||
}; | ||
|
||
export const useDownloadStaticReport = () => { | ||
return useMutation(downloadStaticReport); | ||
}; |