Skip to content

Instantly share code, notes, and snippets.

@AlaBenAicha
Created October 2, 2022 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlaBenAicha/c9c4e51f6ce49fa2d51ae2f90aca58c8 to your computer and use it in GitHub Desktop.
Save AlaBenAicha/c9c4e51f6ce49fa2d51ae2f90aca58c8 to your computer and use it in GitHub Desktop.
import React from "react";
import axios from "axios";
import type { GetStaticProps, GetStaticPaths } from "next";
import { useQuery, QueryClient, dehydrate } from "react-query";
import { useRouter } from "next/router";
import PokemonCard from "../../components/PokemonCard";
const fetchPokemon = (id: string) =>
axios
.get(`https://pokeapi.co/api/v2/pokemon/${id}/`)
.then(({ data }) => data);
export default function Pokemon() {
const router = useRouter();
const pokemonID = typeof router.query?.id === "string" ? router.query.id : "";
const { isSuccess, data: pokemon, isLoading, isError } = useQuery(
["getPokemon", pokemonID],
() => fetchPokemon(pokemonID),
{
enabled: pokemonID.length > 0,
staleTime: Infinity
}
);
if (isSuccess) {
return (
<div className="container">
<PokemonCard
name={pokemon.name}
image={pokemon.sprites?.other?.["official-artwork"]?.front_default}
weight={pokemon.weight}
xp={pokemon.base_experience}
abilities={pokemon.abilities?.map((item) => item.ability.name)}
/>
</div>
);
}
if (isLoading) {
return <div className="center">Loading...</div>;
}
if (isError) {
return (
<div className="center">
We couldn't find your pokemon{" "}
<span role="img" aria-label="sad">
😢
</span>
</div>
);
}
return <></>;
}
export const getStaticProps: GetStaticProps = async (context) => {
const id = context.params?.id as string;
const queryClient = new QueryClient();
await queryClient.prefetchQuery(["getPokemon", id], () => fetchPokemon(id));
return {
props: {
dehydratedState: dehydrate(queryClient)
}
};
};
export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: [],
fallback: "blocking"
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment