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

Create Dog CEO App #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 24 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Intro to AJAX Practice Tasks</title>
<script src="src/index.js" charset="utf-8"></script>
<script defer src="src/index.js" charset="utf-8"></script>
</head>
<body>
<h1>Dog CEO</h1>
Expand All @@ -15,10 +15,33 @@ <h1>Dog CEO</h1>
<hr>
<label for="select-breed">Filter Breeds That Start with:</label>
<select id="breed-dropdown" name="select-breed">
<option default value="all">all</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
<option value="f">f</option>
<option value="g">g</option>
<option value="h">h</option>
<option value="i">i</option>
<option value="j">j</option>
<option value="k">k</option>
<option value="l">l</option>
<option value="m">m</option>
<option value="n">n</option>
<option value="o">o</option>
<option value="p">p</option>
<option value="q">q</option>
<option value="r">r</option>
<option value="s">s</option>
<option value="t">t</option>
<option value="u">u</option>
<option value="v">v</option>
<option value="w">w</option>
<option value="x">x</option>
<option value="y">y</option>
<option value="z">z</option>
Comment on lines +23 to +44

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you think of any ways to do this programmatically in JS?

</select>

<ul id="dog-breeds">
Expand Down
91 changes: 90 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,90 @@
console.log('%c HI', 'color: firebrick')
const imageDiv = document.querySelector('#dog-image-container')
const dogBreedsUL = document.querySelector('#dog-breeds')
const breedFilter = document.querySelector('#breed-dropdown')
const imgUrl = "https://dog.ceo/api/breeds/image/random/4";
const breedUrl = "https://dog.ceo/api/breeds/list/all";

const allBreedsArray = []

const fetchRandomDog = (url) => {
fetch(url)
.then(res => res.json())
.then(handleRandomSuccess)
.catch(handleError)
}

const fetchBreeds = (url) => {
fetch(url)
.then(res => res.json())
.then(handleBreedsSuccess)
.catch(handleError)
}

const filterBreeds = (e) => {
if(e.target.value !== 'all'){
const filteredArray = allBreedsArray.filter(breed => breed.charAt(0) === e.target.value)
renderBreed(filteredArray)
}else{
renderBreed(allBreedsArray)
}
window.scrollBy({
top: 200,
behavior: 'smooth'
})
}

const handleRandomSuccess = (data) => data.message.forEach(renderImage)

const handleBreedsSuccess = (data) => {
for(const breed in data.message){
allBreedsArray.push(breed)
}
renderBreed(allBreedsArray)
}

const capitalizeBreed = (breedString) => breedString.charAt(0).toUpperCase() + breedString.slice(1)

const renderBreed = (breedsArray) => {
dogBreedsUL.innerHTML = ''
breedsArray.forEach(breed => {
const li = document.createElement('li')
li.textContent = capitalizeBreed(breed)
li.addEventListener('click', changeColor)
dogBreedsUL.appendChild(li)
})
}

const renderImage = (image) => {
const img = document.createElement('img')
img.src = image
img.style.width = '500px'
imageDiv.appendChild(img)
}

const changeColor = (e) => e.target.style.color = getRandomColor()

function getRandomColor() {
const hexLetters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += hexLetters[Math.floor(Math.random() * 16)];
}
return color;
}
Comment on lines +66 to +73

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun!


const handleError = (error) => {
console.log(error)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(error)

alert('Something went wrong when fetching! Check the console.')
}

const setFilter = () => breedFilter.value = 'all'

const initApp = () => {
fetchRandomDog(imgUrl)
fetchBreeds(breedUrl)
document.addEventListener('DOMContentLoaded', setFilter)
breedFilter.addEventListener('change', filterBreeds)
}

initApp()