-
Notifications
You must be signed in to change notification settings - Fork 60
Maja E - Recipe Library #68
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
Open
majaengberg02
wants to merge
17
commits into
Technigo:main
Choose a base branch
from
majaengberg02:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9508fa6
Header and filter buttons
majaengberg02 3208fb8
Styling filter buttons
majaengberg02 3bfc48d
Fixed filter buttons
majaengberg02 c941d5a
Placeholder recipe cards and styling
majaengberg02 0ff7a91
Placeholder filters/sorting and clean up html code
majaengberg02 bc6381c
Added JavaScrip filter/sort function
majaengberg02 0c30b6d
Mockup recipes and updated JavaScript and arrays
majaengberg02 140c895
Unchecked box
majaengberg02 db6d358
Fetching data from API, local test data file
majaengberg02 872b150
Using data from API instead of local
majaengberg02 8ae9a0d
Added a random recipe button and made cards clickable with link
majaengberg02 397316b
Fetching recipes from API
majaengberg02 f559751
Error messages and small changes
majaengberg02 cf3c2c9
Small changes to error message
majaengberg02 2421793
Small changes to buttons
majaengberg02 790d99b
Changed fetch style
majaengberg02 c753936
Pointer on hover in css instead of js
majaengberg02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,14 @@ | ||
| function getRecipes() { | ||
| const xhttp = new XMLHttpRequest(); | ||
| xhttp.open("GET", "https://api.spoonacular.com/recipes/random?apiKey=24c0ad763b794214b472e5a5e70fac09&number=20&includeNutrition=false", false); | ||
| xhttp.send(); | ||
|
|
||
| if (xhttp.status === 200) { | ||
| const respObj = JSON.parse(xhttp.responseText); | ||
| return { recipes: respObj.recipes, error: null }; | ||
| } else if (xhttp.status === 402) { | ||
| return { recipes: [], error: "Daily quota reached. Please try again tomorrow." }; | ||
| } else { | ||
| return { recipes: [], error: "Something went wrong. Please try again later." }; | ||
| } | ||
| } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,94 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Recipe Library</title> | ||
| <link rel="stylesheet" href="styles.css"> | ||
| <script src="data.js" defer></script> | ||
| <script src="script.js" defer></script> | ||
| </head> | ||
| <body> | ||
|
|
||
| <h1>Recipe Library</h1> | ||
|
|
||
| <section class="filters"> | ||
| <div class="filtername-and-buttons"> | ||
| <h2>Dietary Preference</h2> | ||
|
|
||
| <div class="filter-diet"> | ||
| <input | ||
| type="radio" | ||
| name="diet" | ||
| value="all" | ||
| id="all" | ||
| checked> | ||
| <label for="all" class="diet-label">All</label> | ||
|
|
||
| <input | ||
| type="radio" | ||
| name="diet" | ||
| value="vegetarian" | ||
| id="vegetarian"> | ||
| <label for="vegetarian" class="diet-label">Vegetarian</label> | ||
|
|
||
| <input | ||
| type="radio" | ||
| name="diet" | ||
| value="vegan" | ||
| id="vegan"> | ||
| <label for="vegan" class="diet-label">Vegan</label> | ||
|
|
||
| <input | ||
| type="radio" | ||
| name="diet" | ||
| value="gluten free" | ||
| id="gluten-free"> | ||
| <label for="gluten-free" class="diet-label">Gluten-Free</label> | ||
|
|
||
| <input | ||
| type="radio" | ||
| name="diet" | ||
| value="dairy free" | ||
| id="dairy-free"> | ||
| <label for="dairy-free" class="diet-label">Dairy-Free</label> | ||
|
|
||
| </div> | ||
| </div> | ||
|
|
||
| <div class="filtername-and-buttons"> | ||
| <h2>Sort by Time</h2> | ||
|
|
||
| <div class="sort-time"> | ||
| <input | ||
| type="radio" | ||
| name="sort-time" | ||
| value="desc" | ||
| id="sort-desc"> | ||
|
|
||
| <label | ||
| for="sort-desc" class="time-label">Longest to Shortest</label> | ||
|
|
||
| <input | ||
| type="radio" | ||
| name="sort-time" | ||
| value="asc" | ||
| id="sort-asc"> | ||
|
|
||
| <label | ||
| for="sort-asc" class="time-label">Shortest to Longest</label> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="filtername-and-buttons"> | ||
| <h2>Not sure?</h2> | ||
| <button id="random-recipe-btn">Random Recipe</button> | ||
| </div> | ||
|
|
||
| </section> | ||
|
|
||
|
|
||
|
|
||
| <section class="recipe-card-container"></section> | ||
| </body> | ||
| </html> |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,98 @@ | ||
| document.addEventListener("DOMContentLoaded", () => { | ||
| // Function to filter and sort recipes, then render them | ||
| function filterAndSortRecipes() { | ||
| const dietInput = document.querySelector('input[name="diet"]:checked'); | ||
| const sortInput = document.querySelector('input[name="sort-time"]:checked'); | ||
| const diet = dietInput ? dietInput.value : "all"; | ||
| const sort = sortInput ? sortInput.value : "desc"; | ||
|
|
||
|
|
||
| // Filter | ||
| let filtered = recipes; | ||
| if (diet !== "all") { | ||
| filtered = recipes.filter(recipe => recipe.diets.includes(diet)); | ||
| } | ||
|
|
||
| // Sort | ||
| if (sort === "asc") { | ||
| filtered = filtered.slice().sort((a, b) => a.readyInMinutes - b.readyInMinutes); | ||
| } else { | ||
| filtered = filtered.slice().sort((a, b) => b.readyInMinutes - a.readyInMinutes); | ||
| } | ||
|
|
||
| renderRecipes(filtered); | ||
| } | ||
|
|
||
| // Event listeners to all filter and sort radio buttons | ||
| document.querySelectorAll('input[name="diet"], input[name="sort-time"]').forEach(input => { | ||
| input.addEventListener("change", filterAndSortRecipes); | ||
| }); | ||
|
|
||
| // Render recipes initially | ||
| filterAndSortRecipes(); | ||
|
|
||
| // Event listener for random recipe button | ||
| const randomBtn = document.getElementById('random-recipe-btn'); | ||
| if (randomBtn) { | ||
| if (recipes.length === 0) { | ||
| randomBtn.disabled = true; // Disable button if no recipes | ||
| } else { | ||
| randomBtn.addEventListener('click', () => { | ||
| // Pick a random recipe from the array | ||
| const randomRecipe = recipes[Math.floor(Math.random() * recipes.length)]; | ||
| // Render only the random recipe | ||
| renderRecipes([randomRecipe]); | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Fetch recipes from data.js | ||
| const apiResult = getRecipes(); | ||
| const recipes = apiResult.recipes || []; | ||
| const apiErrorMessage = apiResult.error || null; | ||
|
|
||
| // Function to render recipe cards | ||
| function renderRecipes(recipesArray) { | ||
| const container = document.querySelector('.recipe-card-container'); | ||
| container.innerHTML = ""; // Clear previous cards | ||
|
|
||
| // Show message when no recipes to display | ||
| if (!recipesArray || recipesArray.length === 0) { | ||
| const message = apiErrorMessage | ||
| ? apiErrorMessage | ||
| : "No recipes found — try different filters or try again later."; | ||
| container.innerHTML = `<p class="no-recipes">${message}</p>`; | ||
| return; | ||
| } | ||
|
|
||
| recipesArray.forEach(recipe => { | ||
| const card = document.createElement('article'); | ||
| card.className = 'recipe-card'; | ||
| card.tabIndex = 0; // Makes card focusable for accessibility | ||
| card.style.cursor = 'pointer'; // Shows pointer on hover | ||
|
||
|
|
||
| // Recipe card content | ||
| let content = ` | ||
| <img class="recipe-images" src="${recipe.image}" alt="${recipe.title}"> | ||
| <h3>${recipe.title}</h3> | ||
| <hr> | ||
| <h4>Ready in: ${recipe.readyInMinutes} min | Servings: ${recipe.servings}</h4> | ||
| ${recipe.diets.length > 0 ? `<h4>Diet: ${recipe.diets.join(', ')}</h4>` : ''} | ||
| <hr> | ||
| <h4>Ingredients</h4> | ||
| <ul> | ||
| ${recipe.extendedIngredients.map(ing => `<li>${ing.name}</li>`).join('')} | ||
| </ul> | ||
| `; | ||
|
|
||
| card.innerHTML = content; | ||
|
|
||
| // Make the whole card clickable | ||
| card.addEventListener('click', () => { | ||
| window.open(recipe.sourceUrl, '_blank'); | ||
| }); | ||
|
|
||
| container.appendChild(card); | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this XMLHttpRequest for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It creates an object that fetches data from the server. It requests for data in the background and updates the page without reloading it for the client. It’s an older way to make HTTP requests that I thought was a bit easier to understand and use in the beginning, and for this smaller project.