Skip to content

Commit 3820226

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

File tree

2 files changed

+101
-11
lines changed

2 files changed

+101
-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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 = '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'
15+
const link = locationAssign(url)
16+
expect(link.download).toEqual('trackedEntities_2024-03-12_ouMode_CAPTURE__program_lxAQ7Zs9VYR.json')
17+
18+
})
19+
it('should create url with orgUnits', () => {
20+
const url = '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';
21+
const link = locationAssign(url)
22+
expect(link.download).toEqual('trackedEntities_2024-03-12_ouMode_SELECTED__orgUnits_O6uvpzGd5pu,fdc6uOvgoji__program_kla3mAPgvCH.json')
23+
})
24+
it('should create url with tracked entities', () => {
25+
const url = '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';
26+
const link = locationAssign(url)
27+
expect(link.download).toEqual('trackedEntities_2024-03-12_ouMode_SELECTED__orgUnits_ImspTQPwCqd__trackedEntityType_bVkFYAvoUCP.json')
28+
})
29+
it('should create url with CSV', () => {
30+
const url = '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';
31+
const link = locationAssign(url)
32+
expect(link.download).toEqual('trackedEntities_2024-03-12_ouMode_SELECTED__orgUnits_ImspTQPwCqd__program_lxAQ7Zs9VYR.csv')
33+
})
34+
35+
it('should create url with events zip', () => {
36+
const url = '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';
37+
const link = locationAssign(url)
38+
expect(link.download).toEqual('events_2024-03-12_orgUnit_fwH9ipvXde9__program_VBqh0ynB2wv__ouMode_CHILDREN.json.zip')
39+
})
40+
41+
it('should create url with events gzip', () => {
42+
const url = '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';
43+
const link = locationAssign(url)
44+
expect(link.download).toEqual('events_2024-03-12_orgUnit_ImspTQPwCqd__program_lxAQ7Zs9VYR__ouMode_SELECTED.json.gz')
45+
})
46+
it('should work with relative URLs when bundled in DHIS2', () => {
47+
Object.defineProperty(global.document, 'baseURI', { value: 'http://localhost:8080/dhis-web-import-export/index.html#/export/tei' });
48+
const url = '../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'
49+
const link = locationAssign(url)
50+
expect(link.download).toEqual('trackedEntities_2024-03-12_ouMode_SELECTED__orgUnits_ImspTQPwCqd__program_lxAQ7Zs9VYR.json')
51+
})
52+
it('should work with relative URLs when bundled in DHIS2 for zip', () => {
53+
Object.defineProperty(global.document, 'baseURI', { value: 'http://localhost:8080/dhis-web-import-export/index.html#/export/tei' });
54+
const url ='../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'
55+
const link = locationAssign(url)
56+
expect(link.download).toEqual('trackedEntities_2024-03-12_ouMode_SELECTED__orgUnits_ImspTQPwCqd__program_lxAQ7Zs9VYR.json')
57+
})
58+
59+
})

0 commit comments

Comments
 (0)