Skip to content
Snippets Groups Projects
Commit 3e2b3037 authored by Yusuf Akgül's avatar Yusuf Akgül :hatching_chick:
Browse files

small optimization

parent df762b41
No related branches found
No related tags found
1 merge request!12Feat.back to top button
Pipeline #35258 passed
...@@ -3,7 +3,7 @@ import { InfiniteScrollGames } from "@/components/infinity-scroll"; ...@@ -3,7 +3,7 @@ import { InfiniteScrollGames } from "@/components/infinity-scroll";
import ScrollToTop from "@/components/scroll-to-top"; import ScrollToTop from "@/components/scroll-to-top";
import SearchInput from "@/components/search-input"; import SearchInput from "@/components/search-input";
// renders a list of games infinitely (presumably) // renders a list of games infinitely
export default async function GamesPage() { export default async function GamesPage() {
return ( return (
<> <>
......
...@@ -30,12 +30,12 @@ export async function GET(req: NextRequest) { ...@@ -30,12 +30,12 @@ export async function GET(req: NextRequest) {
} }
const games = await getGames(page, const games = await getGames(page,
search ? search : '', search ? search : undefined,
category ? EGameCategory[category as keyof typeof EGameCategory] : undefined, category ? EGameCategory[category as keyof typeof EGameCategory] : undefined,
genre ? EGameGenres[genre as keyof typeof EGameGenres] : undefined, genre ? EGameGenres[genre as keyof typeof EGameGenres] : undefined,
filteredPlatforms, filteredPlatforms,
sortby ? sortby : '', sortby ? sortby : undefined,
order ? order : '' order ? order : undefined
); );
return NextResponse.json(games); return NextResponse.json(games);
} catch (error) { } catch (error) {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue } from "@/components/ui/select";
import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useEffect, useState } from "react"; import { useState } from "react";
import { Icons } from "./icons"; import { Icons } from "./icons";
import { Button } from "./ui/button"; import { Button } from "./ui/button";
import { Card } from "./ui/card"; import { Card } from "./ui/card";
...@@ -25,8 +25,8 @@ export default function Sort() { ...@@ -25,8 +25,8 @@ export default function Sort() {
category: selectedCategory ? `category=${selectedCategory}` : '', category: selectedCategory ? `category=${selectedCategory}` : '',
genre: selectedGenre ? `genre=${selectedGenre}` : '', genre: selectedGenre ? `genre=${selectedGenre}` : '',
platform: selectedPlatform ? `platform=${selectedPlatform}` : '', platform: selectedPlatform ? `platform=${selectedPlatform}` : '',
sortMethod: selectedSortMethod ? `sortby=${selectedSortMethod}` : '', sortMethod: selectedSortMethod == 'total_rating_count' ? '' : `sortby=${selectedSortMethod}`,
sortOrder: selectedSortOrder ? `order=${selectedSortOrder}` : '', sortOrder: selectedSortOrder == 'desc' ? '' : `order=${selectedSortOrder}`,
}; };
const queryParamString = Object.values(urlParams) const queryParamString = Object.values(urlParams)
...@@ -49,6 +49,7 @@ export default function Sort() { ...@@ -49,6 +49,7 @@ export default function Sort() {
setSelectedGenre(''); setSelectedGenre('');
setSelectedPlatform(''); setSelectedPlatform('');
setSelectedSortMethod('total_rating_count'); setSelectedSortMethod('total_rating_count');
setSelectedSortOrder('desc');
} }
return ( return (
......
...@@ -34,7 +34,7 @@ export function InfiniteScrollGames() { ...@@ -34,7 +34,7 @@ export function InfiniteScrollGames() {
fetchNextPage, fetchNextPage,
hasNextPage, hasNextPage,
} = useInfiniteQuery( } = useInfiniteQuery(
['infiniteGames', params], [params],
async ({ pageParam = 1 }) => async ({ pageParam = 1 }) =>
await fetch(`/api/games/?page=${pageParam}${params}`, await fetch(`/api/games/?page=${pageParam}${params}`,
).then((result) => result.json() as Promise<IGame[]>), ).then((result) => result.json() as Promise<IGame[]>),
...@@ -49,7 +49,9 @@ export function InfiniteScrollGames() { ...@@ -49,7 +49,9 @@ export function InfiniteScrollGames() {
<Card className="p-6"> <Card className="p-6">
{status === 'error' {status === 'error'
? ?
(<span>Uh oh... something went wrong: {(error as Error).message}</span>) (<span className="text-center">
Uh oh... something went wrong
</span>)
: :
(status === 'success' && ( (status === 'success' && (
<InfiniteScroll <InfiniteScroll
......
...@@ -28,7 +28,7 @@ async function getToken(): Promise<IAuth> { ...@@ -28,7 +28,7 @@ async function getToken(): Promise<IAuth> {
} }
// fetches the top 200 games with a rating of 96 or higher // fetches the top 200 games with a rating of 96 or higher
export async function getGames(page = 1, search?: string, category?: number, genre?: number, platform?: number[], sortby?: string, order?: string): Promise<IGame[]> { export async function getGames(page = 1, search?: string, category?: number, genre?: number, platform?: number[], sortby = "total_rating_count", order = "desc"): Promise<IGame[]> {
const auth = await getToken(); const auth = await getToken();
const url = new URL(`${IGDB_BASE_URL}/games`); const url = new URL(`${IGDB_BASE_URL}/games`);
...@@ -42,7 +42,7 @@ export async function getGames(page = 1, search?: string, category?: number, gen ...@@ -42,7 +42,7 @@ export async function getGames(page = 1, search?: string, category?: number, gen
}, },
body: body:
`fields name, cover.image_id; limit ${limit}; offset ${offset}; `fields name, cover.image_id; limit ${limit}; offset ${offset};
sort ${sortby ? sortby : "total_rating_count"} ${order ? order : "desc"}; sort ${sortby} ${order};
where total_rating_count > 3 where total_rating_count > 3
& cover != null & total_rating != null & rating != null & age_ratings != null & cover != null & total_rating != null & rating != null & age_ratings != null
& name ~ *"${search ? search : ""}"* & name ~ *"${search ? search : ""}"*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment