-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
364 lines (289 loc) · 11.6 KB
/
Copy pathindex.js
File metadata and controls
364 lines (289 loc) · 11.6 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// import axios from 'axios';
// Get the menu icon and navigation menu elements
const menuIcon = document.querySelector('.menu-icon');
const navMenu = document.querySelector('nav ul');
// Add event listener to the menu icon
menuIcon.addEventListener('click', () => {
menuIcon.classList.toggle('hidden');
navMenu.classList.toggle('show');
});
// SOS Message functionality
const sosForm = document.getElementById('sos-form');
const sosMessageInput = document.getElementById('sos-message');
sosForm.addEventListener('submit', async (e) => {
e.preventDefault();
const sosMessage = sosMessageInput.value.trim();
if (sosMessage) {
try {
const response = await axios.post('/api/sos', { message: sosMessage });
console.log(response.data);
sosMessageInput.value = '';
} catch (error) {
alert(error.message);
}
}
});
// // Sign In functionality
// const signInForm = document.getElementById('sign-in-form');
// const signInEmailInput = document.getElementById('sign-in-email');
// const signInPasswordInput = document.getElementById('sign-in-password');
// const signinresponds = document.getElementById('sign-in-responds');
// signInForm.addEventListener('submit', async (e) => {
// e.preventDefault();
// const email = signInEmailInput.value.trim();
// const password = signInPasswordInput.value.trim();
// if (email && password) {
// console.log(email,password);
// try {
// const response = await axios.post('/signin', { email, password });
// // Process the data
// if (response.ok) {
// signinresponds.textContent = "Signed in successsfully";
// } else {
// signinresponds.textContent = "Sign in unsuccessful"
// }
// } catch (error) {
// console.error(error);
// }
// } else {
// displayError('Please fill in all fields');
// return false;
// }
// });
// // Sign Up functionality
// const signUpForm = document.getElementById('sign-up-form');
// const signUpNameInput = document.getElementById('sign-up-name');
// const signUpEmailInput = document.getElementById('sign-up-email');
// const signUpPasswordInput = document.getElementById('sign-up-password');
// const signupresponds = document.getElementById('sign-up-responds');
// signUpForm.addEventListener('submit', async (e) => {
// e.preventDefault();
// const signUpname = signUpNameInput.value.trim();
// const signUpemail = signUpEmailInput.value.trim();
// const signUppassword = signUpPasswordInput.value.trim();
// if (signUpname && signUpemail && signUppassword) {
// try {
// const response = await axios.post('/signup', { signUpname, signUpemail, signUppassword });
// alert.log(response.data);
// // Process the data
// if (response.ok) {
// signupresponds.textContent = "Signed up successsfully";
// } else {
// signupresponds.textContent = "Sign up unsuccessful"
// }
// } catch (error) {
// console.error(error);
// if (error) {
// signupresponds.textContent = `An error occurred:${error}`;
// }
// }
// }
// });
// toggle-password-icon
document.getElementById('toggle-password-icon').addEventListener('click', function() {
const togglePasswordIcon = document.querySelector('i #toggle-password-icon .fa');
// Toggle password visibility
if (signUpPasswordInput.type === 'pasword') {
togglePasswordIcon.classList.add('fa-unlock');
togglePasswordIcon.classList.remove('fa-lock');
} else {
signUpEmailInput.type = 'password'; // Hide Password
togglePasswordIcon.classList.remove('fa-unlock');
togglePasswordIcon.classList.add('fa-lock');
}if (error) {
console.error(error);
signupresponds.textContent = `An error occurred:${error}`;
}
});
// Logout Functionality
document.getElementById('logout-btn').addEventListener('click', () => {
document.getElementById('confirmation-dialog').style.display = 'block';
});
document.getElementById('no-btn').addEventListener('click', () => {
document.getElementById('confirmation-dialog').style.display = 'none';
});
// // Confirmation Dialog
// document.getElementById('yes-btn').addEventListener('click', async () => {
// try {
// const response = await fetch('/api/logout', {
// method:'POST'});
// if (response.ok) {
// document.getElementById('sign')
// }
// }
// })
// Toggle Switch
async function DarkMode() {
const themeToggle = document.getElementById('theme-toggle');
themeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-mode');
document.querySelectorAll('header, footer').forEach((element) => {
element.classList.toggle('dark-mode');
});
document.querySelectorAll('.container.b').forEach((container) => {
container.classList.toggle('dark-container');
});
document.querySelectorAll('button').forEach((button) => {
button.classList.toggle('dark-mode');
});
document.querySelectorAll('h2, p').forEach((element) => {
element.classList.toggle('dark-text-head');
});
document.querySelectorAll('nav').forEach((element) => {
element.classList.toggle('dark-mode');
});
document.querySelectorAll('svg').forEach((svg) => {
svg.classList.toggle('dark-mode');
});
localStorage.setItem('theme',
document.body.classList.contains('dark-mode') ? 'dark': 'light'
);
document.body.classList.toggle('light-mode');
});
}
// Set initial theme based on local storage
const initialTheme = localStorage.getItem('theme');
if (initialTheme === 'dark') {
document.body.classList.add('dark-mode');
}
// Get the menu icon and the mobile nav element
const menuicon = document.getElementById('menu-icon');
const mobileNav = document.querySelector('nav');
// Toggle the mobile nav when the menu icon is clicked
menuicon.addEventListener('click', () => {
mobileNav.classList.toggle('show');
mobileNav.classList.toggle('mobile-nav');
mobileNav.classList.toggle('nav');
menuicon.classList.toggle('menu-icon');
});
// Live Coverage functionality
const liveCoverageList = document.getElementById('live-coverage-list');
async function fetchLiveCoverage() {
try {
const response = await fetch('/api/live-coverage');
const liveCoverage = response.data;
liveCoverageList.innerHTML = '';
liveCoverage.forEach((item) => {
const listItem = document.createElement('li');
listItem.textContent = item;
liveCoverageList.appendChild(listItem);
});
} catch (error) {
console.error(error);
}
}
// AI Identifier functionality
async function fetchAiIdentifier() {
try {
const image = document.getElementById('image').files[0];
const formData = new FormData();
formData.append('image', image);
const response = await fetch('/api/ai-identifier', {
method: 'POST',
body: formData
});
const data = await response.json();
const aiIdentifierResult = document.getElementById('ai-identifier-result');
aiIdentifierResult.textContent = data.result.join(', ');
} catch (error) {
console.error(error);
}
};
// Space Guide functionality
async function fetchSpaceGuide() {
try {
const response = await fetch('/api/space-guide');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
// Process the data
document.getElementById('space-guide-info').innerHTML = `
<h2>${data.title}</h2>
<p>${data.explanation}</p>
<img src="${data.url}" alt="${data.title}" />
<p><strong>Copyright:</strong> ${data.copyright}</p>
<a href="${data.hdurl}" target="_blank">View High-Definition Image</a>
`;
} catch (error) {
console.error('Error fetching space guide:', error);
document.getElementById('space-guide-info').innerHTML = 'Failed to load space guide.';
}
}
// Call fetch function when the page loads
window.onload = fetchAstronomyPictureOfTheDay;
async function fetchAstronomyPictureOfTheDay() {
try {
const response = await fetch('/api/apod');
const apodData = await response.json();
console.log(apodData);
if (apodData && apodData.url && apodData.title && apodData.explanation) {
document.getElementById('space-guide-info').innerHTML = `
<h2>${apodData.title}</h2>
<img src="${apodData.url}" alt="${apodData.title}" />
<p>${apodData.explanation}</p>
`;
} else {
document.getElementById('space-guide-info').innerText = 'Failed to load Astronomy Picture of the Day';
}
} catch (error) {
console.error(error);
document.getElementById('space-guide-info').innerText = 'Failed to load Astronomy Picture of the Day';
}
}
async function fetchSpaceNews() {
try {
const response = await fetch('/api/space-news');
const data = await response.json(); // Ensure this is correct
const articles = data.articles; // Assuming the articles are in the "articles" key
const newsList = articles.map(article => `
<li>
<h3>${article.title}</h3>
<p>${article.summary}</p>
<a href="${article.url}" target="_blank">Read more</a>
</li>
`).join('');
document.getElementById('space-news-list').innerHTML = newsList;
} catch (error) {
console.error(error);
document.getElementById('space-news-list').innerText = 'Failed to load space news';
}
}
async function fetchMarsRoverPhotos() {
try {
const response = await fetch('/api/mars-rover-photos'); // Correct endpoint for Mars Rover photos
const photos = await response.json(); // Get photo data
const photoList = photos.map(photo => {
const img = document.createElement('img');
img.src = photo.img_src; // Make sure 'img_src' is the correct field
img.alt = 'Mars Rover Photo';
img.style.width = '200px';
img.style.margin = '5px';
return img.outerHTML; // Correctly add the image elements
}).join('');
document.getElementById('photos-mars-result').innerHTML = photoList;
} catch (error) {
console.error(error);
document.getElementById('photos-mars-result').innerText = 'Failed to load Mars rover photos';
}
}
// Function to initialize the app by fetching all necessary data
async function initializeApp() {
await fetchAstronomyPictureOfTheDay(); // Fetch APOD
await fetchSpaceNews(); // Fetch space news
await fetchMarsRoverPhotos(); // Fetch Mars rover photos
await fetchSpaceGuide(); //
await fetchAiIdentifier();
await fetchLiveCoverage();
}
// Immediately invoke the initialize function on DOM content loaded
document.addEventListener('DOMContentLoaded', (event) => {
initializeApp(); // Call the function to initialize the app
});
const MainIndex = document.getElementById('mainIndex');
const MainProfile = document.getElementById('mainProfile');
const profileMain = document.querySelector('a#profilemain');
profileMain.addEventListener('click', () => {
MainIndex.style.display = 'none';
MainProfile.style.display = 'block';
});