add: news search bar, application history
This commit is contained in:
+18
-7
@@ -5,13 +5,23 @@ import { getPages } from "@/lib/pages/repository";
|
||||
import { getCurrentUser } from "@/lib/user";
|
||||
import { Metadata } from "next";
|
||||
import Link from "next/link";
|
||||
import SearchBar from "./searchBar";
|
||||
|
||||
export const metadata: Metadata = { title: "News" };
|
||||
|
||||
export default async function News() {
|
||||
export default async function News({ searchParams }: { searchParams: Promise<{ q?: string }> }) {
|
||||
const { q } = await searchParams;
|
||||
const user = await getCurrentUser();
|
||||
const news = await getPages("NEWS", "PUBLISHED");
|
||||
|
||||
const query = q?.trim().toLowerCase() ?? "";
|
||||
|
||||
const filteredNews = query
|
||||
? news.filter((n) =>
|
||||
`${n.title.toLowerCase()} ${n.meta.tags?.join(" ").toLowerCase() ?? ""}`.includes(query)
|
||||
)
|
||||
: news;
|
||||
|
||||
const newsDrafts = await getPages("NEWS", "DRAFT");
|
||||
const pageDrafts = await getPages("PAGE", "DRAFT");
|
||||
|
||||
@@ -21,6 +31,7 @@ export default async function News() {
|
||||
<div className="flex flex-col p-4 md:p-16 gap-8 md:gap-16 mb-4 md:mb-0">
|
||||
<div className="flex flex-col gap-4 md:flex-row md:items-center justify-between">
|
||||
<h1>News</h1>
|
||||
<SearchBar />
|
||||
<div className="flex flex-row items-center gap-4">
|
||||
{user?.isAdmin && (
|
||||
<div className="flex flex-row items-center gap-4">
|
||||
@@ -31,12 +42,12 @@ export default async function News() {
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col gap-6">
|
||||
{
|
||||
news.length > 0 ? news.map((n) => (
|
||||
<NewsCard news={n} key={n.id} />
|
||||
)) : <h2 className="text-muted-foreground">There are no news yet...</h2>
|
||||
}
|
||||
{
|
||||
filteredNews.length > 0 ? filteredNews.map((n) => (
|
||||
<NewsCard news={n} key={n.id} />
|
||||
)) : <h2 className="text-muted-foreground">There are no news yet...</h2>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
"use client"
|
||||
|
||||
import { SearchIcon } from "lucide-react";
|
||||
import { InputGroup, InputGroupAddon, InputGroupInput } from "@/components/ui/input-group";
|
||||
import { Field } from "@/components/ui/field";
|
||||
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
||||
import { useState, useTransition } from "react";
|
||||
|
||||
export default function SearchBar() {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const [value, setValue] = useState(searchParams.get("q") ?? "");
|
||||
const [, startTransition] = useTransition();
|
||||
|
||||
function handleChange(next: string) {
|
||||
setValue(next);
|
||||
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
if (next) {
|
||||
params.set("q", next);
|
||||
} else {
|
||||
params.delete("q");
|
||||
}
|
||||
|
||||
startTransition(() => {
|
||||
router.replace(`${pathname}?${params.toString()}`, { scroll: false });
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<Field className="max-w-sm">
|
||||
<InputGroup>
|
||||
<InputGroupInput
|
||||
placeholder="Search..."
|
||||
value={value}
|
||||
onChange={(e) => handleChange(e.target.value)}
|
||||
/>
|
||||
<InputGroupAddon align="inline-start">
|
||||
<SearchIcon className="text-muted-foreground" />
|
||||
</InputGroupAddon>
|
||||
</InputGroup>
|
||||
</Field>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user