Skip to content
Open
Show file tree
Hide file tree
Changes from 15 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
14 changes: 14 additions & 0 deletions data.js
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();

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?

Copy link
Author

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.


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." };
}
}
Binary file added images/ruth-georgiev-Q4CLQ1BDybU-unsplash.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 94 additions & 0 deletions index.html
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>
1 change: 1 addition & 0 deletions response.json

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions script.js
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

Choose a reason for hiding this comment

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

this should be controlled in the css file, just as the rest of the .recipe-card styling


// 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);
});
}
Loading