Bladeren bron

Add error handler

Specify error handler when no thumbnail is found.
master
TheoryOfNekomata 2 jaren geleden
bovenliggende
commit
cf195c969a
3 gewijzigde bestanden met toevoegingen van 55 en 9 verwijderingen
  1. +1
    -0
      pages/games/[id]/play.tsx
  2. +53
    -8
      pages/games/index.tsx
  3. +1
    -1
      utils/games.ts

+ 1
- 0
pages/games/[id]/play.tsx Bestand weergeven

@@ -98,6 +98,7 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
notFound: true,
}
}

const game = await response.json();
return {
props: {


+ 53
- 8
pages/games/index.tsx Bestand weergeven

@@ -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<HomeProps> = ({
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 (
<>
<Head>
@@ -53,7 +63,7 @@ const GamesPage: NextPage<HomeProps> = ({
}}
>
{
games.map((g) => (
displayGames.map((g) => (
<li
key={g.id}
style={{
@@ -81,12 +91,46 @@ const GamesPage: NextPage<HomeProps> = ({
boxShadow: '0.125rem 0.25rem 0.25rem rgba(0,0,0,0.0625)'
}}
>
<Image
alt={g.name}
src={`/games/${g.id}/${g.thumbnail}`}
layout="fill"
className="foo"
/>
<div
style={{
width: '100%',
height: '100%',
backgroundColor: '#ddd',
}}
>
{
g.thumbnail
&& (
<Image
alt={g.name}
src={`/games/${g.id}/${g.thumbnail}`}
layout="fill"
className="foo"
onError={handleImageError(g)}
/>
)
}
{
!g.thumbnail
&& (
<div
style={{
fontSize: '10rem',
display: 'grid',
placeContent: 'center',
width: '100%',
height: '100%',
opacity: 0.125,
filter: 'grayscale(1)',
overflow: 'hidden',
lineHeight: 1,
}}
>
🎮
</div>
)
}
</div>
<div
style={{
position: 'absolute',
@@ -113,12 +157,13 @@ const GamesPage: NextPage<HomeProps> = ({
}

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: {


+ 1
- 1
utils/games.ts Bestand weergeven

@@ -7,7 +7,7 @@ export type Game = {
id: string,
name: string,
main: string,
thumbnail: string,
thumbnail?: string,
data: {
aspectRatio: number,
},


Laden…
Annuleren
Opslaan