Skip to content

Commit

Permalink
Fixing export link
Browse files Browse the repository at this point in the history
  • Loading branch information
Harald Wilhelmi committed Jan 9, 2025
1 parent 28e205e commit 8234484
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
39 changes: 39 additions & 0 deletions client/src/services/modification.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { test, expect } from 'vitest'
import { getModificationExportLink } from '@/services/modification'
import { type SearchBy } from '@/utils/search'

test.each<{ searchBy: SearchBy; apiBaseUrl: string; expected: string }>([
{
searchBy: 'Gene/Chrom',
apiBaseUrl: 'http://localhost:1234/',
expected: 'http://localhost:1234/modification/csv/gene?rnaType=WTS&taxaId=123'
},
{
searchBy: 'Modification',
apiBaseUrl: 'http://localhost:1234/',
expected: 'http://localhost:1234/modification/csv?rnaType=WTS&taxaId=123'
},
{
searchBy: 'Modification',
apiBaseUrl: '/',
expected: '/modification/csv?rnaType=WTS&taxaId=123'
}
])('getModificationExportLink(%s)', ({ searchBy, apiBaseUrl, expected }) => {
function getApiUrlCb(endpoint: string): string {
return `${apiBaseUrl}${endpoint}`
}

expect(
getModificationExportLink(
{
rna_type: 'WTS',
selections: [],
state: 'complete',
taxa: { taxa_id: 123, taxa_name: 'xxx', taxa_sname: 'x', domain: 'd', kingdom: 'k' },
searchBy
},
[],
getApiUrlCb
)
).toBe(expected)
})
11 changes: 6 additions & 5 deletions client/src/services/modification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,23 @@ function getGeneFilters(searchParameters: SearchParameters): string[] {

function getModificationExportLink(
searchParameters: SearchParameters,
sortMetas?: DataTableSortMeta[]
sortMetas?: DataTableSortMeta[],
getApiUrlCb: (uri: string) => string = getApiUrl
) {
const uri =
searchParameters.searchBy === 'Gene/Chrom' ? 'modification/csv/gene' : 'modification/csv'
const rawParams = getQueryParametersFromSearchParameters(searchParameters, sortMetas)
const url = new URL(getApiUrl(uri))
const params = new URLSearchParams()
for (const [k, v] of Object.entries(rawParams)) {
if (v) {
if (Array.isArray(v)) {
v.forEach((x) => url.searchParams.append(k, x))
v.forEach((x) => params.append(k, x))
} else {
url.searchParams.append(k, v)
params.append(k, v)
}
}
}
return url.toString()
return getApiUrlCb(uri) + '?' + params.toString()
}

function getSiteParams(modification: Modification): SiteParams {
Expand Down

0 comments on commit 8234484

Please sign in to comment.