Skip to content

Commit

Permalink
chore: encode the search state into the URL
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviareichl committed Jul 16, 2024
1 parent f3d7d6e commit 72b735b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
6 changes: 3 additions & 3 deletions components/search-form.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts" setup>
export interface SearchFormData {
search: string;
query: string;
}
const props = defineProps<SearchFormData>();
Expand All @@ -14,7 +14,7 @@ function onSubmit(event: Event) {
const formData = new FormData(element);
emit("submit", {
search: formData.get("q") as string,
query: formData.get("q") as string,
});
}
Expand All @@ -33,7 +33,7 @@ const searchLabelId = "search-field";
<div>
<Input
:id="searchLabelId"
:default-value="props.search"
:default-value="props.query"
name="q"
placeholder="Suche"
type="search"
Expand Down
28 changes: 26 additions & 2 deletions pages/search.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<script lang="ts" setup>
import * as v from "valibot";
import type { SearchFormData } from "@/components/search-form.vue";
import {
Pagination,
PaginationEllipsis,
Expand All @@ -14,6 +17,7 @@ defineRouteRules({
prerender: true,
});
const router = useRouter();
const t = useTranslations();
usePageMetadata({
Expand All @@ -24,10 +28,29 @@ function onUpdatePage(newPage: number) {
offset.value = (newPage - 1) * limit;
}
const searchFiltersSchema = v.object({
query: v.fallback(v.string(), ""),
});
const offset = ref(0);
const limit = 20;
const searchstring = ref("");
type SearchFilters = v.InferOutput<typeof searchFiltersSchema>;
function onChangeSearchInput(values: SearchFormData) {
setSearchFilters(values);
}
function setSearchFilters(query: Partial<SearchFilters>) {
if (query.query === "") {
delete query.query;
}
void router.push({ query });
document.body.scrollTo(0, 0);
}
const { data } = useGetSearchResults(
computed(() => {
return {
Expand All @@ -44,10 +67,11 @@ const { data } = useGetSearchResults(
<h1 class="sr-only">{{ t("SearchPage.title") }}</h1>
<div class="grid h-full grid-cols-[1fr_3fr]">
<SearchForm
search=""
query=""
@submit="
(values) => {
searchstring = values.search;
onChangeSearchInput(values);
searchstring = values.query;
}
"
/>
Expand Down

0 comments on commit 72b735b

Please sign in to comment.