From b83f37090a0368e10196be40532dd9fc2f765514 Mon Sep 17 00:00:00 2001 From: Brijesh Date: Mon, 20 Nov 2023 10:35:42 -0800 Subject: [PATCH] Download map files with plan PDF --- .../attachments/AttachmentRow.js | 36 ++++++++++--------- src/components/rangeUsePlanPage/index.js | 4 ++- .../rangeUsePlanPage/pdf/PDFView.js | 6 +++- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/components/rangeUsePlanPage/attachments/AttachmentRow.js b/src/components/rangeUsePlanPage/attachments/AttachmentRow.js index 61d38fe9..0d1ab1ac 100644 --- a/src/components/rangeUsePlanPage/attachments/AttachmentRow.js +++ b/src/components/rangeUsePlanPage/attachments/AttachmentRow.js @@ -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() @@ -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) } diff --git a/src/components/rangeUsePlanPage/index.js b/src/components/rangeUsePlanPage/index.js index aaa47b79..caa67e91 100644 --- a/src/components/rangeUsePlanPage/index.js +++ b/src/components/rangeUsePlanPage/index.js @@ -215,7 +215,9 @@ const Base = ({ - + { + return item.type === 'mapAttachments' + })} /> )) diff --git a/src/components/rangeUsePlanPage/pdf/PDFView.js b/src/components/rangeUsePlanPage/pdf/PDFView.js index 61b78cbe..f65b028c 100644 --- a/src/components/rangeUsePlanPage/pdf/PDFView.js +++ b/src/components/rangeUsePlanPage/pdf/PDFView.js @@ -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) @@ -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)