Skip to content

Commit

Permalink
feat: add search 'sort' param to URL (closes #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinstadler committed Sep 23, 2024
1 parent 604d8e2 commit 2a2acaa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
15 changes: 8 additions & 7 deletions app/search/instantsearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type RefinementState = {

interface SearchState {
q?: string;
page?: number;
sort?: string;
}

type RouteState = RefinementState & SearchState;
Expand All @@ -70,7 +70,7 @@ const stateMapping: StateMapping<UiState, RouteState> = {
const indexUiState = uiState[collectionName]!;
const route = {} as RouteState;
route.q = indexUiState.query;
route.page = indexUiState.page;
route.sort = indexUiState.sortBy?.split("/").at(-1);
if (indexUiState.refinementList) {
for (const [field, values] of Object.entries(indexUiState.refinementList)) {
const queryarg = Object.entries(queryArgToRefinementField).find(([_k, v]) => {
Expand All @@ -88,6 +88,7 @@ const stateMapping: StateMapping<UiState, RouteState> = {
query: routeState.q,
page: routeState.page,
refinementList: {},
sortBy: routeState.sort ? `${collectionName}/sort/${routeState.sort}` : undefined,
},
} as UiState;
for (const queryarg in queryArgToRefinementField) {
Expand Down Expand Up @@ -151,14 +152,14 @@ export function InstantSearch() {
</div>
<div>
<div className="flex place-content-between">
<SearchBox placeholder={t("filter_publications")} />
<SearchBox placeholder={t("query_placeholder")} />
<div>
sort by
{t("sort_by")}
<SortBy
items={[
{ label: "title", value: `${collectionName}/sort/title:asc` },
{ label: "publication (newest first)", value: `${collectionName}/sort/year:desc` },
{ label: "publication (oldest first)", value: `${collectionName}/sort/year:asc` },
{ label: t("sort.year:desc"), value: `${collectionName}/sort/year:desc` },
{ label: t("sort.year:asc"), value: `${collectionName}/sort/year:asc` },
{ label: t("sort.title"), value: `${collectionName}/sort/title:asc` },
]}
/>
</div>
Expand Down
17 changes: 13 additions & 4 deletions messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,19 @@
},
"SearchPage": {
"filter": "filter",
"filter_publications": "filter publications",
"language": "languages",
"work": "works",
"translator": "translators"
"filter_by": {
"category": "categories",
"language": "languages",
"translator": "translators",
"work": "works"
},
"query_placeholder": "filter publications",
"sort_by": "sort by",
"sort": {
"year:asc": "oldest to newest",
"year:desc": "newest to oldest",
"title": "title"
}
},
"TranslatorPage": {
"published_in": "published in"
Expand Down

0 comments on commit 2a2acaa

Please sign in to comment.