Skip to content

Commit 18b102c

Browse files
committed
180 Forkify: Shopping List (Data) Model
- Added 7.7.12 named as "Building the Shopping List (Data) Model Using uniqid"
1 parent ad37586 commit 18b102c

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

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

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"@babel/polyfill": "^7.8.3",
2424
"axios": "^0.19.2",
25-
"fractional": "^1.0.0"
25+
"fractional": "^1.0.0",
26+
"uniqid": "^5.2.0"
2627
}
2728
}

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
/********************************************************************************************************************
22
* What we'll learn:
33
* ----------------
4-
* 1. Another way of implementing event delegation using matches() method.
4+
* 1. How and why to create unique IDs using an external package called 'uniqid'.
5+
* 2. Difference between Array.slice() and Array.splice() methods.
6+
* 3. More use cases for Array.findIndex() and Array.find() methods.
7+
*
8+
* We make a new model which is the List Model which handles the data for the Shopping List. Check out the List
9+
* Module at ./src/js/models/List.js. One thing to note is that we generate unique ID's for each item inside the List
10+
* Module using a 3rd party API known as 'uniqid' which we install using npm by the command: 'npm i uniqid --save' in
11+
* the project directory. And then we can import it inside the List Model Module. Note that we can import the package
12+
* because it is not a dev dependency but it is a code dependency. Therefore, we can go ahead and import the code
13+
* related to 'uniqid' package.
514
*
6-
* We will implement the functionality to change the ingredient quantities depending on the number of servings.
7-
* Therefore, we give the functionality of increasing/decreasing the number of servings for every recipe.
8-
* This functionality is given in the Recipe Model Module where we define a method called updateServings().
9-
* Again, we will be handling the button clicks in this file as index.js is the controller. We have to apply
10-
* event delegation for the .recipe class' element and catch the event at the element that has the .recipe class
11-
* applied to it. Right at the bottom of this file, we can see that the .recipe class' button click event is handled.
12-
* Consequently, we will update the UI using the updateServingsAndIngredients() method written in recipeView module.
1315
*/
14-
16+
// Import Data Models
1517
import Search from './models/Search';
1618
import Recipe from './models/Recipe';
19+
import List from './models/List';
20+
21+
// Import Front-End Views
1722
import * as searchView from './views/searchView';
1823
import * as recipeView from './views/recipeView';
1924

25+
// Import Common Code Base
2026
import { elements, renderLoader, clearLoader, elementStrings } from './views/base';
2127

2228
/**
@@ -168,4 +174,10 @@ elements.recipe.addEventListener('click', event => {
168174

169175
recipeView.updateServingsAndIngredients(state.recipe);
170176
//console.log(state.recipe); // TESTING
171-
});
177+
});
178+
179+
180+
// SHOPPING LIST CONTROLLER
181+
182+
// TEST CODE
183+
window.l = new List();
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import uniqid from 'uniqid';
2+
3+
export default class List {
4+
constructor() {
5+
/** Array Version */
6+
// this.items = [];
7+
// Instead of using an array, we can simply use a Map
8+
9+
/** Map Version */
10+
this.items = new Map();
11+
}
12+
13+
addItem(count, unit, ingredient) {
14+
/** Array Version */
15+
// const item = {
16+
// id: uniqid(),
17+
// count, unit, ingredient
18+
// };
19+
// this.items.push(item);
20+
// return item;
21+
22+
/** Map Version */
23+
const id = uniqid();
24+
this.items.set(id, { count, unit, ingredient });
25+
return this.items.get(id);
26+
}
27+
28+
deleteItem(id) {
29+
// we use the splice() method to delete the item from the this.items[].
30+
// Syntax of splice(): Array.splice(starting_index, no_of_elements_to_delete) where the deletion in the
31+
// referenced Array begins from starting_index and the number of elements deleted from there onwards are
32+
// mentioned in no_of_elements_to_delete.
33+
// Ex: const list = [100, 200, 300, 400, 500, 600];
34+
// const list2 = list.splice(2, 3); // list2 is now [300, 400, 500] and list is [100, 200, 600]
35+
// We can see that splice() cuts the referenced array and then mutates it, the mutated part is returned by
36+
// the method as an array.
37+
38+
// Array.slice() works differently where the syntax is: Array.slice(start_index, last_index) where we return
39+
// the array elements from start_index to last_index-1 and we don't mutate the referenced Array.
40+
// Ex: const list = [100, 200, 300, 400, 500, 600];
41+
// const list2 = list.slice(2, 3); // list2 is now [300] & list is still [100, 200, 300, 400, 500, 600]
42+
43+
/** Array Version */
44+
// const index = this.items.findIndex(el => el.id === id);
45+
// this.items.splice(index, 1);
46+
47+
/** Map Version */
48+
if (this.items.has(id))
49+
this.items.delete(id);
50+
}
51+
52+
// Update the quantity of the ingredient in the Shopping List
53+
updateCount(id, newCount) {
54+
/** Array Version */
55+
// this.items.find(el => el.id === id).count = newCount;
56+
57+
/** Map Version */
58+
this.items.get(id).count = newCount;
59+
}
60+
61+
62+
63+
}

0 commit comments

Comments
 (0)