forked from amri-tah/burntbrotta.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (72 loc) · 3.34 KB
/
script.js
File metadata and controls
77 lines (72 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
window.onload = function () {
const urlParams = new URLSearchParams(window.location.search);
const recipeId = urlParams.get("id");
fetch("recipes.json")
.then((response) => response.json())
.then((recipes) => {
const recipe = recipes.find((r) => r.id === recipeId);
if (recipe) {
document.getElementById("recipe-name").innerText = recipe.name.toUpperCase();
document.getElementById("recipe-image").src = recipe.image;
document.getElementById("recipe-image").height = 600;
document.getElementById("recipe-image").width = 400;
document.getElementById("recipe-image").alt = recipe.name;
document.getElementById("recipe-description").innerText = recipe.description;
document.getElementById("recipe-time").innerText = recipe.time;
document.getElementById("recipe-servings").innerText = recipe.servings;
document.getElementById("recipe-difficulty").innerText = recipe.difficulty;
const ingredientsList = document.getElementById("ingredients-list");
recipe.ingredients.forEach((ingredient) => {
const li = document.createElement("li");
li.innerText = ingredient;
ingredientsList.appendChild(li);
});
const instructionsList = document.getElementById("instructions-list");
instructionsList.style.listStyleType = "none";
instructionsList.style.paddingLeft = "0";
recipe.instructions.forEach((instruction) => {
const li = document.createElement("li");
li.innerHTML = `${instruction}<br><br>`;
instructionsList.appendChild(li);
});
const renderReviews = () => {
const container = document.getElementById("reviews-container");
container.innerHTML = "";
if (recipe.reviews.user_reviews.length === 0) {
container.innerHTML = `
<div class="no-reviews-message">
<p>No reviews yet! Be the first to add one...</p>
</div>`;
} else {
recipe.reviews.user_reviews.forEach((review) => {
const reviewHTML = `
<div class="review-item">
<div class="review-header">
<img src="images/defaultProfile.jpg" class="profile-pic">
<div class="review-details">
<h3 class="reviewer-username">${review.username}</h3>
<div class="review-stars-and-date">
<p class="review-stars">${'★'.repeat(review.rating)}${'☆'.repeat(5 - review.rating)}</p>
<span class="review-stars-seperator">·</span>
<p class="review-date">Reviewed on ${new Date(review.postedAt).toLocaleDateString('en-IN')} at ${new Date(review.postedAt).toLocaleTimeString()}</p>
</div>
</div>
</div>
<div class="review-content">
<p class="review-text">${review.comment}</p>
</div>
</div>
<hr class="review-seperator">`;
container.innerHTML += reviewHTML;
});
}
};
renderReviews();
} else {
alert("Recipe not found!");
}
})
.catch((error) => {
console.error("Error fetching the recipe data:", error);
});
};