· 5 years ago · Dec 23, 2020, 02:52 AM
1// Grab DOM Elements
2const backgroundIMG = document.querySelector(".masthead");
3const form = document.getElementById("form");
4const input = document.getElementById("input");
5const affirmationsUL = document.getElementById("affirmations");
6
7// Check if there is anything in local storage
8const affirmations = JSON.parse(localStorage.getItem("affirmations"));
9
10if (affirmations) {
11 affirmations.forEach((affirmation) => addAffirmation(affirmation));
12}
13
14//Unsplash access key
15const clientID = "&client_id=lf2gVZLI3hnn28jfnInXEv6doKCeRvmUaMeJsbplHnI";
16
17////// Implement current Time
18$("#current-time").text(
19 luxon.DateTime.local().toLocaleString({
20 hour: "2-digit",
21 minute: "2-digit",
22 })
23);
24
25//Api call for quotes
26function getQuotes() {
27 axios.get("https://type.fit/api/quotes/").then(function (response) {
28 // let ranQuote = response.data[0].text;
29 let randNum = Math.floor(Math.random() * response.data.length);
30 let randQuote = response.data[randNum].text;
31 $("#quotes").text(randQuote);
32 });
33}
34
35function fetchImage() {}
36
37// Add affirmation and append to LI
38function addAffirmation(affirmation) {
39 let affirmationText = input.value;
40
41 if (affirmation) {
42 affirmationText = affirmation.text;
43 }
44
45 if (affirmationText) {
46 const affirmationEl = document.createElement("li");
47 if (affirmation && affirmation.completed) {
48 affirmationEl.classList.add("completed");
49 }
50
51 affirmationEl.innerText = affirmationText;
52
53 affirmationEl.addEventListener("contextmenu", (e) => {
54 e.preventDefault();
55
56 affirmationEl.remove();
57 updateLS();
58 });
59
60 affirmationsUL.prepend(affirmationEl);
61
62 input.value = "";
63
64 updateLS();
65 }
66}
67
68function updateLS() {
69 affirmationsEl = document.querySelectorAll("li");
70
71 const affirmations = [];
72
73 affirmationsEl.forEach((affirmationEl) => {
74 affirmations.unshift({
75 text: affirmationEl.innerText,
76 });
77 });
78
79 localStorage.setItem("affirmations", JSON.stringify(affirmations));
80}
81
82// Add event listener for user input
83form.addEventListener("submit", (e) => {
84 e.preventDefault();
85
86 addAffirmation();
87});
88
89function init() {
90 getQuotes();
91}
92
93window.onload = function () {
94 init();
95};
96