diff --git a/app/api/games/route.js b/app/api/games/route.js index 3d6e111..6bf07c4 100644 --- a/app/api/games/route.js +++ b/app/api/games/route.js @@ -6,7 +6,12 @@ export async function GET(req) { await dbConnect(); try { - const gameData = await Games.find().lean(); + const { searchParams } = new URL(req.url); + const filter = searchParams.get("filter") || ""; + const query = { + category: { $regex: new RegExp(filter, "i") }, + }; + const gameData = await Games.find(query).lean(); return NextResponse.json(gameData, { status: 200 }); } catch (error) { @@ -17,3 +22,4 @@ export async function GET(req) { ); } } + diff --git a/app/games/page.js b/app/games/page.js index f150a78..c473977 100644 --- a/app/games/page.js +++ b/app/games/page.js @@ -1,20 +1,46 @@ import Image from "next/image"; import Link from "next/link"; import { games } from "./data/index"; +import Filter from "../../components/Filter"; + +export default async function page({searchParams}) { + + let filterUrl + if(searchParams?.filter){ + filterUrl = `${process.env.NEXT_PUBLIC_BASE_URL}/api/games?filter=${searchParams.filter}` + }else{ + filterUrl = `${process.env.NEXT_PUBLIC_BASE_URL}/api/games` + } + + const response = await fetch(`${filterUrl}`, { + method: "GET" + }); + + const gameData = await response.json() + + const filterss = [{ + name: 'FPS', value: 'fps' + }, { + name: 'Battle Royale', value: 'battle royale' + },{ + name: "None", value: 'none' + }] -export default function page() { return (
-
Games
+
+

Games

+ +
- {games.map((game, index) => ( + {gameData.map((game, index) => (
{game.name} { + const searchParams = useSearchParams(); + const router = useRouter(); + + const paramsFilter = searchParams.get("filter"); + + const handleUpdateParams = (value) => { + if (value !== "none") { + const newUrl = formUrlQuery({ + params: searchParams.toString(), + key: "filter", + value, + }); + router.push(newUrl, { scroll: false }); + } else { + router.push("/games"); + } + }; + + return ( +
+ +
+ ); +}; + +export default Filter; diff --git a/components/ui/select.jsx b/components/ui/select.jsx new file mode 100644 index 0000000..d97464b --- /dev/null +++ b/components/ui/select.jsx @@ -0,0 +1,145 @@ +"use client"; + +import * as React from "react"; +import * as SelectPrimitive from "@radix-ui/react-select"; +import { Check, ChevronDown, ChevronUp } from "lucide-react"; + +import { cn } from "../../lib/utils"; + +const Select = SelectPrimitive.Root; + +const SelectGroup = SelectPrimitive.Group; + +const SelectValue = SelectPrimitive.Value; + +const SelectTrigger = React.forwardRef( + ({ className, children, ...props }, ref) => ( + span]:line-clamp-1", + className + )} + {...props} + > + {children} + + + + + ) +); +SelectTrigger.displayName = SelectPrimitive.Trigger.displayName; + +const SelectScrollUpButton = React.forwardRef( + ({ className, ...props }, ref) => ( + + + + ) +); +SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName; + +const SelectScrollDownButton = React.forwardRef( + ({ className, ...props }, ref) => ( + + + + ) +); +SelectScrollDownButton.displayName = SelectPrimitive.ScrollDownButton.displayName; + +const SelectContent = React.forwardRef( + ({ className, children, position = "popper", ...props }, ref) => ( + + + + + {children} + + + + + ) +); +SelectContent.displayName = SelectPrimitive.Content.displayName; + +const SelectLabel = React.forwardRef( + ({ className, ...props }, ref) => ( + + ) +); +SelectLabel.displayName = SelectPrimitive.Label.displayName; + +const SelectItem = React.forwardRef( + ({ className, children, ...props }, ref) => ( + + + + + + + {children} + + ) +); +SelectItem.displayName = SelectPrimitive.Item.displayName; + +const SelectSeparator = React.forwardRef( + ({ className, ...props }, ref) => ( + + ) +); +SelectSeparator.displayName = SelectPrimitive.Separator.displayName; + +export { + Select, + SelectGroup, + SelectValue, + SelectTrigger, + SelectContent, + SelectLabel, + SelectItem, + SelectSeparator, + SelectScrollUpButton, + SelectScrollDownButton, +}; diff --git a/lib/utils/index.js b/lib/utils/index.js new file mode 100644 index 0000000..3bef539 --- /dev/null +++ b/lib/utils/index.js @@ -0,0 +1,15 @@ +import qs from "query-string" + + +export const formUrlQuery = ({ params, key, value }) => { + const currentUrl = qs.parse(params); + currentUrl[key] = value; + + return qs.stringifyUrl( + { + url: window.location.pathname, + query: currentUrl + }, + { skipNull: true } + ); + }; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 89f4fe7..0413621 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,11 +15,11 @@ "@radix-ui/react-dialog": "1.1.1", "@radix-ui/react-dropdown-menu": "2.1.1", "@radix-ui/react-icons": "1.3.0", - "@radix-ui/react-label": "^2.1.0", + "@radix-ui/react-label": "2.1.0", "@radix-ui/react-popover": "^1.1.4", "@radix-ui/react-select": "2.1.1", "@radix-ui/react-separator": "^1.1.1", - "@radix-ui/react-slot": "^1.1.0", + "@radix-ui/react-slot": "1.1.0", "@radix-ui/react-toast": "1.2.1", "@tabler/icons": "1.115.0", "@tabler/icons-react": "3.11.0", diff --git a/package.json b/package.json index 6c5c01d..6359053 100644 --- a/package.json +++ b/package.json @@ -19,11 +19,11 @@ "@radix-ui/react-dialog": "1.1.1", "@radix-ui/react-dropdown-menu": "2.1.1", "@radix-ui/react-icons": "1.3.0", - "@radix-ui/react-label": "^2.1.0", + "@radix-ui/react-label": "2.1.0", "@radix-ui/react-popover": "^1.1.4", "@radix-ui/react-select": "2.1.1", "@radix-ui/react-separator": "^1.1.1", - "@radix-ui/react-slot": "^1.1.0", + "@radix-ui/react-slot": "1.1.0", "@radix-ui/react-toast": "1.2.1", "@tabler/icons": "1.115.0", "@tabler/icons-react": "3.11.0", @@ -75,4 +75,4 @@ "prisma": { "seed": "node prisma/seed.js" } -} +} \ No newline at end of file