-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
155 lines (144 loc) · 4.87 KB
/
script.js
File metadata and controls
155 lines (144 loc) · 4.87 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
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
const resultsNav = document.getElementById('resultsNav')
const favoritesNav = document.getElementById('favoritesNav')
const imageContainer = document.querySelector('.images-container')
const saveConfirmed = document.querySelector('.save-confirmed')
const loader = document.querySelector('.loader')
// NASA API
const count = 10
const apiKey = 'Lbdo6sbd5VVaPicmfN7N1wUNb3i4mjPz55pVDOEM'
const apiUrl = `https://api.nasa.gov/planetary/apod?api_key=${apiKey}&count=${count}`
let resultsArray = []
let favorites = {}
function createDomEl(info, saveDeleteText) {
// Card Container
const card = document.createElement('div')
card.classList.add('card')
// link
const link = document.createElement('a')
link.href = info.hdurl;
link.title = 'View Full Image'
link.target = '_blank'
// Image
const image = document.createElement('img')
image.src = info.url;
image.alt = 'NSA Picture of the Day'
image.loading = 'lazy'
image.classList.add('card-image-top')
// Card Body
const cardBody = document.createElement('div')
cardBody.classList.add('card-body')
// Card Title
const cardTitle = document.createElement('h5')
cardTitle.classList.add('card-title')
cardTitle.textContent = info.title
// Save Text
const saveText = document.createElement('p')
saveText.classList.add('clickable')
saveText.textContent = saveDeleteText
saveText.setAttribute('onclick', saveDeleteText.includes('Add') ? `saveFavorite('${info.url}')` : `removeFavorite('${info.url}')`)
// Card Text
const cardText = document.createElement('p')
cardText.classList.add('card-text')
cardText.textContent = info.explanation
// Footer Container
const footer = document.createElement('small')
footer.classList.add('text-muted')
// Date
const date = document.createElement('strong')
date.textContent = info.date
// Copyright
const copyrightInfo = info.copyright === undefined ? '' : info.copyright
const copyright = document.createElement('span')
copyright.textContent = ` ${copyrightInfo}`
// Append
footer.append(date, copyright)
cardBody.append(cardTitle, saveText, cardText, footer)
link.appendChild(image)
card.append(link, cardBody)
imageContainer.appendChild(card)
}
function showLoader(display) {
window.scrollTo({top: 0, behavior: 'instant'})
if (display === true) {
loader.classList.remove('hidden')
} else {
loader.classList.add('hidden')
}
}
function changeNavUi(display) {
if (display === 'Nasa') {
resultsNav.classList.remove('hidden')
favoritesNav.classList.add('hidden')
} else {
resultsNav.classList.add('hidden')
favoritesNav.classList.remove('hidden')
}
}
// Get 10 Images from NASA API
async function getNasaPictured() {
// Show Loader
showLoader(true)
// Retrieve favorites from local storage
if (localStorage.getItem('nasaFavorites')) {
favorites = JSON.parse(localStorage.getItem('nasaFavorites'))
}
try {
// Changed Nav menu
changeNavUi('Nasa')
// Clear Current images & load Nasa images
imageContainer.textContent = ''
const response = await fetch (apiUrl)
resultsArray = await response.json()
resultsArray.forEach((result) => {
createDomEl(result, 'Add to Favorites')
})
showLoader(false)
} catch (error) {
// Catch Error Here
console.log(error);
}
}
// Loads Favorited Nasa Images
function loadFavorites() {
// Changed Nav menu
changeNavUi('Favorites')
// Clear Current images & load Favorited images
imageContainer.textContent = ''
showLoader(true)
const favoritesArray = Object.values(favorites)
favoritesArray.forEach((result) => {
createDomEl(result, 'Remove From Favorites')
})
showLoader(false)
}
// Add result to favorites
function saveFavorite(itemUrl) {
// Loop through Results Array to select Favorite
resultsArray.forEach((item) => {
if (item.url.includes(itemUrl) && !favorites[itemUrl]) {
favorites[itemUrl] = item
// Show Save Confirmation for 2 Seconds
saveConfirmed.hidden = false
saveConfirmed.classList.add('save-complete')
setTimeout(() => {
saveConfirmed.hidden = true
}, 2000)
setTimeout(() => {
saveConfirmed.classList.remove('save-complete')
}, 2100)
// Set Favorites in localStorage
localStorage.setItem('nasaFavorites', JSON.stringify(favorites))
}
})
}
// Remove item from favorites
function removeFavorite(itemUrl) {
if (favorites[itemUrl]) {
delete favorites[itemUrl]
// Set Favorites in localStorage
localStorage.setItem('nasaFavorites', JSON.stringify(favorites))
loadFavorites()
}
}
// On Load
getNasaPictured()