Skip to content

Commit

Permalink
added feature to navigate between all pet photos
Browse files Browse the repository at this point in the history
  • Loading branch information
Wei-J-Huang committed Oct 27, 2020
1 parent 5f5c501 commit 985526d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/components/pets/PetInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,29 @@ export default function PetInfo({ token }) {
});
}, [id, token]);

const [ photoIndex, setPhotoIndex] = useState(0);
let index = photoIndex;

function nextPhoto(){
if(pet.photos[index+1] !== undefined){
setPhotoIndex(prevState => prevState + 1);
}
}

function previousPhoto(){
if(pet.photos[index-1] !== undefined){
setPhotoIndex(prevState => prevState - 1);
}
}

return (
<div className="petInfo">
<h1>{pet.name}</h1>
{pet && <Image src={pet.photos[0].large} style={{}} />}

{pet && <Image src={pet.photos[index].large} style={{}} />}
<button onClick={previousPhoto}>Previous Photo</button>
<button onClick={nextPhoto}>Next Photo</button>
<br />
<br />
{pet && (
<Card>
<Card.Header as="h5">{pet.type}</Card.Header>
Expand Down

0 comments on commit 985526d

Please sign in to comment.