-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLikes.js
More file actions
108 lines (84 loc) · 3.05 KB
/
Likes.js
File metadata and controls
108 lines (84 loc) · 3.05 KB
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
export default class Likes {
constructor() {
/** Array Version */
// this.likesArr = [];
/** Map Version */
this.likes = new Map();
}
addLikedItem(id, title, author, img) {
/** Array Version */
// const likedItem = { id, title, author, img };
// this.likesArr.push(likedItem);
// Persist data in window.Storage using localStorage()
// this.persistData();
// return likedItem;
/** Map Version */
this.likes.set(id, { title, author, img });
// Persist data in window.Storage using localStorage()
this.persistData();
return { id, value: this.likes.get(id) };
}
deleteLikedItem(id) {
/** Array Version */
// const index = this.likesArr.findIndex(el => el.id === id);
// this.likes.splice(index, 1);
// // Persist data in window.Storage using localStorage()
// this.persistData();
/** Map Version */
if (this.likes.has(id))
this.likes.delete(id);
// Persist data in window.Storage using localStorage()
this.persistData();
}
// When we load a particular recipe, we have to know whether that recipe has been liked previously
// or not, so we can style the recipe accordingly on to the front-end view.
isLikedItem(id) {
/** Array Version */
// return this.likes.findIndex(el => el.id === id) !== -1;
/** Map Version */
return this.likes.has(id);
}
getNumberOfLikedItems() {
/**Array Version */
// return this.likes.length;
/** Map Version */
return this.likes.size;
}
persistData() {
// we know that both key-value pairs should be a string, therefore, we can convert the likes
// object into a string using JSON.stringify() method and pass this.likes onto the method.
// convert the Map elements into JSON format:
const likesArr = [];
for (let [key, value] of this.likes.entries()) {
// console.log(key, value);
likesArr.push({
id: key,
title: value.title,
author: value.author,
img: value.img
});
}
// this.likesArr = likesArr;
// localStorage.clear();
localStorage.setItem('likes', JSON.stringify(likesArr));
}
readStorage() {
// To retrieve the data stored in the localStorage for likes data persistency.
const storage = JSON.parse(localStorage.getItem('likes'));
// Restoring the likes from the localStorage, convert the likesArr back to a Map() object
if (storage) {
/** Array Version */
// this.likesArr = storage;
/** Map Version */
storage.forEach(el => {
if (!this.likes.has(el.id)) {
this.likes.set(el.id, {
title: el.title,
author: el.author,
img: el.img
});
}
});
}
}
}