mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-09 12:57:45 +00:00
chore: WIP add working search
This commit is contained in:
@@ -28,7 +28,7 @@ function PopupContents(props: Pick<PaginationProps, "page" | "getPageHref">) {
|
|||||||
class={style.popupInner}
|
class={style.popupInner}
|
||||||
onSubmit={(e) => {
|
onSubmit={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
location.href = `/page/${count}`;
|
location.href = props.getPageHref(count);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div class={style.popupTopArea}>
|
<div class={style.popupTopArea}>
|
||||||
|
|||||||
@@ -3,10 +3,14 @@ import {
|
|||||||
useEffect,
|
useEffect,
|
||||||
useMemo,
|
useMemo,
|
||||||
useReducer,
|
useReducer,
|
||||||
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
} from "preact/hooks";
|
} from "preact/hooks";
|
||||||
import { Pagination } from "components/pagination/pagination";
|
import { Pagination } from "components/pagination/pagination";
|
||||||
import { useDebounce } from "./use-debounce";
|
import { useDebounce } from "./use-debounce";
|
||||||
|
import { PostInfo } from "types/PostInfo";
|
||||||
|
import { PostCard } from "components/post-card/post-card";
|
||||||
|
import unicornProfilePicMap from "../../../public/unicorn-profile-pic-map";
|
||||||
|
|
||||||
const SEARCH_QUERY_KEY = "searchQuery";
|
const SEARCH_QUERY_KEY = "searchQuery";
|
||||||
const SEARCH_PAGE_KEY = "searchPage";
|
const SEARCH_PAGE_KEY = "searchPage";
|
||||||
@@ -39,15 +43,47 @@ export default function SearchPage() {
|
|||||||
[urlParams]
|
[urlParams]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const abortController = useRef<AbortController | undefined>();
|
||||||
|
|
||||||
|
const [searchResult, setSearchResult] = useState<{
|
||||||
|
state: "loading" | "error" | "success";
|
||||||
|
posts: PostInfo[];
|
||||||
|
totalPosts: number;
|
||||||
|
}>({ state: "loading", posts: [], totalPosts: 0 });
|
||||||
|
|
||||||
const fn = useDebounce(
|
const fn = useDebounce(
|
||||||
(val: string) => {
|
(val: string) => {
|
||||||
console.log("I AM SEARCHING FOR", val);
|
setSearchResult({ state: "loading", posts: [], totalPosts: 0 });
|
||||||
|
if (abortController.current) {
|
||||||
|
abortController.current.abort();
|
||||||
|
abortController.current = undefined;
|
||||||
|
}
|
||||||
|
abortController.current = new AbortController();
|
||||||
|
// plausible("search", { props: { searchVal: val } });
|
||||||
|
fetch(`/api/search?query=${val}`, {
|
||||||
|
signal: abortController.current.signal,
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then(async (serverVal: { posts: PostInfo[]; totalPosts: number }) => {
|
||||||
|
setSearchResult({
|
||||||
|
state: "success",
|
||||||
|
posts: serverVal.posts,
|
||||||
|
totalPosts: serverVal.totalPosts,
|
||||||
|
});
|
||||||
|
abortController.current = undefined;
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
if (err.name === "AbortError") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setSearchResult({ state: "error", posts: [], totalPosts: 0 });
|
||||||
|
abortController.current = undefined;
|
||||||
|
});
|
||||||
},
|
},
|
||||||
{ delay: 500, immediate: false }
|
{ delay: 500, immediate: false }
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log("RERENDER");
|
|
||||||
fn(searchVal);
|
fn(searchVal);
|
||||||
}, [searchVal]);
|
}, [searchVal]);
|
||||||
|
|
||||||
@@ -56,6 +92,16 @@ export default function SearchPage() {
|
|||||||
<form>
|
<form>
|
||||||
<input value={searchVal} onInput={(e) => search(e.target.value)} />
|
<input value={searchVal} onInput={(e) => search(e.target.value)} />
|
||||||
</form>
|
</form>
|
||||||
|
{searchResult.state === "loading" && <div>Loading...</div>}
|
||||||
|
<div>
|
||||||
|
{searchResult.posts.length} / {searchResult.totalPosts}
|
||||||
|
{searchResult.posts.map((post) => {
|
||||||
|
return (
|
||||||
|
<PostCard post={post} unicornProfilePicMap={unicornProfilePicMap} />
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
<div>{searchResult.state === "error" && <div>Error</div>}</div>
|
||||||
<Pagination
|
<Pagination
|
||||||
page={{
|
page={{
|
||||||
currentPage: page,
|
currentPage: page,
|
||||||
|
|||||||
Reference in New Issue
Block a user