-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecipeView.js
136 lines (116 loc) · 4.98 KB
/
recipeView.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
import { elements } from './base';
import { Fraction } from 'fractional';
export const clearRecipe = () => {
elements.recipe.innerHTML = '';
};
// look into documentation of fraction.js @https://github.com/ekg/fraction.js/
const formatCount = count => {
if (count) {
// count = 2.5 --> 2 1/2
// count = 0.5 --> 1/2
const [int, dec] = count.toString().split('.').map(el => parseInt(el, 10));
// count = 2 --> 2 // there's no dec
if (!dec) return count;
// count = 0.5 --> 1/2
if (int === 0) {
const fr = new Fraction(count);
return `${fr.numerator}/${fr.denominator}`;
} else {
// count = 2.5 --> 2 1/2
// if we create a fraction for 2.5, we will get 5/2, which we don't want. Therefore,
// we will create a fraction only for 0.5 and append it to 2. For that, we would
// take the difference between the count and the int and pass it to the Fraction's constructor.
const fr = new Fraction(count - int);
const numerator = parseInt(fr.numerator.toString().slice(0, 1));
const denominator = fr.denominator.toString();
const sliceTill = (denominator.length > 1) ? 2 : 1;
return `${int} ${numerator}/${parseInt(denominator.slice(0, sliceTill))}`;
}
}
return 1;
};
const createIngredient = ingredient => `
<li class="recipe__item">
<svg class="recipe__icon">
<use href="img/icons.svg#icon-check"></use>
</svg>
<div class="recipe__count">${formatCount(ingredient.count)}</div>
<div class="recipe__ingredient">
<span class="recipe__unit">${ingredient.unit}</span>
${ingredient.ingredient}
</div>
</li>
`;
export const renderRecipe = recipe => {
// we get the markup under .recipe class in index.html
const markup = `
<figure class="recipe__fig">
<img src="${recipe.img}" alt="${recipe.title}" class="recipe__img">
<h1 class="recipe__title">
<span>${recipe.title}</span>
</h1>
</figure>
<div class="recipe__details">
<div class="recipe__info">
<svg class="recipe__info-icon">
<use href="img/icons.svg#icon-stopwatch"></use>
</svg>
<span class="recipe__info-data recipe__info-data--minutes">${recipe.time}</span>
<span class="recipe__info-text"> minutes</span>
</div>
<div class="recipe__info">
<svg class="recipe__info-icon">
<use href="img/icons.svg#icon-man"></use>
</svg>
<span class="recipe__info-data recipe__info-data--people">${recipe.servings}</span>
<span class="recipe__info-text"> servings</span>
<div class="recipe__info-buttons">
<button class="btn-tiny">
<svg>
<use href="img/icons.svg#icon-circle-with-minus"></use>
</svg>
</button>
<button class="btn-tiny">
<svg>
<use href="img/icons.svg#icon-circle-with-plus"></use>
</svg>
</button>
</div>
</div>
<button class="recipe__love">
<svg class="header__likes">
<use href="img/icons.svg#icon-heart-outlined"></use>
</svg>
</button>
</div>
<div class="recipe__ingredients">
<ul class="recipe__ingredient-list">
<!--
We don't know the number of ingredients that'll be in a recipe, therefore, we have to loop
through the number of ingredients and then create the list element for each ingredient of recipe
-->
${recipe.ingredients.map(el => createIngredient(el)).join('')}
</ul>
<button class="btn-small recipe__btn">
<svg class="search__icon">
<use href="img/icons.svg#icon-shopping-cart"></use>
</svg>
<span>Add to shopping list</span>
</button>
</div>
<div class="recipe__directions">
<h2 class="heading-2">How to cook it</h2>
<p class="recipe__directions-text">
This recipe was carefully designed and tested by
<span class="recipe__by">${recipe.author}</span>. Please check out directions at their website.
</p>
<a class="btn-small recipe__btn" href="${recipe.url}" target="_blank">
<span>Directions</span>
<svg class="search__icon">
<use href="img/icons.svg#icon-triangle-right"></use>
</svg>
</a>
</div>
`;
elements.recipe.insertAdjacentHTML('afterbegin', markup);
};