-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
161 lines (130 loc) · 4.39 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const cardContainer = document.querySelector('.images-container');
const savedConfirm = document.querySelector('.save-confirmed');
const loader = document.querySelector('.loader');
const resultNav = document.getElementById('resultsNav');
const favNav = document.getElementById('FavoritesNav');
const apiKey = 'DEMO_KEY';
const count = 10;
const apiUrl =`https://api.nasa.gov/planetary/apod?api_key=${apiKey}&count=${count}`;
let resultArr = [];
let favList = {};
function showContent()
{
window.scrollTo({top:0, behavior: 'instant'});
loader.classList.add('hidden')
}
async function getNasaAPOD()
{
loader.classList.remove('hidden');
try{
const response = await fetch(apiUrl);
resultArr = await response.json();
updateDOM('results');
}
catch(err)
{
console.log(err);
}
}
function saveFav(itemUrl)
{
resultArr.forEach((item)=>{
if(item.url === itemUrl && !favList[itemUrl])
{
favList[itemUrl] = item;
savedConfirm.hidden = false;
setTimeout(()=>{
savedConfirm.hidden = true;
}, 2000)
}
})
localStorage.setItem('favorites', JSON.stringify(favList));
}
function removeFav(itemUrl)
{
if(favList[itemUrl])
{
delete favList[itemUrl];
localStorage.setItem('favorites', JSON.stringify(favList));
}
updateDOM('favorites');
}
function createCard(page)
{
const currentList = page === 'results' ? resultArr : Object.values(favList);
currentList.forEach((item)=>{
const card = document.createElement('div')
card.classList.add('card');
// link and Img
const link = document.createElement('a');
link.href = item.url;
link.title = 'View Full Image';
link.target ='_blank';
const Img = document.createElement('img');
Img.src = item.url;
Img.alt = 'NASA Images';
Img.loading = 'lazy';
Img.classList.add('card-img-top');
link.appendChild(Img)
// card body
const cardBody = document.createElement('div');
cardBody.classList.add('card-body');
// title
const title = document.createElement('h5');
title.classList.add('card-title');
title.textContent = item.title;
// Fav button/text
const fav = document.createElement('p');
const favIcon = document.createElement('i');
fav.classList.add('clickable');
if(page === 'results')
{
fav.textContent = 'Add to Favourites ';
fav.setAttribute('onclick',`saveFav('${item.url}')`);
}
else
{
fav.textContent = 'Remove Favourites ';
favIcon.classList.add('fa-solid', 'fa-heart');
fav.setAttribute('onclick',`removeFav('${item.url}')`);
}
fav.appendChild(favIcon);
// card text
const text = document.createElement('p');
text.classList.add('card-text');
text.textContent = item.explanation;
// footer
const footer = document.createElement('small');
footer.classList.add('text-muted');
const date = document.createElement('strong');
date.textContent = `${item.date} | `;
const copyrightIcon = document.createElement('i');
copyrightIcon.classList.add('fa-regular', 'fa-copyright');
const copyright = document.createElement('span');
copyright.textContent = item.copyright ? ` ${item.copyright}` : ` unknown`;
copyright.style.fontFamily = 'Verdana'
copyrightIcon.appendChild(copyright)
footer.append(date,copyrightIcon);
cardBody.append(title,fav,text,footer);
card.append(link,cardBody)
cardContainer.appendChild(card);
})
}
function updateDOM(page)
{
if(page === 'favorites' && localStorage.getItem('favorites'))
{
favList = JSON.parse(localStorage.getItem('favorites'))
resultNav.classList.add('hidden');
favNav.classList.remove('hidden');
}
if(page === 'results')
{
resultNav.classList.remove('hidden');
favNav.classList.add('hidden');
}
cardContainer.textContent = '';
createCard(page);
showContent();
}
getNasaAPOD();