-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (128 loc) · 4.64 KB
/
app.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const resultsNav = document.getElementById("results-nav");
const favoritesNav = document.getElementById("favorites-nav");
const imagesContainer = document.querySelector(".images-container");
const saveConfirmed = document.querySelector(".save-confirmed");
const loader = document.querySelector(".loader");
//NASA API
const count = 11;
const apiKey = 'DEMO_KEY';
const apiURL = `https://api.nasa.gov/planetary/apod?api_key=${apiKey}&count=${count}`;
let resultsArray = [];
let favorites = {};
//Show content
function showContent(page) {
window.scrollTo({ top: 0, behavior: 'instant'});
if(page === 'results') {
resultsNav.classList.remove("hidden");
favoritesNav.classList.add("hidden");
} else {
resultsNav.classList.add("hidden");
favoritesNav.classList.remove("hidden");
}
loader.classList.add("hidden");
}
//Create DOM Nodes
function createDOMNodes(page) {
const pageRes = page === 'results' ? resultsArray : Object.values(favorites);
console.log("current Array;", page, pageRes);
pageRes.forEach((result) => {
//Card Container
const card = document.createElement("div");
card.classList.add("card");
//link to wrap image
const link = document.createElement("a");
link.href = result.hdurl;
link.title = "View Full Image";
link.target = "_blank";
card.appendChild(link);
//Image
const image = document.createElement("img");
image.src = result.url;
image.alt = "NASA Picture of the Day";
image.loading = "lazy";
image.classList.add("card-img-top");
link.appendChild(image);
//Card Body
const cardBody = document.createElement("div");
cardBody.classList.add("card-body");
card.appendChild(cardBody);
//Card Title
const cardTitle = document.createElement("h2");
cardTitle.textContent = result.title;
cardTitle.classList.add("card-title");
cardBody.appendChild(cardTitle);
//Add to favourites link
const addToFav = document.createElement("p");
addToFav.classList.add("clickable");
if(page === "results" ) {
addToFav.textContent = "Add To Favourites";
addToFav.setAttribute("onclick", `saveFavorite('${result.url}')`);
} else {
addToFav.textContent = "Remove From Favourites";
addToFav.setAttribute("onclick", `deleteFavorite('${result.url}')`);
}
cardBody.appendChild(addToFav);
//Add Description of the image
const description = document.createElement("p");
description.textContent = result.explanation;
cardBody.appendChild(description);
//Add Date and copyright
const infoCont = document.createElement("small");
infoCont.classList.add("text-muted");
cardBody.appendChild(infoCont);
const date = document.createElement("strong");
date.textContent = result.date;
infoCont.appendChild(date);
const copyright = document.createElement("span");
if(result.copyright) {
copyright.textContent =` Copyright by ${result.copyright}`;
infoCont.appendChild(copyright);
}
imagesContainer.appendChild(card);
});
}
//Populate data to the website
function updateDOM(page) {
//Get favourites from local storage
if(localStorage.getItem("favoriteUrl")) {
favorites = JSON.parse(localStorage.getItem("favoriteUrl"));
}
imagesContainer.textContent = '';
createDOMNodes(page);
showContent(page);
}
//Get 10 images from NASA
async function fetchNasa() {
//Show the loader
loader.classList.remove("hidden");
try {
const response = await fetch(apiURL);
resultsArray = await response.json();
updateDOM("results");
} catch(err){
console.log("Something went wrong:", err);
}
}
//Call the function
fetchNasa();
//Saving to favorites
function saveFavorite(itemUrl) {
resultsArray.forEach((item) => {
if(item.url.includes(itemUrl) && !favorites[itemUrl]) {
favorites[itemUrl] = item;
//Show Confirmation
saveConfirmed.classList.remove("hidden");
setTimeout(() => saveConfirmed.classList.add("hidden"), 3000);
//Save item to local Storage
localStorage.setItem('favoriteUrl', JSON.stringify(favorites));
}
})
}
//Removing favorites from the Local Storage
function deleteFavorite(itemUrl) {
if(favorites[itemUrl]) {
delete favorites[itemUrl];
localStorage.setItem('favoriteUrl', JSON.stringify(favorites));
updateDOM("favorites");
}
}