Skip to content

Commit c651fe6

Browse files
committed
170 Forkify: Recipe View (Part 2)
- Added 7.7.11.2 named as "Using 3rd Part API fractional to convert the Quantities into a Fraction & Highlighting the Selected Recipes using classList property"
1 parent fc409c6 commit c651fe6

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vscode
22
JS-DOM/challenge
33
Modern-JS-ES6-NPM-Babel-Webpack/forkify_project/node_modules
4-
.gitignore
4+
.gitignore
5+
Modern-JS-ES6-NPM-Babel-Webpack/forkify_project/package-lock.json

Modern-JS-ES6-NPM-Babel-Webpack/forkify_project/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"dependencies": {
2323
"@babel/polyfill": "^7.8.3",
24-
"axios": "^0.19.2"
24+
"axios": "^0.19.2",
25+
"fractional": "^1.0.0"
2526
}
2627
}

Modern-JS-ES6-NPM-Babel-Webpack/forkify_project/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<link href="https://fonts.googleapis.com/css?family=Nunito+Sans:400,600,700" rel="stylesheet">
99
<link rel="stylesheet" href="css/style.css">
1010
<link rel="shortcut icon" href="img/favicon.png" type="image/x-icon">
11-
<title>forkify // Search over 1,000,000 recipes</title>
11+
<title>forkify - Search a 100,000 recipes</title>
1212
</head>
1313

1414
<body>

Modern-JS-ES6-NPM-Babel-Webpack/forkify_project/src/js/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
/********************************************************************************************************************
22
* What we'll learn:
33
* ----------------
4-
* 1. Using loop to generate HTML markup.
4+
* 1. Using external third-party API called fractional.
5+
*
6+
* We want to convert the recipe quantity (i.e., count) from decimal values (i.e., 4.5, 2.25, etc) into fractional
7+
* values (i.e., 4 1/2, 2 1/4, etc). In order to do that, we use a 3rd party API called fractional.
8+
* We install it using npm and save it as a code dependency because we will import the API in our code.
9+
* Now, we implement the formatCount() method inside the recipeView module, where we will use the fractional API.
10+
* Another important aspect is to keep the selected recipe from the .results__list highlighted.
11+
* For that, we will implement another function in searchView module which will be called here, onto which the ID of
12+
* the selected item is passed onto to the method. The method is called highlightSelected(). It is called inside
13+
* the controlRecipe() method in this file.
514
*
615
*/
716

@@ -102,6 +111,10 @@ const controlRecipe = async () => {
102111
recipeView.clearRecipe();
103112
renderLoader(elements.recipe);
104113

114+
// Highlight the selected recipe
115+
if (state.search)
116+
searchView.highlightSelected(id);
117+
105118
// Create new recipe object
106119
state.recipe = new Recipe(id);
107120
// window.r = state.recipe; // TEST CODE

Modern-JS-ES6-NPM-Babel-Webpack/forkify_project/src/js/views/recipeView.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
11
import { elements } from './base';
2+
import { Fraction } from 'fractional';
23

34
export const clearRecipe = () => {
45
elements.recipe.innerHTML = '';
56
};
67

8+
// look into documentation of fraction.js @https://github.com/ekg/fraction.js/
9+
const formatCount = count => {
10+
if (count) {
11+
// count = 2.5 --> 2 1/2
12+
// count = 0.5 --> 1/2
13+
const [int, dec] = count.toString().split('.').map(el => parseInt(el, 10));
14+
15+
// count = 2 --> 2 // there's no dec
16+
if (!dec) return count;
17+
18+
// count = 0.5 --> 1/2
19+
if (int === 0) {
20+
const fr = new Fraction(count);
21+
return `${fr.numerator}/${fr.denominator}`;
22+
} else {
23+
// count = 2.5 --> 2 1/2
24+
// if we create a fraction for 2.5, we will get 5/2, which we don't want. Therefore,
25+
// we will create a fraction only for 0.5 and append it to 2. For that, we would
26+
// take the difference between the count and the int and pass it to the Fraction's constructor.
27+
const fr = new Fraction(count - int);
28+
return `${int} ${fr.numerator}/${fr.denominator}`;
29+
}
30+
}
31+
return 1;
32+
};
33+
734
const createIngredient = ingredient => `
835
<li class="recipe__item">
936
<svg class="recipe__icon">
1037
<use href="img/icons.svg#icon-check"></use>
1138
</svg>
12-
<div class="recipe__count">${ingredient.count}</div>
39+
<div class="recipe__count">${formatCount(ingredient.count)}</div>
1340
<div class="recipe__ingredient">
1441
<span class="recipe__unit">${ingredient.unit}</span>
1542
${ingredient.ingredient}

Modern-JS-ES6-NPM-Babel-Webpack/forkify_project/src/js/views/searchView.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ export const clearResults = () => {
2424
};
2525

2626

27+
export const highlightSelected = id => {
28+
// Before highlighting the selected recipe from the .results__list class, we have to remove the
29+
// .results__link--active class from all the recipes in the .results__list class' elements.
30+
const recipeArr = Array.from(document.querySelectorAll(`.results__link--active`));
31+
recipeArr.forEach(el => {
32+
el.classList.remove('results__link--active');
33+
});
34+
35+
// highlight the selected recipe from .results__list class
36+
document.querySelector(`a[href="#${id}"]`).classList.add('results__link--active');
37+
};
38+
39+
2740
// function to limit the recipe name in the .results__list class
2841
const limitRecipeTitle = (title, limit = 17) => { // 17 is the sweet spot for limiting the no. of letters
2942
let newTitle = [];

0 commit comments

Comments
 (0)