-
Notifications
You must be signed in to change notification settings - Fork 1
/
paginated-recipes.js
126 lines (111 loc) · 3.74 KB
/
paginated-recipes.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
/**
* INPUT:
* <paginated-recipes size="10" .recipes=${[...]}>
*
* ----
* OUTPUT:
*
* <div id="recipe-result">
* <div class="paginated-container">
* <div class="pages">
* <button class="btn btn-info">1</button> recipes/10
* </div>
* </div>
* <div class="page"> x recipes/10
* <div class="recipe"></div> x10
* </div>
* </div>
*/
class PaginatedRecipes extends HTMLElement {
get size() {
return this.getAttribute('size');
}
set size(value) {
this.setAttribute('size', value);
}
get recipes() {
return this._recipes;
}
set recipes(value) {
this._recipes = value;
this.render();
}
connectedCallback() {
this.render();
}
recipeTemplate(recipe) {
return `
<div class="recipe">
<div><img src="${recipe.picture}"></div>
<div class="recipe__content">
<h4 class="recipe__title"><a href="./recipe.html?id=${recipe.id}">${recipe.name}</a></h4>
<div class="nutrition-values-per-serving">
Carbs: ${recipe.carbsPerServing.toFixed(2)} gr
<text class="nutrition-values-recipe">|</text>
Net Carbs: ${recipe.netCarbsPerServing.toFixed(2)} gr
<text class="nutrition-values-recipe">|</text>
Fats: ${recipe.fatsPerServing.toFixed(2)} gr
<text class="nutrition-values-recipe">|</text>
Protein: ${recipe.proteinPerServing.toFixed(2)} gr
<text class="nutrition-values-recipe">|</text>
Calories: ${recipe.caloriesPerServing.toFixed(2)}
</div>
<p class="recipe__description">
${recipe.description}
</p>
</div>
</div>
`;
}
render() {
const recipes = this.recipes || [];
const amountOfRecipes = recipes.length;
const pages = Math.ceil(amountOfRecipes / 10);
const recipesByTen = [[]];
let counter = 0;
recipes.forEach((item, index) => {
if (recipesByTen[counter].length === 10) {
counter++;
recipesByTen[counter] = [];
}
recipesByTen[counter].push(item);
});
let recipeData = ``;
recipesByTen.forEach((chunk, index) => {
recipeData += `
<div class="page" style="display:none" id="page-${index + 1}">
`;
chunk.forEach((recipe, index) => {
recipeData += `
${this.recipeTemplate(recipe)}
`;
})
recipeData += `
</div>
`;
})
this.innerHTML = `
<div class="pagination-container">
<div class="pages">
${Array(pages).fill('').map((item, index) => `<button class="btn btn-info">${index + 1}</button>`).reduce((acc, curr) => acc.concat(curr), '')}
</div>
</div>
${recipeData}
`;
const pagesEl = this.querySelector('.pages');
const buttonEls = Array.from(pagesEl.children);
const pageEls = Array.from(document.querySelectorAll('.page'));
pageEls[0].style.display = 'block';
buttonEls.forEach((btn, index) => {
btn.addEventListener('click', () => {
pageEls.forEach((page, pageIndex) => {
page.style.display = 'none';
if (pageIndex === index) {
page.style.display = 'block';
}
})
})
})
}
}
customElements.define('paginated-recipes', PaginatedRecipes);