From cf195c969a44f882474ffa64a951a48ceccb48a1 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Tue, 25 Oct 2022 00:01:52 +0800 Subject: [PATCH] Add error handler Specify error handler when no thumbnail is found. --- pages/games/[id]/play.tsx | 1 + pages/games/index.tsx | 61 ++++++++++++++++++++++++++++++++++----- utils/games.ts | 2 +- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/pages/games/[id]/play.tsx b/pages/games/[id]/play.tsx index 5a195a7..473fc70 100644 --- a/pages/games/[id]/play.tsx +++ b/pages/games/[id]/play.tsx @@ -98,6 +98,7 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => { notFound: true, } } + const game = await response.json(); return { props: { diff --git a/pages/games/index.tsx b/pages/games/index.tsx index 993122f..1582bc6 100644 --- a/pages/games/index.tsx +++ b/pages/games/index.tsx @@ -4,6 +4,7 @@ import {Game} from '../../utils/games'; import Link from 'next/link'; import Head from 'next/head'; import Image from 'next/image'; +import {useState} from 'react'; type HomeProps = { games: Game[], @@ -12,6 +13,15 @@ type HomeProps = { const GamesPage: NextPage = ({ games, }) => { + const [displayGames, setDisplayGames] = useState(games); + + const handleImageError = (game: Game) => () => { + setDisplayGames((games) => games.map(g => ({ + ...g, + thumbnail: g.id === game.id ? undefined : g.thumbnail, + }))) + } + return ( <> @@ -53,7 +63,7 @@ const GamesPage: NextPage = ({ }} > { - games.map((g) => ( + displayGames.map((g) => (
  • = ({ boxShadow: '0.125rem 0.25rem 0.25rem rgba(0,0,0,0.0625)' }} > - {g.name} +
    + { + g.thumbnail + && ( + {g.name} + ) + } + { + !g.thumbnail + && ( +
    + 🎮 +
    + ) + } +
    = ({ } export const getServerSideProps: GetServerSideProps = async (ctx) => { - const response = await fetch(`http://localhost:3000/api/games`); + const response = await fetch('http://localhost:3000/api/games'); if (response.status !== 200) { return { notFound: true, } } + const games = await response.json(); return { props: { diff --git a/utils/games.ts b/utils/games.ts index aecd1ad..45ca923 100644 --- a/utils/games.ts +++ b/utils/games.ts @@ -7,7 +7,7 @@ export type Game = { id: string, name: string, main: string, - thumbnail: string, + thumbnail?: string, data: { aspectRatio: number, },