Skip to content

Commit

Permalink
Fixed search skipping the first pagination
Browse files Browse the repository at this point in the history
Fixed a bug where 1 pagination page would be skipped as the search counts from 0 and the app requests for p = 1.
  • Loading branch information
UltimateDoge5 committed Apr 14, 2023
1 parent 0b086d2 commit 0772da6
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/pages/api/cpu/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
// p - page
let { q, p } = req.query as { q: string; p: string };

p = parseInt(p) < 0 ? "0" : p;
if (p === undefined) p = "1";
const pInt = (parseInt(p) < 1 ? 1 : parseInt(p) - 1); //The app starts at p = 1, but we start at 0
if (q === undefined) q = "";

// Get 5 cpus matching the query
const names = INTEL_PRODUCTS
.concat(AMD_PRODUCTS.map((p) => p.name))
.filter((cpu) => cpu.toLowerCase().includes(q.toLowerCase().trim())).map((cpu) => cpu.toLowerCase());
.filter((cpu) => new RegExp(q.trim(), "i").test(cpu));

const remainingItems = Math.max(names.length - parseInt(p) * 5, 0);
const remainingItems = Math.max(names.length - Math.max(pInt, 1) * 5, 0);

res.status(200).json({
names: names.slice(parseInt(p) * 5, parseInt(p) * 5 + 5).map((name) => ({
names: names.slice(pInt * 5, pInt * 5 + 5).map((name) => ({
model: beautifyNames(name),
manufacturer: name.includes("amd") ? "amd" : "intel",
})),
Expand Down

1 comment on commit 0772da6

@vercel
Copy link

@vercel vercel bot commented on 0772da6 Apr 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.