mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-18 07:56:57 +00:00
search site
This commit is contained in:
parent
8f9c560706
commit
911eec0c90
@ -1,15 +1,18 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import prisma from "@/app/prisma";
|
import prisma from "@/app/prisma";
|
||||||
|
import Fuse from "fuse.js";
|
||||||
|
|
||||||
interface QueryParams {
|
interface QueryParams {
|
||||||
currentPage: number;
|
currentPage: number;
|
||||||
itemPerPage: number;
|
itemPerPage: number;
|
||||||
|
search: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const { searchParams } = request.nextUrl;
|
const { searchParams } = request.nextUrl;
|
||||||
const currentPage = Number(searchParams.get("currentPage")) || 1;
|
const currentPage = Number(searchParams.get("currentPage")) || 1;
|
||||||
const itemPerPage = Number(searchParams.get("itemPerPage")) || 1;
|
const itemPerPage = Number(searchParams.get("itemPerPage")) || 1;
|
||||||
|
const search = searchParams.get("search") || "";
|
||||||
|
|
||||||
const skip = (currentPage - 1) * itemPerPage;
|
const skip = (currentPage - 1) * itemPerPage;
|
||||||
const take = itemPerPage;
|
const take = itemPerPage;
|
||||||
@ -18,8 +21,29 @@ export async function GET(request: NextRequest) {
|
|||||||
const sites = await prisma.site.findMany({
|
const sites = await prisma.site.findMany({
|
||||||
skip,
|
skip,
|
||||||
take,
|
take,
|
||||||
|
where: {
|
||||||
|
name: {
|
||||||
|
contains: search,
|
||||||
|
mode: "insensitive",
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
contains: search,
|
||||||
|
mode: "insensitive",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const total = await prisma.site.count({
|
||||||
|
where: {
|
||||||
|
name: {
|
||||||
|
contains: search,
|
||||||
|
mode: "insensitive",
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
contains: search,
|
||||||
|
mode: "insensitive",
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const total = await prisma.site.count();
|
|
||||||
return NextResponse.json({ sites, total, currentPage, itemPerPage }, { status: 200 });
|
return NextResponse.json({ sites, total, currentPage, itemPerPage }, { status: 200 });
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
||||||
|
|||||||
@ -18,6 +18,7 @@ export default function SitesPage({ username, name }: SitesPageProps) {
|
|||||||
const [currentPage, setCurrentPage] = useState(1);
|
const [currentPage, setCurrentPage] = useState(1);
|
||||||
const [itemPerPage, setItemPerPage] = useState(5);
|
const [itemPerPage, setItemPerPage] = useState(5);
|
||||||
const [total, setTotal] = useState(0);
|
const [total, setTotal] = useState(0);
|
||||||
|
const [search, setSearch] = useState("");
|
||||||
|
|
||||||
const handlePageChange = (page: number) => {
|
const handlePageChange = (page: number) => {
|
||||||
setCurrentPage(page);
|
setCurrentPage(page);
|
||||||
@ -27,12 +28,13 @@ export default function SitesPage({ username, name }: SitesPageProps) {
|
|||||||
params: {
|
params: {
|
||||||
currentPage,
|
currentPage,
|
||||||
itemPerPage,
|
itemPerPage,
|
||||||
|
search,
|
||||||
},
|
},
|
||||||
}).then((response) => {
|
}).then((response) => {
|
||||||
setSites(response.data.sites);
|
setSites(response.data.sites);
|
||||||
setTotal(response.data.total);
|
setTotal(response.data.total);
|
||||||
});
|
});
|
||||||
}, [currentPage, itemPerPage]);
|
}, [currentPage, itemPerPage, search]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Sidebar
|
<Sidebar
|
||||||
@ -54,7 +56,7 @@ export default function SitesPage({ username, name }: SitesPageProps) {
|
|||||||
<AddSite />
|
<AddSite />
|
||||||
|
|
||||||
<div className="flex gap-2 items-center pt-4">
|
<div className="flex gap-2 items-center pt-4">
|
||||||
<input type="text" placeholder="Search..." className="input input-bordered w-full" />
|
<input type="text" placeholder="Search..." className="input input-bordered w-full" onChange={(e) => setSearch(e.target.value)} />
|
||||||
<select defaultValue="Pick a font" className="select w-24" onChange={(e) => setItemPerPage(Number(e.target.value))}>
|
<select defaultValue="Pick a font" className="select w-24" onChange={(e) => setItemPerPage(Number(e.target.value))}>
|
||||||
<option disabled={true}>Layout</option>
|
<option disabled={true}>Layout</option>
|
||||||
<option value={5}>List</option>
|
<option value={5}>List</option>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user