diff --git a/README.md b/README.md index 58f1a8a66..5c3ea5176 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ # js-project-recipe-library +Link to Netlify: https://js-recipe-library.netlify.app/ \ No newline at end of file diff --git a/cross-white.svg b/cross-white.svg new file mode 100644 index 000000000..eacf48c91 --- /dev/null +++ b/cross-white.svg @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 000000000..3e890f3d4 --- /dev/null +++ b/index.html @@ -0,0 +1,89 @@ + + +
+ + +Loading...
`; + } + + try { + const response = await fetch(url); + + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + + const data = await response.json(); + + const fetchedRecipes = data.recipes; + + // turn off loading state + fetchLoadingState = false; + + // if fetch is successful, save it to local storage and update allRecipes + if(fetchedRecipes && fetchedRecipes.length > 0 ) { + localStorage.setItem("recipes", JSON.stringify(fetchedRecipes)); + allRecipes = fetchedRecipes; + // if the fetch has no data (empty array), update allRecipes with data from local storage + } else { + allRecipes = storedRecipes; + } + + if (allRecipes.length > 0) { + showRecipeCards(allRecipes); + } else { + cardContainer.innerHTML = ``; + } + } + // if fetch fails + catch(error) { + console.error('Fetch error:', error); + // update allRecipes with data from local storage + allRecipes = storedRecipes; + + if (allRecipes.length > 0) { + showRecipeCards(allRecipes); + } else { + cardContainer.innerHTML = ``; + } + } +}; + + +const showRecipeCards = (recipeArray) => { + + //reset card container before filling it + cardContainer.innerHTML = ""; + + if(!recipeArray || recipeArray.length === 0) { + cardContainer.innerHTML = ``; + } + + recipeArray.forEach((recipe) => { + + const card = document.createElement("div"); + card.classList.add("card"); + card.dataset.id = recipe.id; + + // check if the recipes id match any id of the recipes in favoriteRecipes. If true, set the variable to the class of active state for the heart icon + const heartIconClass = favoriteRecipes.some(favoriteRecipe => favoriteRecipe.id === recipe.id) ? "fas" : "far"; + + + // create elements in each card with content from each recipe + card.innerHTML += ` +Cuisine: ${ + recipe.cuisines && recipe.cuisines.length > 0 + ? recipe.cuisines.join(", ") + : "Not specified" + }
+Time: ${recipe.readyInMinutes} min
+Servings: ${recipe.servings}
+${recipe.summary.split(". ").slice(0, 2).join(". ") + "."}
+