From cf4bdbaae70301e42d1702248385314a7cf5480b Mon Sep 17 00:00:00 2001 From: Rkebede Date: Thu, 3 Sep 2020 03:37:00 -0500 Subject: [PATCH] Done. --- src/index.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/index.js b/src/index.js index 407dde1..fcb5b40 100644 --- a/src/index.js +++ b/src/index.js @@ -1 +1,63 @@ console.log('%c HI', 'color: firebrick') +const imgUrl = "https://dog.ceo/api/breeds/image/random/4" +const breedUrl = 'https://dog.ceo/api/breeds/list/all' +let type +let breedList +let fetchedList + +document.addEventListener("DOMContentLoaded", () => { + fetchImages() + fetchBreed() + document.getElementById("breed-dropdown").addEventListener("change", findBreedByFirstLetter) + breedList = document.getElementById("dog-breeds") +}) + +function fetchImages(){ + fetch(imgUrl) + .then(resp => resp.json()) + .then(json => addImages(json)) +} + +function addImages(images){ + const imgContainer = document.getElementById("dog-image-container") + images.message.forEach(image => { + let img = document.createElement("IMG") + img.src = image + imgContainer.appendChild(img) + }) +} + +function fetchBreed(){ + fetch(breedUrl) + .then(resp => resp.json()) + .then(json => addBreed(json)) +} + +function addBreed(breeds){ + const breedList= document.getElementById("dog-breeds") + + breedList.addEventListener("click", (e) => { + if (e.target.tagName === "LI"){ + e.target.style.color = "red" + } + }) + + fetchedList = Object.keys(breeds.message) + createLiForBreeds(fetchedList) +} + +function createLiForBreeds(breeds){ + console.log(breeds) + breeds.forEach(breed => { + let type = document.createElement("LI") + type.setAttribute('class','breed-list') + type.innerText = breed + breedList.appendChild(type) + }) +} + +function findBreedByFirstLetter(e){ + const filteredBreeds = fetchedList.filter((breed) => breed[0] === e.target.value) + breedList.innerHTML = " " + createLiForBreeds(filteredBreeds) +}