-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
131 lines (116 loc) · 4.79 KB
/
script.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
const quoteContainer = document.getElementById("quote-container");
const quoteText = document.getElementById("quote");
const authorText = document.getElementById("author");
const twitterBtn = document.getElementById("twitter");
const newQuoteBtn = document.getElementById("new-quote");
const loader = document.getElementById("loader");
const favouriteIcon = document.getElementById("favourite");
const favouritesContainer = document.getElementById("favourites-container");
const showfavourite = document.getElementById("show-favourites");
const showhome = document.getElementById("homeBtn");
let apiQuotes = []; //an array of all the quotes(objects)
let quote = {}; //an object which stores the quote at random
let favouriteQuotes = []; //an array which stores the favourite quote
// Show Loading
function showLoadingSymbol() {
loader.hidden = false;
quoteContainer.hidden = true;
}
// Remove Loading Symbol
function removeLoadingSymbol() {
quoteContainer.hidden = false;
loader.hidden = true;
}
const addQuoteToFavourites = function () {
saveToLocalFavourites(quote);
};
const saveToLocalFavourites = function (favourite) {
let favouriteQuotesJsonString = localStorage.getItem("favouriteQuotes");
if (favouriteQuotesJsonString != null)
favouriteQuotes = JSON.parse(favouriteQuotesJsonString);
const validateQuote = favouriteQuotes.find((q) => q.text === favourite.text);
if (!validateQuote) {
favouriteQuotes.push(favourite);
favouriteIcon.style.color = "red";
}
// If it's already a favourite then remove the quote from the favourite list and change the icon color to grey
else {
removeQuoteFromFavourites(favourite);
favouriteIcon.style.color = "grey";
}
favouriteQuotesJsonString = JSON.stringify(favouriteQuotes);
localStorage.setItem("favouriteQuotes", favouriteQuotesJsonString);
};
const removeQuoteFromFavourites = function (favourite) {
const indexToBeDeleted = favouriteQuotes.findIndex(
(q) => q.text === favourite.text
);
favouriteQuotes.splice(indexToBeDeleted, 1);
let favouriteQuotesJsonString = JSON.stringify(favouriteQuotes);
localStorage.setItem("favouriteQuotes", favouriteQuotesJsonString);
};
const showFavouriteQuotes = function () {
favouriteQuotes = JSON.parse(localStorage.getItem("favouriteQuotes"));
for (let index = 0; index < favouriteQuotes.length; index++) {
const quotePar = document.createElement("p");
quotePar.className = "fav-q-par";
quotePar.id = "fav" + index;
favouritesContainer.appendChild(quotePar);
if (!favouriteQuotes[index].author)
favouriteQuotes[index].author = "Author Unknown";
let quoteParId = document.getElementById(`${quotePar.id}`);
quoteParId.innerHTML = `<i class="fas fa-heart heart" title="remove from favourites" id="${index}" style="color:red"></i> ${favouriteQuotes[index].text} (${favouriteQuotes[index].author})`;
}
let heart = document.querySelectorAll(".heart");
favouritesContainer.addEventListener("click", function (e) {
const clicked = e.target;
const quoteToRemove = {};
//take only the text part from the quote (without the author) and save it as a property in the quoteToRemove obj
let removeQuote = document.getElementById(`fav${clicked.id}`);
console.log(removeQuote);
removeQuote.style.display = "none";
removeQuoteFromFavourites(quoteToRemove); // remove the quote from the local storage
});
};
showfavourite.addEventListener("click", function () {
quoteContainer.hidden = true;
favouritesContainer.hidden = false;
favouritesContainer.innerHTML =
"<h2>your favourite quotes</h2><h4>to remove a quote - click on the heart<h4>";
showFavouriteQuotes();
});
showhome.addEventListener("click", function () {
quoteContainer.hidden = false;
favouritesContainer.hidden = true;
getQuotes();
});
newQuote = function () {
showLoadingSymbol();
favouriteIcon.style.color = "grey";
quote = apiQuotes[Math.floor(Math.random() * apiQuotes.length)];
if (!quote.author) authorText.textContent = "Unknown";
else authorText.textContent = quote.author;
quoteText.textContent = quote.text;
removeLoadingSymbol();
};
//get quotes from the api
getQuotes = async function () {
showLoadingSymbol();
const apiUrl = "https://type.fit/api/quotes";
try {
const response = await fetch(apiUrl);
apiQuotes = await response.json();
console.log(apiQuotes);
newQuote();
} catch (err) {
console.error(err.message);
}
removeLoadingSymbol();
};
getQuotes();
tweetQuote = function () {
const twitterUrl = `https://twitter.com/intent/tweet?text=${quote.text} -${quote.author}`;
window.open(twitterUrl);
};
twitterBtn.addEventListener("click", tweetQuote);
newQuoteBtn.addEventListener("click", getQuotes);