Site Search Update

This commit is contained in:
headlesdev
2025-05-20 17:40:29 +02:00
parent cd014f7de0
commit d1a289c4fa
6 changed files with 76 additions and 42 deletions

View File

@@ -1,38 +1,39 @@
import { NextRequest, NextResponse } from "next/server";
import prisma from "@/app/prisma";
interface QueryParams {
currentPage: number;
itemPerPage: number;
search: string;
}
import Fuse from 'fuse.js';
import { Site } from "@/app/types";
export async function GET(request: NextRequest) {
const { searchParams } = request.nextUrl;
const currentPage = Number(searchParams.get("currentPage")) || 1;
const itemPerPage = Number(searchParams.get("itemPerPage")) || 1;
const itemPerPage = Number(searchParams.get("itemPerPage")) || 10;
const search = searchParams.get("search") || "";
const skip = (currentPage - 1) * itemPerPage;
const take = itemPerPage;
try {
const sites = await prisma.site.findMany({
skip,
take,
where: {
name: {
contains: search,
mode: "insensitive",
if (!search) {
const skip = (currentPage - 1) * itemPerPage;
const take = itemPerPage;
const sites = await prisma.site.findMany({
skip,
take,
orderBy: {
name: 'asc',
},
description: {
contains: search,
mode: "insensitive",
include: {
networks: {
orderBy: {
name: 'asc',
},
},
},
},
orderBy: {
name: 'asc',
},
});
const total = await prisma.site.count();
return NextResponse.json({ sites, total, currentPage, itemPerPage }, { status: 200 });
}
const allSites = await prisma.site.findMany({
include: {
networks: {
orderBy: {
@@ -41,20 +42,42 @@ export async function GET(request: NextRequest) {
},
},
});
const total = await prisma.site.count({
where: {
name: {
contains: search,
mode: "insensitive",
},
description: {
contains: search,
mode: "insensitive",
},
},
const fuseOptions = {
includeScore: true,
threshold: 0.4,
keys: ['name', 'description', 'networks.name']
};
const fuse = new Fuse(allSites, fuseOptions);
const searchResults = fuse.search(search);
const filteredSites = searchResults.map(result => {
const item = result.item;
return {
...item,
id: String(item.id),
networks: item.networks?.map(network => ({
...network,
id: String(network.id),
siteId: String(network.siteId)
})) || []
} as Site;
});
return NextResponse.json({ sites, total, currentPage, itemPerPage }, { status: 200 });
const total = filteredSites.length;
const startIndex = (currentPage - 1) * itemPerPage;
const endIndex = startIndex + itemPerPage;
const paginatedSites = filteredSites.slice(startIndex, endIndex);
return NextResponse.json({
sites: paginatedSites,
total,
currentPage,
itemPerPage
}, { status: 200 });
} catch (error: any) {
console.error("Search error:", error);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}

View File

@@ -42,7 +42,7 @@ export default function SetupPage() {
}
try {
// Create user
const response = await axios.post("/api/user/create", {
await axios.post("/api/user/create", {
email,
username,
name,
@@ -216,7 +216,7 @@ export default function SetupPage() {
<button className="btn btn-outline flex-1" onClick={handlePreviousStep}>
Back
</button>
<button className="btn btn-primary flex-1" onClick={handleComplete}>Next</button>
<button className="btn btn-primary flex-1" onClick={handleNextStep}>Next</button>
</div>
</div>
)}