Skip to content

Commit

Permalink
Merge pull request #1050 from bcgov/Download-map-files-with-plan-PDF
Browse files Browse the repository at this point in the history
Download map files with plan PDF
  • Loading branch information
brijesh-amin authored Nov 20, 2023
2 parents 1a8492d + b83f370 commit 4db12d8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
36 changes: 19 additions & 17 deletions src/components/rangeUsePlanPage/attachments/AttachmentRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ export const attachmentAccess = [
}
]

export const downloadAttachment = async (attachmentId, attachmentName) => {
const res = await axios.get(
GET_SIGNED_DOWNLOAD_URL(attachmentId),
getAuthHeaderConfig()
)
const fileRes = await axios.get(res.data.url, {
responseType: 'blob',
skipAuthorizationHeader: true
})
const url = window.URL.createObjectURL(fileRes.data)
const link = document.createElement('a')
link.href = url
link.setAttribute('download', attachmentName)
document.body.appendChild(link)
link.click()
link.parentNode.removeChild(link)
}

const AttachmentRow = ({ attachment, index, onDelete, error }) => {
const [isDownloading, setDownloading] = useState(false)
const [errorDownloading, setErrorDownloading] = useState()
Expand All @@ -40,26 +58,10 @@ const AttachmentRow = ({ attachment, index, onDelete, error }) => {
setErrorDownloading(null)
setDownloading(true)
try {
const res = await axios.get(
GET_SIGNED_DOWNLOAD_URL(attachment.id),
getAuthHeaderConfig()
)
const fileRes = await axios.get(res.data.url, {
responseType: 'blob',
skipAuthorizationHeader: true
})

const url = window.URL.createObjectURL(fileRes.data)
const link = document.createElement('a')
link.href = url
link.setAttribute('download', attachment.name)
document.body.appendChild(link)
link.click()
link.parentNode.removeChild(link)
downloadAttachment(attachment.id, attachment.name);
} catch (e) {
setErrorDownloading(e)
}

setDownloading(false)
}

Expand Down
4 changes: 3 additions & 1 deletion src/components/rangeUsePlanPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ const Base = ({
<Button type="button" onClick={closePDFModal}>
Close
</Button>
<PDFView match={match} agreementId={currentPlan.agreement.id} />
<PDFView match={match} agreementId={currentPlan.agreement.id} mapAttachments={currentPlan.files.filter((item) => {
return item.type === 'mapAttachments'
})} />
</Modal.Actions>
</Modal>
))
Expand Down
6 changes: 5 additions & 1 deletion src/components/rangeUsePlanPage/pdf/PDFView.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState } from 'react'
import { generatePDF } from '../../../api'
import { PrimaryButton } from '../../common'
import { downloadAttachment } from '../attachments/AttachmentRow'

const PDFView = ({ match, agreementId }) => {
const PDFView = ({ match, agreementId, mapAttachments }) => {
const { planId } = match.params
const hasError = !!error
const [loading, setLoading] = useState(false)
Expand All @@ -22,6 +23,9 @@ const PDFView = ({ match, agreementId }) => {
link.click()
link.parentNode.removeChild(link)
setLoading(false)
mapAttachments.forEach(attachment => {
downloadAttachment(attachment.id, attachment.name);
});
} catch (e) {
setLoading(false)
setError(e)
Expand Down

0 comments on commit 4db12d8

Please sign in to comment.