Skip to content

Commit

Permalink
Display search api results
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansive committed Dec 21, 2023
1 parent 6dc9a74 commit f187929
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/app/api/search/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export async function GET(request: NextRequest) {
fuzziness: 0,
});

return NextResponse.json({ results });
return NextResponse.json(results);
}
14 changes: 8 additions & 6 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import SearchBar from "@/components/SearchBar";
import SearchResultList from "@/components/SearchResultList";
import Spinner from "@/components/Spinner";

import { Book, BOOKS } from "@/data/book-data";
import { Book } from "@/data/book-data";

import styles from "./page.module.css";

Expand All @@ -22,11 +22,13 @@ export default function Home() {

setStatus("loading");

setTimeout(() => {
const nextSearchResults = BOOKS.filter(({ title }) =>
title.includes(searchTerm)
);
setSearchResults(nextSearchResults);
setTimeout(async () => {
// const nextSearchResults = BOOKS.filter(({ title }) =>
// title.includes(searchTerm)
// );
const response = await fetch("/api/search?query=" + searchTerm);
const data = await response.json();
setSearchResults(data["records"]);
setStatus("success");
}, 1500);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type CardInterface = {
id: string;
title: string;
sources: string[];
size: number;
size: string;
extension: string;
};

Expand Down
9 changes: 8 additions & 1 deletion src/components/SearchResultList/SearchResultList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ function SearchResultList({ searchResults }: SearchResultListInterface) {
};
}, []);

// https://stackoverflow.com/a/48764436
function round(num: number, decimalPlaces = 0) {
var p = Math.pow(10, decimalPlaces);
var n = num * p * (1 + Number.EPSILON);
return Math.round(n) / p;
}

return (
<div ref={listRef} className={styles.searchResultList}>
{searchResults?.map((book) => (
Expand All @@ -39,7 +46,7 @@ function SearchResultList({ searchResults }: SearchResultListInterface) {
id={book.id}
title={book.title}
sources={book.sources}
size={book.size}
size={round(book.size / 1048576, 2).toFixed(1)} // to mebibytes
extension={book.extension}
/>
))}
Expand Down

0 comments on commit f187929

Please sign in to comment.