Skip to content

Commit 6e5c9e5

Browse files
Fishamble feature #393 article search (#449)
* Implement search feature * made search bar responsive
1 parent 8fede41 commit 6e5c9e5

4 files changed

Lines changed: 62 additions & 5 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
interface Props {
3+
searchTerm: string;
4+
setSearchTerm: React.Dispatch<React.SetStateAction<string>>;
5+
}
6+
7+
const SearchBar = (props: Props) => {
8+
const { searchTerm, setSearchTerm } = props;
9+
10+
console.log(searchTerm);
11+
12+
return (
13+
<div className='flex grow justify-end'>
14+
<label htmlFor="searchInput" aria-label="Enter search term"></label>
15+
<input
16+
onChange={(e) => setSearchTerm(e.target.value)}
17+
type="text"
18+
value={searchTerm}
19+
placeholder="search"
20+
id="searchInput"
21+
name="searchInput"
22+
className="max-w-56 w-56 max-[500px]:w-full sm:text-sm sm:leading-6 ml-2 mr-2 mt-2 block rounded-md border-0 p-1.5 ring-1 ring-inset ring-neutral-300 focus:ring-2 focus:ring-pink-600"
23+
/>
24+
</div>
25+
);
26+
};
27+
28+
export default SearchBar;

pages/articles/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Children, Fragment, useEffect } from "react";
1+
import { Children, Fragment, useEffect, useState } from "react";
22
import Head from "next/head";
33
import { TagIcon } from "@heroicons/react/outline";
44
import ArticlePreview from "../../components/ArticlePreview/ArticlePreview";
@@ -10,6 +10,7 @@ import { useRouter } from "next/router";
1010
import Link from "next/link";
1111
import Image from "next/image";
1212
import challenge from "../../public/images/announcements/challenge.png";
13+
import SearchBar from "../../components/ArticleSearch/SearchBar";
1314

1415
// Needs to be added to DB but testing with hardcoding
1516
const tagsToShow = [
@@ -29,7 +30,7 @@ const ArticlesPage = () => {
2930

3031
const { filter, tag: dirtyTag } = router.query;
3132
const tag = typeof dirtyTag === "string" ? dirtyTag.toLowerCase() : null;
32-
33+
const [searchTerm, setSearchTerm] = useState("");
3334
type Filter = "newest" | "oldest" | "top";
3435
const filters: Filter[] = ["newest", "oldest", "top"];
3536

@@ -45,7 +46,7 @@ const ArticlesPage = () => {
4546

4647
const { status, data, isFetchingNextPage, fetchNextPage, hasNextPage } =
4748
trpc.post.all.useInfiniteQuery(
48-
{ limit: 15, sort: selectedSortFilter, tag },
49+
{ limit: 15, sort: selectedSortFilter, tag, searchTerm },
4950
{
5051
getNextPageParam: (lastPage) => lastPage.nextCursor,
5152
}
@@ -97,7 +98,8 @@ const ArticlesPage = () => {
9798
"Articles"
9899
)}
99100
</h1>
100-
<div>
101+
<SearchBar searchTerm={searchTerm} setSearchTerm={setSearchTerm}/>
102+
<div className='min-w-fit'>
101103
<label htmlFor="filter" className="sr-only">
102104
Location
103105
</label>

schema/post.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const GetPostsSchema = z.object({
6161
cursor: z.string().nullish(),
6262
sort: z.enum(["newest", "oldest", "top"]),
6363
tag: z.string().nullish(),
64+
searchTerm: z.string().nullish(),
6465
});
6566

6667
export type SavePostInput = z.TypeOf<typeof SavePostSchema>;

server/trpc/router/post.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export const postRouter = router({
251251
all: publicProcedure.input(GetPostsSchema).query(async ({ ctx, input }) => {
252252
const userId = ctx.session?.user?.id;
253253
const limit = input?.limit ?? 50;
254-
const { cursor, sort, tag } = input;
254+
const { cursor, sort, tag, searchTerm } = input;
255255

256256
const orderMapping = {
257257
newest: {
@@ -287,6 +287,32 @@ export const postRouter = router({
287287
},
288288
}
289289
: {}),
290+
...(searchTerm
291+
? {
292+
OR: [
293+
{
294+
user: {
295+
name: {
296+
contains: searchTerm || "",
297+
mode: "insensitive",
298+
},
299+
},
300+
},
301+
{
302+
title: {
303+
contains: searchTerm || "",
304+
mode: "insensitive",
305+
},
306+
},
307+
{
308+
excerpt: {
309+
contains: searchTerm || "",
310+
mode: "insensitive",
311+
},
312+
},
313+
],
314+
}
315+
: {}),
290316
},
291317
select: {
292318
id: true,

0 commit comments

Comments
 (0)