Skip to content

Commit

Permalink
Pass sort direction on select change
Browse files Browse the repository at this point in the history
  • Loading branch information
dgcohen committed Nov 7, 2023
1 parent 1e6d00b commit 72448ff
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
9 changes: 7 additions & 2 deletions pages/search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export default function Search({ results }) {
const drbResults = mapWorksToDRBResults(drbWorks)

const handleSortChange = async (e) => {
const newQuery = getQueryString({ ...searchParams, sortBy: e.target.value })
const [sortBy, order] = e.target.value.split("_")
const newQuery = getQueryString({ ...searchParams, sortBy, order })
await replace(newQuery)
}

Expand All @@ -64,7 +65,11 @@ export default function Search({ results }) {
labelText="Sort by"
mb="l"
onChange={handleSortChange}
value={searchParams.sortBy}
value={
searchParams.order
? `${searchParams.sortBy}_${searchParams.order}`
: searchParams.sortBy
}
>
{Object.keys(sortOptions).map((key) => (
<option value={key} key={`sort-by-${key}`}>
Expand Down
10 changes: 3 additions & 7 deletions src/types/searchTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface SearchParams {
q?: string
field?: string
sortBy?: SortKey
order?: string
order?: SortOrder
filters?: SearchFilters
contributor?: string
title?: string
Expand All @@ -42,12 +42,8 @@ export interface SearchParams {
identifiers?: Identifiers
}

export type SortKey =
| "relevance"
| "title_asc"
| "title_desc"
| "date_asc"
| "date_desc"
export type SortKey = "relevance" | "title" | "date"
export type SortOrder = "asc" | "desc"

type SearchFormField = { value: string }

Expand Down
2 changes: 1 addition & 1 deletion src/utils/drbUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function mapSearchParamsToDRBQueryParams(params: SearchParams): DRBQueryParams {
}

if (sortBy) {
const sortDirection = order || (sortBy === "date_desc" ? "desc" : "asc")
const sortDirection = order || (sortBy === "date" ? "desc" : "asc")
drbQuery.sort = [sortBy, sortDirection].join(":")
}

Expand Down
5 changes: 3 additions & 2 deletions src/utils/searchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
Identifiers,
SearchResultsElement,
SortKey,
SortOrder,
} from "../types/searchTypes"
import SearchResultsBib from "../models/SearchResultsBib"

Expand Down Expand Up @@ -170,7 +171,7 @@ export function mapElementsToSearchResultsBibs(
* sortOptions
* The allowed keys for the sort field and their respective labels
*/
export const sortOptions: Record<SortKey, string> = {
export const sortOptions: Record<string, string> = {
relevance: "Relevance",
title_asc: "Title (A - Z)",
title_desc: "Title (Z - A)",
Expand Down Expand Up @@ -205,7 +206,7 @@ export function mapQueryToSearchParams({
title,
subject,
sortBy: sort as SortKey,
order: sort_direction,
order: sort_direction as SortOrder,
filters,
identifiers: {
issn,
Expand Down

0 comments on commit 72448ff

Please sign in to comment.