Skip to content

Commit 954156b

Browse files
committed
fix(DHIS2-16988): download uncompressed json rather than open inline
1 parent 6cd4f22 commit 954156b

File tree

2 files changed

+127
-11
lines changed

2 files changed

+127
-11
lines changed

src/utils/helper.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,53 @@ const uploadFile = ({
188188
})
189189
}
190190

191-
const downloadWindowTitle = i18n.t('Loading exported data')
192-
const downloadWindowHtml = `
193-
<div style="height: 100%; width: 100%; display: flex; justify-content: center; align-items: center; color: rgb(33, 41, 52)">
194-
<p>${downloadWindowTitle}</p>
195-
</div>
196-
`
191+
const propsToInclude = [
192+
'program',
193+
'ouMode',
194+
'orgUnits',
195+
'orgUnit',
196+
'trackedEntityType',
197+
]
198+
const getParamsForFileName = (url) => {
199+
const params = [...new URL(url).searchParams.entries()]
200+
return params
201+
.filter(([prop]) => propsToInclude.includes(prop))
202+
.map(([prop, val]) => `${prop}_${val}`)
203+
.join('__')
204+
}
197205

198206
// call stub function if available
199-
const locationAssign = (url) => {
207+
const locationAssign = (relativeUrl) => {
200208
if (window.locationAssign) {
201-
window.locationAssign(url)
209+
window.locationAssign(relativeUrl)
202210
} else {
203-
const downloadWindow = window.open(url, '_blank')
211+
try {
212+
const url = relativeUrl.startsWith('..')
213+
? new URL(relativeUrl, document.baseURI).href
214+
: relativeUrl
215+
216+
const requestParams = getParamsForFileName(url)
217+
218+
const urlFilePart = new URL(url).pathname.split('/').pop()
219+
const [, file, extension] = urlFilePart.match(/(^[^.]+)(\..+$)/)
204220

205-
downloadWindow.document.title = downloadWindowTitle
206-
downloadWindow.document.body.innerHTML = downloadWindowHtml // does not work in Chrome
221+
const timeStamp = new Intl.DateTimeFormat('en-CA').format(
222+
new Date()
223+
) // en-CA is yyyy-MM-dd
224+
225+
const downloadedFileName = `${file}_${timeStamp}_${requestParams}${extension}`
226+
227+
const link = document.createElement('a')
228+
link.href = url
229+
link.download = downloadedFileName
230+
link.target = '_blank'
231+
link.click()
232+
233+
return link
234+
} catch (err) {
235+
console.error(err)
236+
window.open(relativeUrl, '_blank')
237+
}
207238
}
208239
}
209240

src/utils/helper.test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { locationAssign } from './helper.js'
2+
3+
describe('locationAssign', () => {
4+
beforeEach(() => {
5+
jest.useFakeTimers('modern')
6+
jest.setSystemTime(new Date(2024, 2, 12))
7+
})
8+
9+
afterEach(() => {
10+
jest.useRealTimers()
11+
})
12+
13+
it('should create a file name based on the params', () => {
14+
const url =
15+
'https://debug.dhis2.org/dev/api/tracker/trackedEntities.json?ouMode=CAPTURE&format=json&includeDeleted=false&dataElementIdScheme=UID&eventIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=trackedEntities.json&paging=false&totalPages=false&program=lxAQ7Zs9VYR'
16+
const link = locationAssign(url)
17+
expect(link.download).toEqual(
18+
'trackedEntities_2024-03-12_ouMode_CAPTURE__program_lxAQ7Zs9VYR.json'
19+
)
20+
})
21+
it('should create url with orgUnits', () => {
22+
const url =
23+
'https://debug.dhis2.org/dev/api/tracker/trackedEntities.json?ouMode=SELECTED&includeDeleted=false&dataElementIdScheme=UID&eventIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=trackedEntities.json&paging=false&totalPages=false&orgUnits=O6uvpzGd5pu,fdc6uOvgoji&program=kla3mAPgvCH'
24+
const link = locationAssign(url)
25+
expect(link.download).toEqual(
26+
'trackedEntities_2024-03-12_ouMode_SELECTED__orgUnits_O6uvpzGd5pu,fdc6uOvgoji__program_kla3mAPgvCH.json'
27+
)
28+
})
29+
it('should create url with tracked entities', () => {
30+
const url =
31+
'https://debug.dhis2.org/dev/api/tracker/trackedEntities.json?ouMode=SELECTED&includeDeleted=false&dataElementIdScheme=UID&eventIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=trackedEntities.json&paging=false&totalPages=false&orgUnits=ImspTQPwCqd&trackedEntityType=bVkFYAvoUCP'
32+
const link = locationAssign(url)
33+
expect(link.download).toEqual(
34+
'trackedEntities_2024-03-12_ouMode_SELECTED__orgUnits_ImspTQPwCqd__trackedEntityType_bVkFYAvoUCP.json'
35+
)
36+
})
37+
it('should create url with CSV', () => {
38+
const url =
39+
'https://debug.dhis2.org/dev/api/tracker/trackedEntities.csv?ouMode=SELECTED&includeDeleted=false&dataElementIdScheme=UID&eventIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=trackedEntities.csv&paging=false&totalPages=false&orgUnits=ImspTQPwCqd&program=lxAQ7Zs9VYR'
40+
const link = locationAssign(url)
41+
expect(link.download).toEqual(
42+
'trackedEntities_2024-03-12_ouMode_SELECTED__orgUnits_ImspTQPwCqd__program_lxAQ7Zs9VYR.csv'
43+
)
44+
})
45+
46+
it('should create url with events zip', () => {
47+
const url =
48+
'https://debug.dhis2.org/dev/api/tracker/events.json.zip?links=false&paging=false&totalPages=false&orgUnit=fwH9ipvXde9&program=VBqh0ynB2wv&includeDeleted=false&dataElementIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=events.json.zip&occurredAfter=2023-12-12&occurredBefore=2024-03-12&ouMode=CHILDREN&format=json'
49+
const link = locationAssign(url)
50+
expect(link.download).toEqual(
51+
'events_2024-03-12_orgUnit_fwH9ipvXde9__program_VBqh0ynB2wv__ouMode_CHILDREN.json.zip'
52+
)
53+
})
54+
55+
it('should create url with events gzip', () => {
56+
const url =
57+
'https://debug.dhis2.org/dev/api/tracker/events.json.gz?links=false&paging=false&totalPages=false&orgUnit=ImspTQPwCqd&program=lxAQ7Zs9VYR&includeDeleted=false&dataElementIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=events.json.gz&occurredAfter=2023-12-12&occurredBefore=2024-03-12&ouMode=SELECTED&format=json'
58+
const link = locationAssign(url)
59+
expect(link.download).toEqual(
60+
'events_2024-03-12_orgUnit_ImspTQPwCqd__program_lxAQ7Zs9VYR__ouMode_SELECTED.json.gz'
61+
)
62+
})
63+
it('should work with relative URLs when bundled in DHIS2', () => {
64+
Object.defineProperty(global.document, 'baseURI', {
65+
value: 'http://localhost:8080/dhis-web-import-export/index.html#/export/tei',
66+
})
67+
const url =
68+
'../api/tracker/trackedEntities.json?ouMode=SELECTED&includeDeleted=false&dataElementIdScheme=UID&eventIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=trackedEntities.json&paging=false&totalPages=false&orgUnits=ImspTQPwCqd&program=lxAQ7Zs9VYR'
69+
const link = locationAssign(url)
70+
expect(link.download).toEqual(
71+
'trackedEntities_2024-03-12_ouMode_SELECTED__orgUnits_ImspTQPwCqd__program_lxAQ7Zs9VYR.json'
72+
)
73+
})
74+
it('should work with relative URLs when bundled in DHIS2 for zip', () => {
75+
Object.defineProperty(global.document, 'baseURI', {
76+
value: 'http://localhost:8080/dhis-web-import-export/index.html#/export/tei',
77+
})
78+
const url =
79+
'../api/tracker/events.json.zip?links=false&paging=false&totalPages=false&orgUnit=ImspTQPwCqd&program=lxAQ7Zs9VYR&includeDeleted=false&dataElementIdScheme=UID&orgUnitIdScheme=UID&idScheme=UID&attachment=events.json.zip&occurredAfter=2023-12-12&occurredBefore=2024-03-12&ouMode=SELECTED&format=json'
80+
const link = locationAssign(url)
81+
expect(link.download).toEqual(
82+
'trackedEntities_2024-03-12_ouMode_SELECTED__orgUnits_ImspTQPwCqd__program_lxAQ7Zs9VYR.json'
83+
)
84+
})
85+
})

0 commit comments

Comments
 (0)