-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
117 lines (100 loc) · 3.24 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
// Hide preloader
const preloader = document.querySelector('.preloader')
window.addEventListener('load',()=> {
preloader.classList.add('hide-preloader')
})
// import("./array.js").then((m) => {
// const menu = m.default; // Access the default export
// console.log(menu);
// });
// //Import File
import menu from "./array.js";
let menus = menu();
// console.log(menus);
// Container for Menu content
const sectionContainer = document.querySelector(".section-center");
// Container for Button content
const buttonContainer = document.querySelector(".btn-container");
// Filter Btn
// selecting btn in the html to filter content according to category from the button in the HTML
// const filterBtn = document.querySelectorAll('.filter-btn');
// Filter button needs to be selected now after the dynamic buttons has been added
// So it would be moved downwards after the inside the function box of dynamic buttons
//Writing a function for easy selection of the menu items in all event
const menuItemFunction = (menuPar) => {
let menuItem = menuPar.map((item) => {
// console.log(item);
return `<article class="menu-item">
<img src=${item.img} class="photo" alt=${item.title} />
<!-- Article Information -->
<div class="item-info">
<header>
<h4>${item.title}</h4>
<h4 class="price">$${item.price}</h4>
</header>
<p class="item-text">
${item.desc}
</p>
</div>
</article>`;
});
menuItem = menuItem.join("");
sectionContainer.innerHTML = menuItem;
// console.log(menuItem);
};
//Writing a function for easy selection of the Button items in all event
// dynamic button
const buttonItemFunction = (buttonPar) => {
let buttonValues = buttonPar.reduce(
(values, value) => {
if (!values.includes(value.category)) {
values.push(value.category);
}
return values;
},
["all"]
);
// console.log(buttonValues)
let dynamicButton = buttonValues.map((value) => {
return `
<button class="filter-btn" type="button" data-class="${value}">${value}</button>
`;
});
dynamicButton = dynamicButton.join("");
// console.log(dynamicButton)
buttonContainer.innerHTML = dynamicButton;
// filter btn
// calling filter btn after it has been dynamically added by js
const filterBtn = document.querySelectorAll(".filter-btn");
// Btn to filter by category
filterBtn.forEach((element) => {
element.addEventListener("click", (e) => {
// On clicking a particular btn
const target = e.currentTarget.dataset.class;
// console.log(target)
const targetCategory = menus.filter((filterElement) => {
// console.log(filterElement)
// PS: The category in the imported array menu
// console.log(filterElement.category)
// filtering condition
if (filterElement.category == target) {
return filterElement;
}
});
// console.log(targetCategory);
// Conditional statement for the target 'ALL'
if (target == "all") {
menuItemFunction(menus);
} else {
menuItemFunction(targetCategory);
}
});
});
};
// Content loaded on page load
window.addEventListener("DOMContentLoaded", () => {
// menu item function
menuItemFunction(menus);
// menu item function
buttonItemFunction(menus);
});