Skip to content

Commit

Permalink
refactor: address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros committed Jun 3, 2024
1 parent 400e2b1 commit ca7d26b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
22 changes: 10 additions & 12 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,30 +189,28 @@ const uploadFile = ({
}

// call stub function if available
const locationAssign = (relativeUrl) => {
const locationAssign = (url) => {
if (window.locationAssign) {
window.locationAssign(relativeUrl)
window.locationAssign(url)
} else {
try {
const url = relativeUrl.startsWith('..')
? new URL(relativeUrl, document.baseURI).href
: relativeUrl
const downloadUrl = url.startsWith('..')
? new URL(url, document.baseURI).href
: url

const urlFilePart = new URL(url).pathname.split('/').pop()
const [, file, extension] = urlFilePart.match(/(^[^.]+)(\..+$)/)

const downloadedFileName = `${file}${extension}`
const urlFilePart = new URL(downloadUrl).pathname.split('/').pop()
const [, filename] = urlFilePart.match(/(^[^.]+)(\..+$)/)

const link = document.createElement('a')
link.href = url
link.download = downloadedFileName
link.href = downloadUrl
link.download = filename
link.target = '_blank'
link.click()

return link
} catch (err) {
console.error(err)
window.open(relativeUrl, '_blank')
window.open(url, '_blank')
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/utils/helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ describe('locationAssign', () => {
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'
const link = locationAssign(url)
expect(link.download).toEqual('trackedEntities.json')
expect(link.download).toEqual('trackedEntities')
})
it('should create url with orgUnits', () => {
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'
const link = locationAssign(url)
expect(link.download).toEqual('trackedEntities.json')
expect(link.download).toEqual('trackedEntities')
})
it('should create url with tracked entities', () => {
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'
const link = locationAssign(url)
expect(link.download).toEqual('trackedEntities.json')
expect(link.download).toEqual('trackedEntities')
})
it('should create url with CSV', () => {
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'
const link = locationAssign(url)
expect(link.download).toEqual('trackedEntities.csv')
expect(link.download).toEqual('trackedEntities')
})

it('should create url with events zip', () => {
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'
const link = locationAssign(url)
expect(link.download).toEqual('events.json.zip')
expect(link.download).toEqual('events')
})

it('should create url with events gzip', () => {
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'
const link = locationAssign(url)
expect(link.download).toEqual('events.json.gz')
expect(link.download).toEqual('events')
})
it('should work with relative URLs when bundled in DHIS2', () => {
Object.defineProperty(global.document, 'baseURI', {
Expand All @@ -46,7 +46,7 @@ describe('locationAssign', () => {
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'
const link = locationAssign(url)
expect(link.download).toEqual('trackedEntities.json')
expect(link.download).toEqual('trackedEntities')
})
it('should work with relative URLs when bundled in DHIS2 for zip', () => {
Object.defineProperty(global.document, 'baseURI', {
Expand All @@ -55,6 +55,6 @@ describe('locationAssign', () => {
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'
const link = locationAssign(url)
expect(link.download).toEqual('events.json.zip')
expect(link.download).toEqual('events')
})
})

0 comments on commit ca7d26b

Please sign in to comment.