-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { SIDEKICK_URL } from '@/helpers/constants'; | ||
import pkg from '@/../package.json'; | ||
|
||
export function useReportDownload() { | ||
const isDownloadingVotes = ref(false); | ||
|
||
async function downloadFile(blob: Blob, fileName: string) { | ||
const href = URL.createObjectURL(blob); | ||
const a = Object.assign(document.createElement('a'), { | ||
href, | ||
style: 'display:none', | ||
download: fileName | ||
}); | ||
document.body.appendChild(a); | ||
a.click(); | ||
URL.revokeObjectURL(href); | ||
a.remove(); | ||
} | ||
|
||
async function downloadVotes(proposalId: string) { | ||
isDownloadingVotes.value = true; | ||
|
||
try { | ||
const response = await fetch(`${SIDEKICK_URL}/api/votes/${proposalId}`, { | ||
method: 'POST' | ||
}); | ||
|
||
if (response.status !== 200) { | ||
const data = await response.json(); | ||
|
||
// NOTE: This is a temporary workaround until the API is updated to return a proper error object | ||
if (data.error) { | ||
throw new Error(data.error.message); | ||
} else { | ||
throw new Error('PENDING_GENERATION'); | ||
} | ||
} | ||
|
||
downloadFile(await response.blob(), `${pkg.name}-report-${proposalId}`); | ||
return true; | ||
} finally { | ||
isDownloadingVotes.value = false; | ||
} | ||
} | ||
|
||
return { | ||
downloadVotes, | ||
isDownloadingVotes | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters