Skip to content

Commit

Permalink
Implement downloading of attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
ipasic-softserve committed Nov 10, 2024
1 parent 8756945 commit 33e46d0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/constants/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export const URLs = {
attachments: {
get: '/attachments',
patch: '/attachments',
delete: '/attachments'
delete: '/attachments',
download: '/attachments'
},
questions: {
get: '/questions',
Expand Down
6 changes: 4 additions & 2 deletions src/containers/course-section/resource-item/ResourceItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import {
selectionFields
} from '~/containers/course-section/resource-item/ResourceItem.constants'
import { styles } from '~/containers/course-section/resource-item/ResourceItem.styles'

import { ResourceService } from '~/services/resource-service'
import {
Attachment,
CourseResource,
ResourceAvailability,
ResourceAvailabilityStatusEnum,
Expand Down Expand Up @@ -208,7 +209,8 @@ const ResourceItem: FC<ResourceItemProps> = ({
const type = resourceType ?? resource.resourceType

if (type === ResourceType.Attachment) {
console.log('download the attachment / go to the attachment view')
const fileName = (resource as Attachment).fileName
void ResourceService.downloadAttachment(resource._id, fileName)
return
}

Expand Down
16 changes: 16 additions & 0 deletions src/services/resource-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ export const ResourceService = {
headers: { 'Content-Type': 'multipart/form-data' }
})
},
downloadAttachment: async (id: string, fileName: string): Promise<void> => {
const response = await axiosClient.get(
createUrlPath(URLs.resources.attachments.download, id),
{ responseType: 'blob' }
)

const url = window.URL.createObjectURL(new Blob([response.data]))
const link = document.createElement('a')
link.href = url
link.setAttribute('download', fileName)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)

setTimeout(() => window.URL.revokeObjectURL(url), 100)
},
getQuestions: (
params?: GetResourcesParams
): Promise<AxiosResponse<ItemsWithCount<Question>>> => {
Expand Down

0 comments on commit 33e46d0

Please sign in to comment.