feat: allow to download json for invoices and burn reports#34
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds JSON download functionality for CSP invoice drafts and burn reports, providing users with an additional data format option alongside the existing document/CSV formats.
Key Changes:
- Added two new API functions (
downloadCspDraftJSONanddownloadBurnReportJSON) to support JSON downloads - Renamed
downloadBurnReporttodownloadBurnReportCSVfor clarity - Added "Download JSON" buttons to both the invoice drafts interface and burn report interface
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/lib/api/backend.tsx | Added downloadCspDraftJSON and downloadBurnReportJSON functions; renamed downloadBurnReport to downloadBurnReportCSV |
| src/components/account/invoicing/DraftInvoiceCard.tsx | Added JSON download button with handler, updated existing button label to "Download .doc" for clarity |
| src/components/account/BurnReport.tsx | Split download functionality into separate CSV and JSON handlers with corresponding buttons |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/lib/api/backend.tsx
Outdated
| export const downloadCspDraftJSON = async (draftId: string) => { | ||
| const res = await axiosDapp.get(`/invoice-draft/download-csp-draft-json?draftId=${draftId}`); | ||
|
|
||
| if (res.status !== 200) { | ||
| throw new Error(`Download failed with status ${res.status}.`); | ||
| } | ||
|
|
||
| if (res.data?.error) { | ||
| throw new Error(res.data.error); | ||
| } | ||
|
|
||
| const payload = res.data?.data ?? res.data; | ||
|
|
||
| let filename = 'csp_draft.json'; | ||
| const contentDisposition = res.headers['content-disposition']; | ||
| if (contentDisposition) { | ||
| const filenameMatch = contentDisposition.match(/filename=([^;]+)/); | ||
| if (filenameMatch) { | ||
| filename = filenameMatch[1].replace(/['"]/g, ''); | ||
| } | ||
| } | ||
|
|
||
| const blob = new Blob([JSON.stringify(payload, null, 2)], { type: 'application/json' }); | ||
| const urlObj = URL.createObjectURL(blob); | ||
| const a = document.createElement('a'); | ||
| a.href = urlObj; | ||
| a.download = filename; | ||
| document.body.appendChild(a); | ||
| a.click(); | ||
| a.remove(); | ||
| setTimeout(() => URL.revokeObjectURL(urlObj), 0); | ||
|
|
||
| return payload; | ||
| }; |
There was a problem hiding this comment.
There's significant code duplication between downloadCspDraftJSON and downloadBurnReportJSON functions. Consider extracting the common JSON download logic into a reusable helper function that accepts the URL, default filename, and optional parameters. This would improve maintainability and reduce the risk of bugs when updating the download logic.
No description provided.