Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added feature to navigate between all pet photos #14

Merged
merged 2 commits into from
Oct 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/components/pets/PetInfo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import axios from "axios";
import { Card, Image } from "react-bootstrap";
import { Card, Image, Button } from "react-bootstrap";
export default function PetInfo({ token }) {
let { id } = useParams();
const [pet, setPet] = useState("");
Expand All @@ -20,11 +20,31 @@ 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 variant="primary" onClick={previousPhoto}>Previous Photo</Button>
&nbsp;&nbsp;
<Button variant="primary" onClick={nextPhoto}>Next</Button>

<br />
<br />
{pet && (
<Card>
<Card.Header as="h5">{pet.type}</Card.Header>
Expand Down