1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import * as S from "fp-ts/string"
- import * as RNEA from "fp-ts/ReadonlyNonEmptyArray"
- import { pipe } from "fp-ts/function"
- import { Ref } from "@nuxtjs/composition-api"
- import { refAutoReset } from "@vueuse/core"
- import { useI18n, useToast } from "~/helpers/utils/composables"
- export type downloadResponseReturnType = (() => void) | Ref<any>
- export default function useDownloadResponse(
- contentType: string,
- responseBody: Ref<string | ArrayBuffer>
- ): {
- downloadIcon: Ref<string>
- downloadResponse: () => void
- } {
- const downloadIcon = refAutoReset<"download" | "check">("download", 1000)
- const toast = useToast()
- const t = useI18n()
- const downloadResponse = () => {
- const dataToWrite = responseBody.value
- const file = new Blob([dataToWrite], { type: contentType })
- const a = document.createElement("a")
- const url = URL.createObjectURL(file)
- a.href = url
- // TODO: get uri from meta
- a.download = pipe(
- url,
- S.split("/"),
- RNEA.last,
- S.split("#"),
- RNEA.head,
- S.split("?"),
- RNEA.head
- )
- document.body.appendChild(a)
- a.click()
- downloadIcon.value = "check"
- toast.success(`${t("state.download_started")}`)
- setTimeout(() => {
- document.body.removeChild(a)
- URL.revokeObjectURL(url)
- }, 1000)
- }
- return {
- downloadIcon,
- downloadResponse,
- }
- }
|