Skip to content

Commit f2bebcf

Browse files
committed
chore: export type handle download props
1 parent 4c44bfa commit f2bebcf

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

src/Common/Constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ export const ZERO_TIME_STRING = '0001-01-01T00:00:00Z'
476476
export const EXCLUDED_FALSY_VALUES = [undefined, null, '', NaN] as const
477477

478478
export const API_STATUS_CODES = {
479+
OK: 200,
479480
BAD_REQUEST: 400,
480481
UNAUTHORIZED: 401,
481482
PERMISSION_DENIED: 403,

src/Shared/Components/CICDHistory/Artifacts.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ const Artifacts = ({
152152
}, [copied])
153153

154154
async function handleArtifact() {
155+
// TODO: Use useDownload() Hook instead to download file/folder
155156
try {
156157
const response = await getArtifactPromise()
157158
const b = await (response as any).blob()

src/Shared/Hooks/UseDownload/UseDownload.tsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import { showError } from '@Common/Helper'
22
import { ToastBody } from '@Common/ToastBody'
33
import { useState } from 'react'
44
import { toast } from 'react-toastify'
5+
import { API_STATUS_CODES } from '@Common/Constants'
56
import { getDownloadResponse } from './service'
6-
7-
export interface HandleDownloadProps {
8-
downloadUrl: string
9-
showFilePreparingToast?: boolean
10-
fileName?: string
11-
}
7+
import { HandleDownloadProps } from './types'
128

139
const useDownload = () => {
1410
const [isDownloading, setIsDownloading] = useState<boolean>(false)
1511

16-
/*
17-
downloadUrl: API url for downloading file
18-
showFilePreparingToast: Show toast 'Preparing file for download'
19-
fileName: fileName of the downloaded file
20-
*/
21-
const handleDownload = async ({ downloadUrl, showFilePreparingToast = false, fileName = 'file.tgz' }) => {
12+
/**
13+
* @param downloadUrl - API url for downloading file
14+
* @param filterType - Show toast 'Preparing file for download'
15+
* @param query - fileName of the downloaded file
16+
*/
17+
const handleDownload = async ({
18+
downloadUrl,
19+
showFilePreparingToast = false,
20+
fileName = 'file.tgz',
21+
}: HandleDownloadProps) => {
2222
setIsDownloading(true)
2323
if (showFilePreparingToast) {
2424
toast.info(
@@ -30,7 +30,7 @@ const useDownload = () => {
3030
}
3131
try {
3232
const response = await getDownloadResponse(downloadUrl)
33-
if (response.status === 200) {
33+
if (response.status === API_STATUS_CODES.OK) {
3434
const data = await (response as any).blob()
3535

3636
// Create a new URL object
@@ -55,11 +55,12 @@ const useDownload = () => {
5555

5656
toast.success('Downloaded Successfully')
5757
} else {
58-
const error = (await response.json()).errors[0].userMessage
59-
showError(error)
58+
const jsonResponse = await response?.json()
59+
const error = jsonResponse.errors[0].userMessage || jsonResponse.errors[0].internalMessage
60+
showError(new Error(error))
6061
}
6162
} catch (error) {
62-
toast.error(error)
63+
showError(error)
6364
} finally {
6465
setIsDownloading(false)
6566
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { default as useDownload } from './UseDownload'
2+
export * from './types'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface HandleDownloadProps {
2+
downloadUrl: string
3+
showFilePreparingToast?: boolean
4+
fileName?: string
5+
}

0 commit comments

Comments
 (0)