web: simplify link generation for search
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
"use client";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useDebouncedCallback } from "use-debounce";
|
||||
import { PageHeader } from "../general/PageHeader";
|
||||
import { useSearchParams, usePathname, useRouter } from "next/navigation";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { getSearchPath } from "@/lib/common";
|
||||
import styles from './searchContainer.module.scss';
|
||||
import { Icon } from "../general/Icon";
|
||||
@@ -14,12 +15,15 @@ export function SearchContainer({
|
||||
query: string;
|
||||
results: any;
|
||||
}) {
|
||||
const searchParams = useSearchParams();
|
||||
const pathname = usePathname();
|
||||
const { replace } = useRouter();
|
||||
const [inputValue, setInputValue] = useState(query);
|
||||
|
||||
const onQueryChange = useDebouncedCallback((query) => {
|
||||
replace(getSearchPath(query));
|
||||
useEffect(() => {
|
||||
setInputValue(query);
|
||||
}, [query]);
|
||||
|
||||
const pushQuery = useDebouncedCallback((next: string) => {
|
||||
replace(getSearchPath(next));
|
||||
}, 500);
|
||||
|
||||
return (
|
||||
@@ -30,9 +34,10 @@ export function SearchContainer({
|
||||
name="query"
|
||||
type="text"
|
||||
autoFocus
|
||||
defaultValue={query ?? ""}
|
||||
value={inputValue}
|
||||
onChange={(e) => {
|
||||
onQueryChange(e.target.value);
|
||||
setInputValue(e.target.value);
|
||||
pushQuery(e.target.value);
|
||||
}}
|
||||
/>
|
||||
<div className={styles.searchIcon}>
|
||||
@@ -49,22 +54,7 @@ function capitalizeFirstLetter(s: string) {
|
||||
}
|
||||
|
||||
function linkTo(page: any): string | null {
|
||||
if (page.__typename === "EventPage") {
|
||||
return `/arrangementer/${page.slug}`;
|
||||
}
|
||||
if (page.__typename === "NewsPage") {
|
||||
return `/aktuelt/${page.slug}`;
|
||||
}
|
||||
if (page.__typename === "AssociationPage") {
|
||||
return `/foreninger/${page.slug}`;
|
||||
}
|
||||
if (page.__typename === "GenericPage") {
|
||||
return `/{page.slug}`;
|
||||
}
|
||||
if (page.__typename === "VenuePage") {
|
||||
return `/lokaler/${page.slug}`;
|
||||
}
|
||||
return null;
|
||||
return page?.url ?? null;
|
||||
}
|
||||
|
||||
const PAGE_TYPES: Record<string, string> = {
|
||||
|
||||
Reference in New Issue
Block a user