-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
485 lines (413 loc) · 20.1 KB
/
app.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
////////////
///Need to fix sometimes same ingredient but 1 rcipe does not say how much so need to add a bit
/// OK /// Need to add serving number in list of ingredients
/// Find a way for serving number and sum list ingredients for 6 personnes but also when not 6 personnes
/// Need to fix display title list ingredients etc
/// OK /// Need to add recipeid in the original data.json from colab notebook
/// Add button to copy the list for shopping
/// In aggregate ingredients add list of recipes and number of serving per recipe
/// better style for the Show sum ingredients
/// Create another dialog to say if we are sure to remove a favorite
/// Save button to save list of favorites recipes in json or other format
///Return values of modal.title modal.ingredients modal serving to "" when the modal is closed (to avoid that it is reused in another modal if the functio nthat calls the modal does not implement t ochange these)
////////////////////////
////// LARGE CHANGE TO DO : modify code so that i nfavorites and recipes it adds only the "recipeID" and then use it to retrieve recipes directly fro mdatabase instead of saving the recipes themselves in favorites and slected recipes in local storage
////////////////////////
// Function to load recipes from database data.json
async function loadRecipes() {
try {
const response = await fetch('./data.json');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error("Could not load recipes:", error);
return {};
}
}
function getRandomMainCourseRecipes(data, count = 5) {
const mainCourseRecipes = Object.values(data).filter(recipe => recipe.category === 'platprincipal' && (recipe.difficulty === 'facile' || recipe.difficulty === 'tr\u00e8s facile') && recipe.temps_prep_min <= 30);//&& recipe.country === "Mexico"
const shuffled = mainCourseRecipes.sort(() => 0.5 - Math.random());
return shuffled.slice(0, count);
}
function createSetFilter(data,property){
const uniqueProperty = new Set();
console.log(property);
// Collect unique property values
Object.values(data).forEach(item => {
if (item[property]) {//we cannot use item.property here we need to use item[property] because our property comes as a string when we call the function createSetFilter
uniqueProperty.add(item[property]);
}
});
// Convert Set to Array and sort
const sortedProperty= Array.from(uniqueProperty).sort();
return sortedProperty
}
function allRecipesRandomPage(data) {
const container = document.getElementById('recipesContainer');
const filtListCont = document.getElementById("filterDiv");
//make selection for category
{
const selectCategory = document.createElement('select');
selectCategory.id= "selectCategory";
selectCategory.required=true;// This indicates that an option with a non-empty string value must be selected.
// This allows to create an option with "" value to use as placeholder because "select" has no placeholder otherwise
const option = document.createElement('option');//for our placeholder
option.value = "";//for our placeholder
option.textContent = "Select a dish category";//for our placeholder
selectCategory.appendChild(option);//for our placeholder
filtListCont.appendChild(selectCategory);
const listCategory = createSetFilter(data,"category");
listCategory.forEach(category => {
const option = document.createElement('option');
option.value = category;
option.textContent = category;
selectCategory.appendChild(option);
});
}
//make selection for difficulty
{
const selectDifficulty = document.createElement('select');
selectDifficulty.id= "selectDifficulty";
selectDifficulty.required=true;// This indicates that an option with a non-empty string value must be selected.
// This allows to create an option with "" value to use as placeholder because "select" has no placeholder otherwise
const option = document.createElement('option');//for our placeholder
option.value = "";//for our placeholder
option.textContent = "Select a difficulty";//for our placeholder
selectDifficulty.appendChild(option);//for our placeholder
filtListCont.appendChild(selectDifficulty);
const listDifficulty = createSetFilter(data,"difficulty");
listDifficulty.forEach(difficulty => {
const option = document.createElement('option');
option.value = difficulty;
option.textContent = difficulty;
selectDifficulty.appendChild(option);
});
}
container.innerHTML = '';
}
function displayRecipes(recipes, container = document.getElementById('recipesContainer')) {
container.innerHTML = '';
recipes.forEach(recipe => {
const recipeDiv = document.createElement('div');
recipeDiv.classList.add('recipe');
const isFavorite = isFavoriteRecipe(recipe.name);
const isSelected = isSelectedRecipe(recipe.name);
recipeDiv.innerHTML = `
<img src="${recipe.IMG}" alt="${recipe.name}">
<div class="recipe-info" >
<h2>${recipe.name}</h2>
<p>Selection: ${recipe.selection}</p>
<p>Category: ${recipe.category}</p>
<p>Prep Time: ${recipe.temps_prep}</p>
<p>Total Time: ${recipe.temps_total}</p>
<p>Difficulty: ${recipe.difficulty}</p>
<a href="${recipe.URL}" target="_blank">View Recipe</a>
<button class="view-ingredients">View Ingredients</button>
</div>
<i class="favorite-icon ${isFavorite ? 'fas' : 'far'} fa-heart ${isFavorite ? 'active' : ''}"></i>
<i class="list-icon fas fa-list ${isSelected ? 'active' : ''}"></i>
`;
//console.log(recipe)
//<i class="list-icon fas fa-list ${isSelected ? 'active' : ''}"></i>
container.appendChild(recipeDiv);
const favoriteIcon = recipeDiv.querySelector('.favorite-icon');
favoriteIcon.addEventListener('click', (e) => {
e.stopPropagation();
toggleFavorite(recipe, favoriteIcon);
});
const listIcon = recipeDiv.querySelector('.list-icon');
listIcon.addEventListener('click', (e) => {
e.stopPropagation();
toggleSelection(recipe, listIcon);
});
const viewIngredientsButton = recipeDiv.querySelector('.view-ingredients');
viewIngredientsButton.addEventListener('click', (e) => {
e.stopPropagation();
showIngredients(recipe);
});
});
}
// Function to display and store last random recipes
function displayRandomRecipes(data) {
const randomRecipes = getRandomMainCourseRecipes(data);
displayRecipes(randomRecipes);
storelast(randomRecipes);
}
function toggleFavorite(recipe, icon) {
const favorites = JSON.parse(localStorage.getItem('favorites')) || [];
const index = favorites.findIndex(fav => fav.name === recipe.name);
if (index === -1) {
favorites.push(recipe);
icon.classList.remove('far');//fas is icon in style regular (see Font Awesome)
icon.classList.add('fas', 'active');//fas is icon in style solid (see Font Awesome)
} else {
favorites.splice(index, 1);
icon.classList.remove('fas', 'active');
icon.classList.add('far');
}
localStorage.setItem('favorites', JSON.stringify(favorites));
}
function toggleSelection(recipe, icon) {
const selected = JSON.parse(localStorage.getItem('selectedRecipes')) || [];
const index = selected.findIndex(sel => sel.name === recipe.name);
if (index === -1) {
selected.push(recipe);
icon.classList.remove('fas');//fas is icon in style solid (see Font Awesome) I cannot use the far "list" icon because it is in paid version
icon.classList.add('fas', 'active');
} else {
selected.splice(index, 1);
icon.classList.remove('fas', 'active');
icon.classList.add('fas');
}
localStorage.setItem('selectedRecipes', JSON.stringify(selected));
}
//function to store last recipes shown
function storelast(recipes) {
localStorage.setItem('last_recipes', JSON.stringify(recipes));
}
function isFavoriteRecipe(recipeName) {
const favorites = JSON.parse(localStorage.getItem('favorites')) || [];
return favorites.some(fav => fav.name === recipeName);
}
function isSelectedRecipe(recipeName) {
const selected = JSON.parse(localStorage.getItem('selectedRecipes')) || [];
return selected.some(sel => sel.name === recipeName);
}
function showFavorites() {
const favorites = JSON.parse(localStorage.getItem('favorites')) || [];
displayRecipes(favorites);
//console.log(favorites)
}
//added
//function showLast() {
// const last_recipes = JSON.parse(localStorage.getItem('last_recipes')) || [];
// displayRecipes(last_recipes);
//}
function showSelectionOfWeek() {
const selected = JSON.parse(localStorage.getItem('selectedRecipes')) || [];
//const selectionContainer = document.getElementById('selectionOfWeekContainer');
//selectionContainer.style.display = 'grid';
// selectionContainer.innerHTML = '<h2>Selection of the Week</h2>';
//displayRecipes(selected, selectionContainer);
displayRecipes(selected);
//document.getElementById('recipesContainer').style.display = 'none';
}
function showIngredients(recipe) {
const modal = document.getElementById('ingredientsModal');
const modalTitle = document.getElementById('modalTitle');
const modalrecipeName = document.getElementById('modalrecipeName');
const ingredientsList = document.getElementById('ingredientsList');
const serving = document.getElementById('servingDefault');
ingredientsList.innerHTML = '';
const ingredientsObj = recipe.ingredients_default_;
let ingredientString = "";
for (const [ingredient, details] of Object.entries(ingredientsObj)) {
const { count, unit } = details;
ingredientString = `${ingredient}: ${count} ${unit}; `;
const li = document.createElement('li');
li.textContent = ingredientString
ingredientsList.appendChild(li);
modalTitle.textContent = `Liste of ingredients`;
modalrecipeName.textContent = `${recipe.name}`;
serving.textContent = `Pour ${recipe.serving_nb} ${recipe.serving_unit} `;
modal.style.display = 'block';
}
}
function closeModal() {
const modal = document.getElementById('ingredientsModal');
modal.style.display = 'none';
}
// function to aggregate ingredients from the "selected recipes" (to sum al lsimilar ingredients for shopping)
function aggregateIngredients(selectedRecipes) {
const ingredientMap = {};
selectedRecipes.forEach(recipe => {
const ingredients = recipe.ingredients_default_;
for (const [ingredient, details] of Object.entries(ingredients)) {
const unit = details.unit;
const count = parseFloat(details.count) || 0;
if (!ingredientMap[ingredient]) {
ingredientMap[ingredient] = { unit, count };
} else if (ingredientMap[ingredient].unit === unit) {
ingredientMap[ingredient].count += count;
} else {
// Handle different units (if necessary, e.g., convert units)
console.warn(`Different units found for ${ingredient}: ${ingredientMap[ingredient].unit} and ${unit};`);
}
}
});
return ingredientMap;
}
// Function to display aggregated ingredients in the modal dialog
function showAggregatedIngredients() {
const modal = document.getElementById('ingredientsModal');
const modalTitle = document.getElementById('modalTitle');
const ingredientsList = document.getElementById('ingredientsList');
const modalrecipeName = document.getElementById('modalrecipeName');
const selectedRecipes = JSON.parse(localStorage.getItem('selectedRecipes')) || [];
var recipesNames = ''
const serving = document.getElementById('servingDefault');
ingredientsList.innerHTML = '';
const aggregatedIngredients = aggregateIngredients(selectedRecipes);
selectedRecipes.forEach(recipe => {
recipesNames += `${recipe.name}, `;
})
for (const [ingredient, details] of Object.entries(aggregatedIngredients)) {
const { count, unit } = details;
const ingredientString = `${ingredient}: ${count.toFixed(2)} ${unit};`;
const li = document.createElement('li');
li.textContent = ingredientString;
ingredientsList.appendChild(li);
}
modalTitle.textContent = 'Aggregated Ingredients for Selected recipes of the week';
modalrecipeName.textContent = `${recipesNames}`;
serving.textContent = ``;
modal.style.display = 'block';
}
function CopyToClipboard() {
var copiedText =''
var r1=document.getElementById("modalrecipeName").textContent;
var r2=(document.getElementById("ingredientsList").textContent).replaceAll(";","\n");
console.log(r2)
copiedText = `${r1}\n\n${r2}`;
navigator.clipboard.writeText(copiedText);
}
///////////////////////// LEFT SIDEBAR MENU EVENT LISTENERS ///////////////////////////////////////////////////////
document.addEventListener('DOMContentLoaded', function() {
const menuToggle = document.querySelector('.menu-toggle');
const menuIcon = document.getElementById('menu-icon');
const sidebar = document.querySelector('.sidebar');
const bodySel = document .querySelector('body');
menuToggle.addEventListener('click', function() {
sidebar.classList.toggle('open');
// this toggle means turn on or off the class .open in the sidebar depending on if it was on or off before running the button (that is defiend by document.querySelector('.sidebar'); this does not remove the .class .sidebar' but it add the class .open to .sidebar.
//so if toggle on this will trigger the css style .sidebar.open that has left =0 so wil lshow the sidebar because before was left =-250.
if (sidebar.classList.contains('open')) {
console.log("Sidebar is open")
menuIcon.classList.remove('fa-bars');
menuIcon.classList.add('fa-times');
bodySel.classList.add('blockedScroll');//this add the class "blockedScroll" to bodysel which is the HTML element "Body" selected just above using const bodySel = document .querySelector('body');
} else {
menuIcon.classList.remove('fa-times');
menuIcon.classList.add('fa-bars');
bodySel.classList.remove('blockedScroll');
}
});
});
///////////////////////////
function saveData() {
const last_recipesList = JSON.parse(localStorage.getItem('last_recipes')) || [];
const favorites = JSON.parse(localStorage.getItem('favorites')) || [];
const selected = JSON.parse(localStorage.getItem('selectedRecipes')) || [];
const data = JSON.stringify({
version: "v7.1",
last_recipes: last_recipesList,
favorites: favorites,
selected: selected
}, null, 4); // Indentation of 4 spaces
const blob = new Blob([data], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'random_recipe.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
//////////////////////////////////////////////
function loadData() {
if (confirm(`Loading data will overwrite current favorites and recipes of the week.
You should save current state to file before continuing`)) {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'application/json';
input.onchange = function(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
const data = JSON.parse(e.target.result);
const last_recipes = data.last_recipes || [];
const favorites = data.favorites || [];
const selected = data.selected || [];
localStorage.setItem('last_recipes', JSON.stringify(last_recipes));
localStorage.setItem('favorites', JSON.stringify(favorites));
localStorage.setItem('selectedRecipes', JSON.stringify(selected));
displayRecipes(last_recipes);
};
reader.readAsText(file);
};
input.click();
};
}
/////////////////////////////////////////////
async function initializeApp() {
const data = await loadRecipes();
document.getElementById('newRecipesButton').addEventListener('click', () => {
const randomRecipes = getRandomMainCourseRecipes(data);
//displayRecipes(randomRecipes);
displayRandomRecipes(data)
document.getElementById('recipesContainer').style.display = 'grid';
document.getElementById('selectionOfWeekContainer').style.display = 'none';
});
document.getElementById('newRecipesButton1').addEventListener('click', () => {
const randomRecipes = getRandomMainCourseRecipes(data);
//displayRecipes(randomRecipes);
displayRandomRecipes(data)
document.getElementById('recipesContainer').style.display = 'grid';
document.getElementById('selectionOfWeekContainer').style.display = 'none';
});
document.getElementById('showsumIngredientsButton').addEventListener('click', () => {
const randomRecipes = getRandomMainCourseRecipes(data);
//displayRecipes(randomRecipes);
showAggregatedIngredients()
document.getElementById('recipesContainer').style.display = 'grid';
document.getElementById('selectionOfWeekContainer').style.display = 'none';
});
document.getElementById('copytoclipboardButton').addEventListener('click', () => {
CopyToClipboard()
});
document.getElementById('favorites').addEventListener('click', (e) => {
e.preventDefault();
showFavorites();
document.getElementById('recipesContainer').style.display = 'grid';
document.getElementById('selectionOfWeekContainer').style.display = 'none';
});
document.getElementById('shuffler').addEventListener('click', (e) => {
e.preventDefault();
//const randomRecipes = getRandomMainCourseRecipes(data);
//displayRecipes(randomRecipes);
//showLast();
const last_recipes = JSON.parse(localStorage.getItem('last_recipes')) || [];
displayRecipes(last_recipes);
document.getElementById('recipesContainer').style.display = 'grid';
document.getElementById('selectionOfWeekContainer').style.display = 'none';
});
document.getElementById('selectionOfWeek').addEventListener('click', (e) => {
e.preventDefault();
showSelectionOfWeek();
document.getElementById('recipesContainer').style.display = 'grid';
document.getElementById('selectionOfWeekContainer').style.display = 'none';
});
// Close modal when clicking on the close button or outside the modal
document.querySelector('.close').addEventListener('click', closeModal);
window.addEventListener('click', (event) => {
const modal = document.getElementById('ingredientsModal');
if (event.target === modal) {
closeModal();
}
});
document.getElementById('allRecipesRandomPageBtn').addEventListener('click', () => {
allRecipesRandomPage(data);
document.getElementById('recipesContainer').style.display = 'grid';
document.getElementById('selectionOfWeekContainer').style.display = 'none';
});
// Initial load
//const initialRecipes = getRandomMainCourseRecipes(data);
const last_recipes = JSON.parse(localStorage.getItem('last_recipes')) || [];
displayRecipes(last_recipes);
}
// Start the app
initializeApp();