From 8d3238d0fb7bd8b38dbe2ed1554f51999de7e4b8 Mon Sep 17 00:00:00 2001 From: felixtanhm Date: Wed, 8 May 2024 13:40:51 +0800 Subject: [PATCH] feat: prev next navigation for pokedetails page --- .../client/src/components/Button.jsx | 13 +++++ .../client/src/components/PokeDetails.jsx | 50 ++++++++++++------- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/full-stack-javascript/21-inventory-application/client/src/components/Button.jsx b/full-stack-javascript/21-inventory-application/client/src/components/Button.jsx index e69de29..820dd3a 100644 --- a/full-stack-javascript/21-inventory-application/client/src/components/Button.jsx +++ b/full-stack-javascript/21-inventory-application/client/src/components/Button.jsx @@ -0,0 +1,13 @@ +function Button({ onClick, isDisabled, text }) { + return ( + + ); +} + +export default Button; diff --git a/full-stack-javascript/21-inventory-application/client/src/components/PokeDetails.jsx b/full-stack-javascript/21-inventory-application/client/src/components/PokeDetails.jsx index e63bf29..091bdc9 100644 --- a/full-stack-javascript/21-inventory-application/client/src/components/PokeDetails.jsx +++ b/full-stack-javascript/21-inventory-application/client/src/components/PokeDetails.jsx @@ -1,25 +1,30 @@ -import { useParams } from "react-router-dom"; +import { useParams, useNavigate } from "react-router-dom"; import { useState } from "react"; import PokeType from "./PokeType"; +import Button from "./Button"; import capitalise from "../utils/capitalise"; import { HeartIcon as HeartIconOutline } from "@heroicons/react/24/outline"; import { HeartIcon as HeartIconSolid } from "@heroicons/react/24/solid"; function PokeDetails() { const params = useParams(); + const navigate = useNavigate(); const [currPokemon, setCurrPokemon] = useState(null); const [state, setState] = useState("loading"); - function toggleFavorite(e, dexId) { - e.stopPropagation(); + function toggleFavorite(dexId) { console.log(dexId); } - async function fetchPokemon() { + function handleNav(dexId, isPrev) { + const nextDexId = isPrev ? dexId - 1 : dexId + 1; + navigate(`/pokemon/${nextDexId}`); + fetchPokemon(nextDexId); + } + + async function fetchPokemon(dexId) { try { - const response = await fetch( - `http://localhost:3000/pokemon/${params.dexId}`, - ); + const response = await fetch(`http://localhost:3000/pokemon/${dexId}`); if (!response.ok) { let errorMessage = `Failed to fetch data. Status: ${response.status}`; @@ -38,7 +43,7 @@ function PokeDetails() { } } - if (!currPokemon) fetchPokemon(); + if (!currPokemon) fetchPokemon(params.dexId); return (
@@ -50,22 +55,31 @@ function PokeDetails() { <> {/* Buttons */}
- + - +