|
1 |
| -//const container = document.getElementById('container'); |
2 |
| -const api_id = '95e7fbbf'; |
3 |
| -const app_key = '22f72d0ac69dde30ee5707e75f4e7b8d'; |
4 |
| -const q = 'pickles'; |
5 |
| -const url = 'https://api.edamam.com/search?q=${pickles}&app_id=${api_id}&app_key=${app_key}'; |
| 1 | +const cardContainer = document.getElementById('card-container'); |
| 2 | +const container = document.getElementById("container"); |
| 3 | +/* const api_id = "95e7fbbf"; |
| 4 | +const app_key = "22f72d0ac69dde30ee5707e75f4e7b8d"; */ |
| 5 | +const q = "q=pickles"; |
| 6 | +const cors = "https://yacdn.org/proxy/"; |
| 7 | +const url = "http://www.recipepuppy.com/api/?"; |
| 8 | +const page = "&p=" + Math.floor(Math.random() * 10); |
6 | 9 |
|
7 | 10 | //Fetch the JSON object
|
8 |
| -const getRecipes = async () =>{ |
9 |
| - const response = await fetch(url); |
| 11 | +const getRecipes = async () => { |
| 12 | + const response = await fetch(`${cors}${url}${q}${page}`); |
10 | 13 | const data = await response.json();
|
11 |
| - for(let i = 0; i < data.hits.length; i++){ |
12 |
| - console.log(data.hits[i]); |
13 |
| - } |
| 14 | + //const dataObj = JSON.parse(data); |
| 15 | + for (let i = 0; i < data.results.length; i++) { |
| 16 | + //console.log(data.results[i]); |
| 17 | + let title = document.createElement("h3"); |
| 18 | + title.setAttribute("id", "title"); |
| 19 | + title.appendChild(document.createTextNode(data.results[i]["title"])); |
| 20 | + //console.log(data.results[i]["title"]); |
| 21 | + |
| 22 | + let image = new Image(); |
| 23 | + if (data.results[i]["thumbnail"] == "") { |
| 24 | + image.src = "images/pickle.jpg"; |
| 25 | + image.setAttribute("id", "defaultImage"); |
| 26 | + } else { |
| 27 | + image.src = data.results[i]["thumbnail"]; |
| 28 | + image.setAttribute("id", "image"); |
| 29 | + } |
| 30 | + |
| 31 | + //console.log(data.results[i]["thumbnail"]); |
| 32 | + |
| 33 | + let recipe = document.createElement("p"); |
| 34 | + recipe.setAttribute("id", "recipe"); |
| 35 | + let ingredients = data.results[i]["ingredients"].toUpperCase(); |
| 36 | + recipe.appendChild(document.createTextNode(ingredients)); |
| 37 | + console.log(data.results[i]["ingredients"]); |
14 | 38 |
|
15 |
| -} |
| 39 | + let href = document.createElement("a"); |
| 40 | + let link = data.results[i]["href"] |
| 41 | + href.setAttribute("href", `${link}`); |
| 42 | + href.appendChild(document.createTextNode(data.results[i]["href"])); |
| 43 | + console.log(data.results[i]["href"]); |
| 44 | + |
| 45 | + let cardElement = document.createElement("div"); |
| 46 | + cardElement.setAttribute("id", "card"); |
| 47 | + cardElement.appendChild(title); |
| 48 | + cardElement.appendChild(image); |
| 49 | + cardElement.appendChild(recipe); |
| 50 | + cardElement.appendChild(href); |
| 51 | + |
| 52 | + cardContainer.appendChild(cardElement); |
| 53 | + } |
| 54 | +}; |
16 | 55 |
|
17 |
| -getRecipes(); |
| 56 | +getRecipes().catch(error => { |
| 57 | + console.log("Error retrieving results"); |
| 58 | + console.error(error); |
| 59 | +}); |
0 commit comments