-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdinner.js
More file actions
340 lines (304 loc) · 16.7 KB
/
dinner.js
File metadata and controls
340 lines (304 loc) · 16.7 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
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
const dinnerMenu = {
"Classics": ["Basil Orzo",
"Chimichurri Sauce",
"Grilled Mahi Mahi",
"Balsamic Grilled Flank Steak",
"Mango Chutney",
"Grilled Asparagus",
"Sautéed Mushrooms",
"Cilantro Chicken Taco",
"Pork Carnitas Taco",
"Beef Taco",
"Refried Pinto Beans",
"Mexican Rice"],
"Condiments-Garnish": ["Red Pepper Hummus",
"Fresh Jalapeno"],
"Deli": ["Ham & Cheese on White",
"Swiss Cheese",
"Whole Grain Sub Roll",
"Tomato Basil Tortilla",
"Multi Grain Bread",
"Garlic Grilled Chicken",
"Sliced Plum Tomatoes",
"Chipotle Mayonnaise",
"Smoked Ham",
"White Sub Roll",
"American Cheese",
"White Bread",
"Olive Oil",
"Hard-boiled Egg",
"Wheat Pita Bread",
"Spinach Herb Tortilla",
"Hummus",
"Honey Mustard Dressing",
"Sliced Red Onions",
"Fresh Spinach",
"Flour Tortilla",
"Roast Beef",
'10" Whole Wheat Tortilla',
"Deli Turkey Breast",
"Shredded Lettuce",
"Cheddar Cheese",
"Dill Pickle Chip",
"Hot Sliced Banana Peppers",
"Red Wine Vinegar"],
"Dessert": ["Two Oatmeal Raisin Cookies",
"Chocolate Chip Cookies",
"Two Butter Sugar Cookies",
"Sundae Bar",
"Have A Nice Day!",
"Chocolate Ice Cream",
"Chocolate Pudding",
"Chocolate Mousse",
"Vanilla Ice Cream",
'Apple Pie',
'Vanilla Pudding',
"Chocolate Sheet Cake"],
"Grill": ["American Cheese",
"Chicken & Cheese Quesadilla",
"Bbq Burger & Crispy Onions"],
"Kosher": ["Cucumber Tomato Salad",
"Roasted Carrots",
"Roasted Marinated Vegetables",
"Tabbouleh Salad",
"Mixed Olives",
"Yellow Rice With Turmeric",
"Shish Taouk (Chicken Kebabs)",
"Medium Thick & Chunky Salsa",
"Pickled Jalapeno Peppers",
"Beef Taco Meat",
"Sauteed Mushrooms",
'Fresh Tomatoes',
'Diced Yellow Onions',
'6" Yellow Corn Tortillas',
'Vegan Shredded Cheddar Cheese',
'Cilantro Rice',
"Sauteed Peppers And Onions"],
"My Kitchen": ["Chicken Caesar Salad"],
"Pasta": ["Cajun Chicken Pasta Bowl",
"Roasted Vegetable Lasagna With Marinara",
"Natural Meatball",
"Tricolor Rotini With Beef, Peppers, & Onions",
"Marinara Sauce"],
"Pizza": ["Cheese Pizza",
"Bbq Chicken Pizza",
"Formaggio Pizzarito"],
"Salad Bar": ["Bacon",
"Spring Lettuce Mix",
"Craisins",
"Feta Cheese Crumbles",
"Guacamole",
"Fresh Jalapeno",
"Shredded Carrots",
"Honey Mustard Dressing",
"Hot Sliced Banana Peppers",
"Hard-Boiled Egg",
"Wheat Pita Bread",
"Flax Seeds",
"Kalamata Olives",
"Shredded Cheddar Cheese",
"Roasted Eggplant",
"Traditional Caesar Dressing",
"Extra Firm Tofu",
"Spiced Pepitas",
"Olive Oil",
"Fresh Cantaloupe",
"Cucumber",
"Extra Firm Tofu",
"Balsamic Chicken Breast",
"Cooked Farro",
"Cherry Tomatoes",
"Yellow Corn Kernel",
"Lemon Wedge",
"Chickpeas",
"Cucumber",
"Shredded Parmesan Cheese",
"Chopped Romaine",
"Hummus",
"Red Quinoa",
"Edamame",
"French Bread Croutons",
"Fresh Spinach"],
"Simple Servings": ["Grilled Chicken Breast",
"Lemony Chickpea Salad",
"Cilantro Leaf",
"Cilantro Basmati Rice",
"House Made Kidney Beans",
"Citrus Herb Tilapia",
"Roasted Zucchini",
"Yellow Rice",
"Pineapple Cucumber Salad",
"Grilled Steak",
"Roasted Brussels Sprouts",
"Aromatic Lentils",
"Herbed Roasted Potato Wedges",
"Grilled Asparagus",
"Roasted Broccoli",
"Black Beans",
"Cilantro Cucumber Salad",
"Traditional Rotisserie Chicken",
"Seasoned Catfish Fillet",
"White Rice"],
"Soup": ["Thick & Zesty Chili",
"Aged Cheddar Cheese Sauce",
"Tomato Basil Soup",
"American Bounty Vegetable Soup"],
"Vegan Station": ["Aromatic Lentils",
"Sweet Potato And Black Bean Chili",
"Quinoa Primavera",
"Marinated Portobello With Quinoa Pilaf",
"Black Beans",
"Millet",
"Thai Eggplant Salad",
"Traditional Garden Salad"]
};
// Function to generate a random number between min (inclusive) and max (inclusive)
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Function to generate a random selection of items from an array within a specified range
function getRandomItems(array, minNumItems, maxNumItems) {
const numItems = getRandomNumber(minNumItems, maxNumItems);
const shuffledArray = array.sort(() => 0.5 - Math.random()); // Shuffle array
return shuffledArray.slice(0, numItems); // Get first numItems
}
// Function to generate HTML for a menu section with a random selection of items within a range
function generateRandomMenuSection(sectionName, items, minNumItems, maxNumItems) {
const randomItems = getRandomItems(items, minNumItems, maxNumItems);
const sectionHTML = document.createElement('div');
sectionHTML.innerHTML = `<h2>${sectionName}</h2><ul>${randomItems.map(item => {
const averageRating = calculateAverageRating(item);
return `<li class="rating-item" data-foodname="${item}">${item} <div class="rating"> <span data-rating="1">★</span> <span data-rating="2">★</span> <span data-rating="3">★</span> <span data-rating="4">★</span> <span data-rating="5">★</span> </div> <span class="average-rating">Average Rating: ${averageRating.toFixed(1)}</span> </li>`;
}).join('')}</ul>`;
return sectionHTML;
}
// Function to generate the entire menu with random items within a range
function generateRandomMenu(menuData, minNumItems, maxNumItems) {
const menuContainer = document.getElementById('dinnerMenu');
for (const sectionName in menuData) {
if (menuData.hasOwnProperty(sectionName)) {
const sectionItems = menuData[sectionName];
const sectionHTML = generateRandomMenuSection(sectionName, sectionItems, minNumItems, maxNumItems);
menuContainer.appendChild(sectionHTML);
}
}
}
// Function to generate the entire menu with random items within a range and store it in local storage
function generateAndStoreRandomMenu(menuData, minNumItems, maxNumItems) {
const menuContainer = document.getElementById('dinnerMenu');
let generatedDinnerMenu = {}; // Store the generated menu
for (const sectionName in menuData) {
if (menuData.hasOwnProperty(sectionName)) {
const sectionItems = menuData[sectionName];
const randomItems = getRandomItems(sectionItems, minNumItems, maxNumItems);
generatedDinnerMenu[sectionName] = randomItems; // Store section with random items
const sectionHTML = generateRandomMenuSection(sectionName, randomItems);
menuContainer.appendChild(sectionHTML);
}
}
localStorage.setItem("generatedDinnerMenu", JSON.stringify(generatedDinnerMenu)); // Store the generated menu in local storage
}
// Function to generate the entire menu from stored data in local storage
function generateMenuFromStorage() {
const menuContainer = document.getElementById('dinnerMenu');
const storedMenu = JSON.parse(localStorage.getItem("generatedDinnerMenu"));
let averageRatings = JSON.parse(localStorage.getItem("averageRatings")) || {};
for (const sectionName in storedMenu) {
if (storedMenu.hasOwnProperty(sectionName)) {
const sectionItems = storedMenu[sectionName];
const sectionHTML = document.createElement('div');
sectionHTML.innerHTML = `<h2>${sectionName}</h2><ul>${sectionItems.map(item => {
const averageRating = averageRatings[item] || 0;
return `<li class="rating-item" data-foodname="${item}">${item} <div class="rating"> <span data-rating="1">★</span> <span data-rating="2">★</span> <span data-rating="3">★</span> <span data-rating="4">★</span> <span data-rating="5">★</span> </div> <span class="average-rating">Average Rating: ${averageRating.toFixed(1)}</span> </li>`;
}).join('')}</ul>`;
menuContainer.appendChild(sectionHTML);
}
}
}
function calculateAverageRating(foodName) {
let ratings = JSON.parse(localStorage.getItem("ratings")) || [];
let totalRating = 0;
let ratingCount = 0;
ratings.forEach(rating => {
if (rating["food-Name"] === foodName) {
totalRating += rating.stars;
ratingCount++;
console.log(totalRating)
console.log(`Matching rating found for foodName: ${foodName}, stars: ${rating.stars}`);
}
});
const averageRating = ratingCount > 0 ? totalRating / ratingCount : 0;
console.log(`Average rating for foodName: ${foodName}, averageRating: ${averageRating}`);
// Store the average rating in localStorage
let averageRatings = JSON.parse(localStorage.getItem("averageRatings")) || {};
averageRatings[foodName] = averageRating;
localStorage.setItem("averageRatings", JSON.stringify(averageRatings));
return averageRating;
}
function attachEventListeners() {
let stars = document.querySelectorAll(".rating span");
stars.forEach(star => {
star.addEventListener("click", function() {
// Prevent further clicks if any star in the same food item has already been clicked
let clickedStars = this.parentElement.querySelectorAll('span[data-clicked="true"]');
if (clickedStars.length > 0) {
return;
}
let clickedRating = parseInt(this.dataset.rating);
// Set data-clicked attribute for the clicked star and stars before it
for (let i = 1; i <= clickedRating; i++) {
let star = this.parentElement.querySelector(`span[data-rating="${i}"]`);
if (star) star.setAttribute("data-clicked", "true");
}
let foodId = this.parentElement.parentElement.dataset.foodid;
let foodName = this.parentElement.parentElement.dataset.foodname;
console.log("Clicked star for foodName: ${foodName}");
let data = {
"stars": clickedRating,
"food-Id": foodId,
"food-Name": foodName
};
let ratings = localStorage.getItem("ratings") ? JSON.parse(localStorage.getItem("ratings")) : [];
ratings.push(data); // Flatten the array
localStorage.setItem("ratings", JSON.stringify(ratings));
// Update the average rating for the clicked food item
let averageRating = calculateAverageRating(foodName);
let foodItemElement = document.querySelector(`li[data-foodname="${foodName}"]`);
console.log(`Found food item element: ${foodItemElement}`);
if (foodItemElement) {
let averageRatingElement = foodItemElement.querySelector('.average-rating');
console.log(`Found average rating element: ${averageRatingElement}`);
if (averageRatingElement) {
averageRatingElement.textContent = `Average Rating: ${averageRating.toFixed(1)}`;
console.log(`Updated average rating for foodName: ${foodName}, averageRating: ${averageRating}`);
}
}
});
});
}
// Minimum and maximum number of items to show per section
const minNumItemsPerSection = 5;
const maxNumItemsPerSection = 10;
// Generate the menu with random items within a range when the page loads
window.onload = function() {
// Check if the menu is already stored in local storage
let storedMenu = localStorage.getItem("generatedDinnerMenu");
if (!storedMenu){
generateRandomMenu(dinnerMenu, minNumItemsPerSection, maxNumItemsPerSection);
let generatedDinnerMenu = document.getElementById('dinnerMenu').innerHTML;
localStorage.setItem("generatedDinnerMenu", generatedDinnerMenu);
} else { // If the menu is stored, retrieve it from local storage
document.getElementById('dinnerMenu').innerHTML = storedMenu;
updateAverageRatings();
}
attachEventListeners();
};
function updateAverageRatings() {
const foodItems = document.querySelectorAll('.rating-item');
foodItems.forEach(item => {
const foodName = item.dataset.foodname;
const averageRating = calculateAverageRating(foodName);
const averageRatingElement = item.querySelector('.average-rating');
averageRatingElement.textContent = `Average Rating: ${averageRating.toFixed(1)}`;
});
}