Skip to content

Commit

Permalink
Merge pull request #1347 from research-software-directory/fix-search-…
Browse files Browse the repository at this point in the history
…param

fix: search for C++
  • Loading branch information
dmijatovic authored Nov 18, 2024
2 parents 70029ae + e38ba6d commit 9f4f4c7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion frontend/components/charts/d3LineChart/formatData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function formatUnixDateData(data: Data) {
}

export function prepareDataForSoftwarePage(data: Data) {
console.log('prepareDataForSoftwarePage...',data)
// console.log('prepareDataForSoftwarePage...',data)
// format unix time in seconds to ms for js
const {lineData,lastUpdateInMs} = formatUnixDateData(data)
// calculate total number of commits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function useProjectOverviewParams() {
// use value from user settings if none provided
params['rows'] = rsd_page_rows
}
// construct url with all query params
// construct and encode url with all query params
const url = ssrProjectsUrl(params)
return url
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function useSoftwareOverviewParams() {
}

function createUrl(key: string, value: string | string[]) {

// console.log('createUrl...',key,value, router.query)
const params: QueryParams = {
// take existing params from url (query)
...ssrSoftwareParams(router.query),
Expand All @@ -54,7 +54,7 @@ export default function useSoftwareOverviewParams() {
// use value from user settings if none provided
params['rows'] = rsd_page_rows
}
// construct url with all query params
// construct and encode url with all query params
const url = ssrViewUrl({
view: getCurrentPage(),
params: params
Expand Down
3 changes: 2 additions & 1 deletion frontend/pages/software/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ export default function SoftwareOverviewPage({
page={page}
renderItem={item => {
if (item.page !== null) {
const url = createUrl('page', item.page.toString())
return (
<Link href={createUrl('page', item.page.toString())}>
<Link href={url}>
<PaginationItem {...item}/>
</Link>
)
Expand Down
47 changes: 33 additions & 14 deletions frontend/utils/extractQueryParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ type BuildUrlQueryProps = EncodeQueryParamProps & {
query: string
}

export function encodeQueryValue(value: EncodeQueryValue,encodeString?:boolean) {
export function encodeQueryValue(value: EncodeQueryValue) {
try {
if (typeof value === 'string') {
if (encodeString) return encodeURIComponent(value)
return value
if (typeof value === 'string' || typeof value === 'number') {
return encodeURIComponent(value)
} else if (Array.isArray(value) === true && (value as any)?.length > 0) {
// arrays are stringified
return encodeURIComponent(JSON.stringify(value))
} else if (typeof value === 'number') {
return encodeURIComponent(value)
}
} catch (e: any) {
logger(`encodeQueryValue...${e.message}`, 'error')
Expand Down Expand Up @@ -83,11 +80,12 @@ export function decodeJsonParam(value: string|null, defaultValue:any) {

// decoding Query params back from encodedUrl
export function decodeQueryParam({query,param,castToType='string',defaultValue}:{
query: ParsedUrlQuery, param: string, castToType?: ('string' | 'number' | 'json-encoded'),
query: ParsedUrlQuery, param: string, castToType?: ('string' | 'number' | 'json-encoded'|'raw'),
defaultValue:any
}){
try{
if (query && query.hasOwnProperty(param)){
// debugger
const rawVal = query[param]
// if value is not "actionable" we return default value
if (typeof rawVal == 'undefined' || rawVal === '' || rawVal === null) return defaultValue
Expand All @@ -102,16 +100,26 @@ export function decodeQueryParam({query,param,castToType='string',defaultValue}:
logger(`decodeQueryParam: query param ${param} NOT a string. Returning defaultValue`, 'warn')
return defaultValue
case 'string':
return decodeURIComponent(rawVal?.toString())
if (typeof rawVal === 'string'){
return decodeURIComponent(rawVal)
}else{
return decodeURIComponent(rawVal?.toString())
}
case 'json-encoded':
if (typeof rawVal === 'string') {
const json = decodeJsonParam(rawVal,defaultValue)
return json
}
logger(`decodeQueryParam: query param ${param} NOT a string. Returning defaultValue`, 'warn')
return defaultValue
case 'raw':
return rawVal
default:
logger(`decodeQueryParam: castToType ${castToType} NOT supported. Returning defaultValue`, 'warn')
if (defaultValue){
logger(`decodeQueryParam: castToType ${castToType} NOT supported. Returning defaultValue`, 'warn')
return defaultValue
}
logger(`decodeQueryParam: castToType ${castToType} NOT supported. Returning rawValue`, 'warn')
return rawVal
}
}else{
Expand All @@ -120,8 +128,8 @@ export function decodeQueryParam({query,param,castToType='string',defaultValue}:
return defaultValue
}
}catch(e:any){
logger(`decodeQueryParam: ${e.description}`,'error')
// throw e
// console.log('decodeQueryParam...',query,param,castToType)
logger(`decodeQueryParam: ${e}`,'error')
return defaultValue
}
}
Expand Down Expand Up @@ -156,7 +164,10 @@ export function ssrSoftwareParams(query: ParsedUrlQuery): SoftwareParams {
query,
param: 'search',
defaultValue: null,
castToType:'string'
// search string is already decoded by next
// and decodeURIComponent fails when % is in the string to decode
// see this issue https://github.com/vercel/next.js/issues/10080
castToType:'raw'
})
const keywords:string[]|undefined = decodeQueryParam({
query,
Expand Down Expand Up @@ -213,7 +224,11 @@ export function ssrProjectsParams(query: ParsedUrlQuery) {
const search:string|null = decodeQueryParam({
query,
param: 'search',
defaultValue: null
defaultValue: null,
// search string is already decoded by next
// and decodeURIComponent fails when % is in the string to decode
// see this issue https://github.com/vercel/next.js/issues/10080
castToType:'raw'
})
const keywords:string[]|null = decodeQueryParam({
query,
Expand Down Expand Up @@ -278,7 +293,11 @@ export function ssrBasicParams(query: ParsedUrlQuery) {
const search = decodeQueryParam({
query,
param: 'search',
defaultValue: null
defaultValue: null,
// search string is already decoded by next
// and decodeURIComponent fails when % is in the string to decode
// see this issue https://github.com/vercel/next.js/issues/10080
castToType:'raw'
})
return {
search,
Expand Down

0 comments on commit 9f4f4c7

Please sign in to comment.