Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement report feature suggestions #1009

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test-ct": "playwright test -c playwright-ct.config.ts"
},
"dependencies": {
"@deltares/fews-pi-requests": "^1.2.7",
"@deltares/fews-pi-requests": "^1.2.8",
"@deltares/fews-ssd-requests": "^1.0.1",
"@deltares/fews-ssd-webcomponent": "^1.0.2",
"@deltares/fews-web-oc-charts": "^3.0.3-beta.6",
Expand Down
53 changes: 21 additions & 32 deletions src/lib/download/downloadFiles.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { DocumentFormat } from '@deltares/fews-pi-requests'
import { toISOString } from '../date'

function downloadWithLink(url: string, fileName: string) {
const encodedFileName = encodeURIComponent(fileName)
url = `${url}&downloadAsFile=${encodedFileName}`
const link = document.createElement('a')
link.href = url
link.setAttribute('download', fileName)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
clickDownloadUrl(url, fileName)
}

async function downloadFileWithFetch(
Expand All @@ -25,12 +19,7 @@ async function downloadFileWithFetch(
})
if (response.ok) {
const blob = await response.blob()
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.setAttribute('download', fileName)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
clickDownloadBlob(blob, fileName)
} else {
console.error('Error downloading file')
}
Expand Down Expand Up @@ -70,7 +59,7 @@ export async function downloadFileAttachment(

export async function downloadFileWithXhr(
url: string,
fileNameSuffix?: string,
fileName: string,
): Promise<void> {
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest()
Expand All @@ -93,24 +82,8 @@ export async function downloadFileWithXhr(
?.split('filename=')[1]
.split(';')[0]

// For when backend does not set 'Access-Control-Expose-Headers: Content-Disposition'
const now = toISOString(new Date())
.replaceAll('-', '')
.replaceAll(':', '')
const fallBackFileName = `${now}${fileNameSuffix ?? '_DATA'}`

const downloadFileName = headerFileName ?? fallBackFileName
const blobUrl = window.URL.createObjectURL(req.response)

const a = document.createElement('a')
a.href = blobUrl
a.setAttribute('download', downloadFileName)

document.body.appendChild(a)
a.click()

a.remove()
window.URL.revokeObjectURL(blobUrl)
const downloadFileName = headerFileName ?? fileName
clickDownloadBlob(req.response, downloadFileName)

resolve()
}
Expand All @@ -125,3 +98,19 @@ export async function downloadFileWithXhr(
req.send()
})
}

export function clickDownloadBlob(blob: Blob, fileName: string) {
const blobUrl = window.URL.createObjectURL(blob)
clickDownloadUrl(blobUrl, fileName)
window.URL.revokeObjectURL(blobUrl)
}

export function clickDownloadUrl(url: string, fileName: string) {
const a = document.createElement('a')
a.href = url
a.setAttribute('download', fileName)

document.body.appendChild(a)
a.click()
a.remove()
}
9 changes: 8 additions & 1 deletion src/stores/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createTransformRequestFn } from '@/lib/requests/transformRequest'
import { configManager } from '@/services/application-config'
import type { BoundingBox } from '@/services/useBoundingBox'
import type { LngLat } from 'maplibre-gl'
import { toISOString } from '@/lib/date'

interface WorkflowsState {
workflowId: string | null
Expand Down Expand Up @@ -86,7 +87,13 @@ const useWorkflowsStore = defineStore('workflows', {
const url = webServiceProvider.processDataUrl(
completeFilter as ProcessDataFilter,
)
await downloadFileWithXhr(url.toString(), options?.fileName)

const now = toISOString(new Date())
.replaceAll('-', '')
.replaceAll(':', '')
const fileName = `${now}${options?.fileName ?? '_DATA'}`

await downloadFileWithXhr(url.toString(), fileName)
}
} finally {
this.numActiveWorkflows--
Expand Down
30 changes: 23 additions & 7 deletions src/views/ReportsDisplayView.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<template>
<div v-if="reports?.length" class="d-flex flex-column h-100">
<v-toolbar
class="py-1"
:title="reports.length === 1 ? reports[0].moduleInstanceId : undefined"
density="compact"
>
<v-toolbar class="py-1" density="compact">
<template v-if="reports.length === 1">
<div class="ml-5">{{ reportToTitle(reports[0]) }}</div>
<v-spacer />
</template>
<v-select
v-if="reports.length > 1"
v-else
v-model="selectedReport"
:items="reports"
return-object
:item-title="(item) => item.moduleInstanceId"
:item-title="(item) => reportToTitle(item)"
:item-value="(item) => item.moduleInstanceId"
hide-details
label="Report"
class="px-2"
menu-icon="mdi-chevron-down"
variant="solo-filled"
density="compact"
/>
Expand All @@ -27,9 +28,11 @@
hide-details
label="Analysis time"
class="pe-2 flex-0-0"
menu-icon="mdi-chevron-down"
variant="solo-filled"
density="compact"
/>
<v-btn @click="downloadFile" icon="mdi-download" />
</v-toolbar>
<iframe :key="url" :src="url" class="html-content" />
</div>
Expand All @@ -47,6 +50,7 @@ import {
import { computed, ref, watch } from 'vue'
import { configManager } from '@/services/application-config'
import { filterToParams } from '@deltares/fews-wms-requests'
import { downloadFileWithXhr } from '@/lib/download'

interface Props {
topologyNode?: TopologyNode
Expand Down Expand Up @@ -104,6 +108,18 @@ function reportItemToId(item: ReportItem) {
function reportItemToTitle(item: ReportItem) {
return `${item.timeZero} - ${item.reportId}`
}

function reportToTitle(item: Report) {
return item.moduleInstanceName ?? item.moduleInstanceId
}

async function downloadFile() {
const report = selectedReportItem.value
if (!report || !url.value) return

const fileName = `${report.timeZero}-${report.moduleInstanceId}`
downloadFileWithXhr(url.value, fileName)
}
</script>

<style scoped>
Expand Down
Loading