chore: WIP search working

This commit is contained in:
Corbin Crutchley
2022-09-25 05:51:52 -07:00
parent ec63266f10
commit 4256a2a474
2 changed files with 42 additions and 22 deletions

View File

@@ -19,5 +19,10 @@ const fuse = new Fuse(
); );
export default async (req: VercelRequest, res: VercelResponse) => { export default async (req: VercelRequest, res: VercelResponse) => {
res.send(fuse.search(req.query.query)); // TODO: `pickdeep` only required fields
const searchStr = req?.query?.query as string;
if (!searchStr) return [];
if (Array.isArray(searchStr)) return [];
const items = fuse.search(searchStr).map((item) => item.item);
res.send(items);
}; };

View File

@@ -22,7 +22,8 @@ const { unicornProfilePicMap } = Astro.props as FilterSearchBarProps;
window.unicornProfilePicMap || unicornProfilePicMap; window.unicornProfilePicMap || unicornProfilePicMap;
</script> </script>
<script> <script>
import { render, createElement } from "preact"; import { render, createElement, Fragment } from "preact";
import { PostInfo } from "types/PostInfo";
import { PostCard } from "../../components/post-card/post-card"; import { PostCard } from "../../components/post-card/post-card";
const searchInput: HTMLElement = document.querySelector("#search-input"); const searchInput: HTMLElement = document.querySelector("#search-input");
@@ -39,6 +40,9 @@ const { unicornProfilePicMap } = Astro.props as FilterSearchBarProps;
const initDisplayVal = postListContainer.style.display; const initDisplayVal = postListContainer.style.display;
let loadingEl: HTMLElement | undefined;
let abortController: AbortController | undefined;
searchInput.addEventListener( searchInput.addEventListener(
"input", "input",
(e: InputEvent & { target: HTMLInputElement }) => { (e: InputEvent & { target: HTMLInputElement }) => {
@@ -52,29 +56,40 @@ const { unicornProfilePicMap } = Astro.props as FilterSearchBarProps;
searchPostListContainer.style.display = "none"; searchPostListContainer.style.display = "none";
return; return;
} }
postListContainer.style.display = "none"; postListContainer.style.display = "none";
searchPostListContainer.style.display = "block"; searchPostListContainer.style.display = "block";
// searchPostListContainer.innerText = val;
const unicornProfilePicMap = (window as any).unicornProfilePicMap || []; const unicornProfilePicMap = (window as any).unicornProfilePicMap || [];
const FilledPostCard = createElement(PostCard, { loadingEl && loadingEl.remove();
unicornProfilePicMap, loadingEl = document.createElement("p");
post: { loadingEl.innerText = "Loading...";
published: new Date().toUTCString(), searchPostListContainer.append(loadingEl);
slug: "",
title: val, if (abortController) {
authorsMeta: [ abortController.abort();
{ abortController = undefined;
name: "Corbin Crutchley", }
id: "crutchcorn", abortController = new AbortController();
color: "", fetch(`/api/search?query=${val}`, { signal: abortController.signal })
}, .then((res) => res.json())
], .then((serverVal: PostInfo[]) => {
tags: [], // TODO: Debounce to avoid server kill
description: "Hello, world!", const FilledPostCard = createElement(
excerpt: "", Fragment,
}, {},
}); serverVal.map((post) =>
render(FilledPostCard, searchPostList); createElement(PostCard, {
unicornProfilePicMap,
post,
})
)
);
render(FilledPostCard, searchPostList);
loadingEl && loadingEl.remove();
loadingEl = undefined;
abortController = undefined;
});
} }
); );
</script> </script>