-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned up directory structure and added error messages for both quer…
…y pages
- Loading branch information
Showing
6 changed files
with
166 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { ImageList, queryOptionsToHTML, DataFetcher } from '../util'; | ||
|
||
function DogQueryForm({queryOptions}) { | ||
const [posted, setPosted] = useState(false); | ||
const [imageLinks, setImageLinks] = useState([]); | ||
|
||
const breedOptions = queryOptionsToHTML(queryOptions); | ||
// Config number of images to pull from API | ||
const imageCount = 27; | ||
|
||
// TODO: Find way to stop user from spamming fetches | ||
function handleSubmit(e) { | ||
e.preventDefault(); | ||
setPosted(true); | ||
const breed = e.target.breeds.value.trim().toLowerCase(); | ||
|
||
if(breed !== undefined && breed.length > 0) { | ||
fetch(`http://localhost:3011/dog/${breed}/get-images/${imageCount}`) | ||
.then(response => response.json()) | ||
.then(data => setImageLinks(data['message'])) | ||
.catch(error => { | ||
setImageLinks([]); | ||
}) | ||
} | ||
} | ||
|
||
let imageSectionHTML = <p> Select a name and click 'Fetch' to get started! </p>; | ||
if(posted) { | ||
if(imageLinks.length > 0) { | ||
imageSectionHTML = <ImageList images={imageLinks} desiredLength={imageCount} />; | ||
} | ||
else { | ||
imageSectionHTML = <p> Invalid breed name! </p>; | ||
} | ||
} | ||
|
||
return ( | ||
<div className='query-form'> | ||
<h1>Lots of dogs! 🐕</h1> | ||
<p>See {imageCount} random photos of your favorite dogs</p> | ||
<form onSubmit={handleSubmit}> | ||
<label htmlFor="breeds"> Select a breed: </label> | ||
<select name="breeds" id="breeds"> | ||
{breedOptions} | ||
</select> | ||
<button className="fetch" type="submit">Fetch</button> | ||
</form> | ||
<br/> | ||
{imageSectionHTML} | ||
</div> | ||
); | ||
} | ||
|
||
export function QueryBreedSection() { | ||
return ( | ||
<> | ||
<section id='QueryBreedSection'> | ||
<DataFetcher url='http://localhost:3011/breeds' | ||
ComponentToRender={DogQueryForm} /> | ||
</section> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { useState } from 'react'; | ||
|
||
|
||
export function RandomDogImage() { | ||
const [posted, setPosted] = useState(null); | ||
const [imageLink, setImageLink] = useState(''); | ||
|
||
function fetchImage() { | ||
fetch('http://localhost:3011/dog/get-random') | ||
.then(response => response.json()) | ||
.then(data => { | ||
setImageLink(data['message']); | ||
setPosted(true); | ||
}) | ||
.catch(error => { | ||
setImageLink(''); | ||
setPosted(false); | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className='random-image-container'> | ||
<h1> Random Dog Image </h1> | ||
<p> <button className='fetch' onClick={fetchImage}>Fetch</button> a new image </p> | ||
{posted === false && <p> Error fetching image from server! </p> } | ||
{posted && <img className='random-dog-image' src={imageLink} alt='Dog' />} | ||
</div> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters