forked from bstashchuk/random-quotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (95 loc) · 3.81 KB
/
index.js
File metadata and controls
108 lines (95 loc) · 3.81 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
import {
hideFavoriteBtn,
showFavoriteCard,
toggleFavoriteCard,
showFavoriteBtn,
removeFavoriteCard,
} from './src/handlers/favorites.js';
import { displayCurrentQuote } from './src/handlers/currentQuote.js';
import {
localStorageSetItem,
localStorageGetItem,
} from './src/utils/localStorage.js';
import { getRandomQuote } from './src/handlers/randomQuote.js';
import { removeObjectFromArrayById } from './src/utils/array.js';
const CURRENT_QUOTE_KEY = 'currentQuote';
const FAVORITE_QUOTES_KEY = 'favoriteQuotes';
const randomQuoteBtn = document.getElementById('random-quote-btn');
const quoteFavoriteBtn = document.getElementById('quote-favorite-btn');
const favoritesContainer = document.getElementById('favorites-container');
let currentQuote = null;
const favoriteQuotes = [];
function removeFavoriteQuote(id) {
// REMOVE FAVORITE QUOTE
if (id === currentQuote.id) {
// Removing from favorites current quote by clicking on the card Remove from favorites button
toggleCurrentQuote();
} else {
// Removing from favorites quote which is not current quote
// sync app state by remove favorite quote from the favorite quotes array
removeObjectFromArrayById(favoriteQuotes, id);
// Remove favorite card from UI
removeFavoriteCard(id);
// Save favorite quotes in the local storage
localStorageSetItem(FAVORITE_QUOTES_KEY, favoriteQuotes);
}
// // The way to find current quote in the HTML in the other module
// const currentQuote = document.querySelector('[data-current-quote-id]');
// const currentQuoteId = currentQuote.dataset.currentQuoteId;
}
function toggleCurrentQuote() {
// CURRENT QUOTE UPDATE
// sync app state and toggle isFavorite of the current quote
currentQuote.isFavorite = !currentQuote.isFavorite;
// update UI by toggling favorite Icon (no need to display again current quote)
showFavoriteBtn(currentQuote.isFavorite);
// Save current quote in the local storage
localStorageSetItem(CURRENT_QUOTE_KEY, currentQuote);
// FAVORITE QUOTES UPDATE
// sync app state and update favoriteQuotes array
if (currentQuote.isFavorite) {
favoriteQuotes.push({ ...currentQuote });
} else {
removeObjectFromArrayById(favoriteQuotes, currentQuote.id);
}
// update UI by adding or removing favorite card
toggleFavoriteCard(currentQuote, favoritesContainer);
// Save favorite quotes in the local storage
localStorageSetItem(FAVORITE_QUOTES_KEY, favoriteQuotes);
}
function setCurrentQuote(quote) {
// SET CURRENT QUOTE WHEN LOADED FROM LOCAL STORAGE OR RECEIVED RANDOMLY
// Change app state and write copy of the quote to the current quote
currentQuote = { ...quote };
// Check if id of the current quote is among favorite quotes and set isFavorite
currentQuote.isFavorite = !!favoriteQuotes.find(
(favoriteQuote) => favoriteQuote.id === currentQuote.id
);
// Show current quote in the UI
displayCurrentQuote(currentQuote);
// Display favorite Icon and change its state
showFavoriteBtn(currentQuote.isFavorite);
// Save current quote in the local storage
localStorageSetItem(CURRENT_QUOTE_KEY, currentQuote);
}
hideFavoriteBtn();
quoteFavoriteBtn.addEventListener('click', toggleCurrentQuote);
// expecting new random quote {id, text, author, ...}
randomQuoteBtn.addEventListener('click', () =>
setCurrentQuote(getRandomQuote())
);
function init() {
const favoriteQuotesFromStorage = localStorageGetItem(FAVORITE_QUOTES_KEY);
if (favoriteQuotesFromStorage) {
favoriteQuotesFromStorage.forEach((quote) => {
favoriteQuotes.push(quote);
showFavoriteCard(quote, favoritesContainer);
});
}
const currentQuoteFromStorage = localStorageGetItem(CURRENT_QUOTE_KEY);
if (currentQuoteFromStorage) {
setCurrentQuote(currentQuoteFromStorage);
}
}
window.addEventListener('load', init);
export { quoteFavoriteBtn, removeFavoriteQuote };