-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
545 lines (478 loc) · 18.2 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
document.addEventListener('DOMContentLoaded', () => {
const modal = document.getElementById('modal');
const displayNameInput = document.getElementById('displayNameInput');
const saveDisplayNameBtn = document.getElementById('saveDisplayNameBtn');
const closeDisplayNameBtn = document.getElementById('closeDisplayNameBtn');
const generatorTab = document.getElementById('generatorTab');
const tagSelect = document.getElementById('tagSelect');
const addIdeaTab = document.getElementById('addIdeaTab');
const myIdeasTab = document.getElementById('myIdeasTab');
const generatorContent = document.getElementById('generatorContent');
const addIdeaContent = document.getElementById('addIdeaContent');
const myIdeasContent = document.getElementById('myIdeasContent');
const generateBtn = document.getElementById('generateBtn');
const ideaContainer = document.getElementById('ideaContainer');
const tagFilterSelect = document.getElementById('tagFilter');
const nameFilterInput = document.getElementById('nameFilter');
const ideaInput = document.getElementById('ideaInput');
const tagInput = document.getElementById('tagInput');
const submitIdeaBtn = document.getElementById('submitIdeaBtn');
const myIdeaList = document.getElementById('myIdeaList');
const allIdeasList = document.getElementById('allIdeasList');
const loadingScreen = document.getElementById('loadingScreen');
const itemsPerPage = 9;
let currentPage = 1;
let filteredIdeas = [];
// Define a mapping of tags to colors
const tagColors = {
technology: 'bg-gradient-to-br from-blue-800 to-violet-600',
art: 'bg-gradient-to-br from-red-600 to-yellow-600',
food: 'bg-gradient-to-br from-red-700 to-pink-600',
travel: 'bg-gradient-to-br from-green-600 to-blue-600',
sports: 'bg-gradient-to-br from-green-600 to-red-600 to-yellow-600',
music: 'bg-gradient-to-br from-pink-500 to-purple-500',
fashion: 'bg-gradient-to-br from-purple-500 to-cyan-500',
books: 'bg-gradient-to-br from-indigo-500 to-teal-500',
movies: 'bg-gradient-to-br from-gray-800 from-75% via-gray-500',
gaming: 'bg-gradient-to-br from-orange-500 to-yellow-500',
health: 'bg-gradient-to-br from-teal-500 to-green-500',
fitness: 'bg-gradient-to-br from-lime-500 to-yellow-500',
photography: 'bg-gradient-to-br from-cyan-500 to-blue-500',
science: 'bg-gradient-to-br from-amber-500 to-yellow-500',
nature: 'bg-gradient-to-br from-green-700 to-emerald-950',
education: 'bg-gradient-to-br from-red-600 to-orange-600',
business: 'bg-gradient-to-br from-orange-600 to-yellow-600',
finance: 'bg-gradient-to-br from-lime-600 to-yellow-600',
humor: 'bg-gradient-to-br from-pink-600 to-purple-600',
history: 'bg-gradient-to-br from-cyan-600 to-blue-600',
};
let displayName = null;
let isModalVisible = true;
// Show the loading screen
function showLoadingScreen() {
loadingScreen.classList.remove('hidden');
}
// Hide the loading screen
function hideLoadingScreen() {
loadingScreen.classList.add('hidden');
}
// Prompt user for Display Name
function promptDisplayName() {
modal.classList.remove('hidden');
}
// Save Display Name
function saveDisplayName() {
displayName = displayNameInput.value.trim();
if (displayName !== '') {
modal.classList.add('hidden');
isModalVisible = false;
localStorage.setItem('displayName', displayName); // Store display name in local storage
document.getElementById(
'currentUser'
).innerHTML = `<p>User: ${displayName}`; // Update the current user element
showTabContent(generatorContent);
}
}
// Show tab content based on the clicked tab
function showTabContent(tabContent) {
if (!isModalVisible) {
generatorContent.classList.add('hidden');
addIdeaContent.classList.add('hidden');
myIdeasContent.classList.add('hidden');
allIdeasContent.classList.add('hidden');
tabContent.classList.remove('hidden');
// Clear the contents of myIdeasContent and allIdeasList
if (tabContent !== myIdeasContent) {
myIdeasContent.innerHTML = '';
}
if (tabContent !== allIdeasContent) {
allIdeasList.innerHTML = '';
}
}
}
// Fetch random idea from the server with tag filter
async function fetchRandomIdea() {
try {
const tagFilterRandom = tagSelect.value; // Get the selected tag filter value
const response = await axios.get('http://localhost:8000/api/ideas');
const ideas = response.data.data;
// Filter ideas based on tag filter
const filteredIdeas =
tagFilterRandom !== 'all'
? ideas.filter((idea) => idea.tag === tagFilterRandom)
: ideas;
if (filteredIdeas.length > 0) {
const randomIdea =
filteredIdeas[Math.floor(Math.random() * filteredIdeas.length)];
ideaContainer.innerHTML = `
<p class="px-4 py-4 rounded-md frosted-dark">${randomIdea.text}</p>
<div class="flex mb-20 mt-2">
<p class="text-2xl px-4 py-4 rounded-md bg-amber-600">by: ${
randomIdea.username
}</p>
<p class="ml-2 text-2xl px-4 py-4 rounded-md ${
tagColors[randomIdea.tag]
}">${randomIdea.tag}</p>
<p class="ml-2 text-2xl px-4 py-4 rounded-md frosted-dark">${randomIdea.date.slice(
0,
10
)}</p>
</div>
`;
} else {
ideaContainer.innerHTML = `<p>No ideas found with the selected tag.</p>`;
}
hideLoadingScreen();
} catch (error) {
console.error(error);
ideaContainer.innerHTML = `<p>Error occurred while fetching ideas.</p>`;
hideLoadingScreen();
}
}
// Delete an idea from the server
async function deleteIdea(ideaId) {
try {
await axios.delete(`http://localhost:8000/api/ideas/${ideaId}`);
fetchMyIdeas(); // Refresh the list of ideas
} catch (error) {
console.error(error);
}
}
// Attach event listeners to delete buttons
function attachDeleteButtonListeners() {
const deleteButtons = document.getElementsByClassName('delete-btn');
for (let i = 0; i < deleteButtons.length; i++) {
deleteButtons[i].addEventListener('click', function () {
const ideaId = this.dataset.ideaId;
deleteIdea(ideaId);
});
}
}
// Fetch and display user's submitted ideas
async function fetchMyIdeas() {
try {
showLoadingScreen();
const response = await axios.get('http://localhost:8000/api/ideas');
const ideas = response.data.data;
const myIdeas = ideas.filter((idea) => idea.username === displayName);
myIdeaList.innerHTML = '';
myIdeas.forEach((idea) => {
const listItem = `
<li class="px-4 py-3 rounded-md frosted-blur">
<div class="flex justify-between rounded-md frosted-dark">
<span class="justify-between text-xl my-2 px-2 py-1 rounded-md">${
idea.text
}</span>
</div>
<div class="flex my-2">
<span class="bg-amber-600 px-2 py-1 rounded-md">by: ${
idea.username
}</span>
<span class="${
tagColors[idea.tag]
} ml-2 px-2 py-1 rounded-md">tag: ${idea.tag}</span>
</div>
<button class="w-full delete-btn px-2 py-1 rounded-md frosted-red" data-idea-id="${
idea._id
}">Delete</button>
</li>
`;
myIdeaList.innerHTML += listItem;
});
attachDeleteButtonListeners(); // Attach event listeners to delete buttons
hideLoadingScreen();
} catch (error) {
console.error(error);
hideLoadingScreen();
}
}
// Fetch and display all ideas NOW with pagination - :)
async function fetchAllIdeas() {
try {
showLoadingScreen();
const response = await axios.get('http://localhost:8000/api/ideas');
const ideas = response.data.data;
const tagFilter = tagFilterSelect.value; // Get the selected tag filter value
// Filter ideas based on tag filter
filteredIdeas =
tagFilter !== ''
? ideas.filter((idea) => idea.tag === tagFilter)
: ideas;
// Apply name filter if available
const nameFilter = nameFilterInput.value.trim().toLowerCase();
if (nameFilter !== '') {
filteredIdeas = filteredIdeas.filter((idea) =>
idea.username.toLowerCase().includes(nameFilter)
);
}
// Calculate total number of pages
const totalPages = Math.ceil(filteredIdeas.length / itemsPerPage);
// Validate current page number
if (currentPage < 1) {
currentPage = 1;
} else if (currentPage > totalPages) {
currentPage = totalPages;
}
// Calculate start and end indices of ideas for the current page
const startIndex = (currentPage - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
// Display ideas for the current page
const currentPageIdeas = filteredIdeas.slice(startIndex, endIndex);
displayIdeas(currentPageIdeas);
// Generate pagination buttons
generatePaginationButtons(totalPages);
hideLoadingScreen();
} catch (error) {
console.error(error);
hideLoadingScreen();
}
}
// Display the ideas on the page
function displayIdeas(ideas) {
const list = document.getElementById('allIdeasList');
list.innerHTML = '';
ideas.forEach((idea) => {
const listItem = `
<li class="px-4 py-3 rounded-md frosted-blur">
<div class="p-2 rounded-md frosted-dark">
<span class="justify-between text-xl rounded-md">${idea.text}</span>
</div>
<div class="flex my-2">
<span class="bg-amber-600 px-2 py-1 rounded-md">by: ${
idea.username
}</span>
<span class="${
tagColors[idea.tag]
} ml-2 px-2 py-1 rounded-md">tag: ${idea.tag}</span>
</div>
</li>
`;
list.innerHTML += listItem;
});
}
// Generate pagination buttons
function generatePaginationButtons(totalPages) {
const paginationContainer = document.getElementById('pagination');
paginationContainer.innerHTML = '';
for (let i = 1; i <= totalPages; i++) {
const button = document.createElement('button');
button.textContent = i;
button.classList.add(
'ml-2',
'px-4',
'py-4',
'rounded-md',
'frosted-blur'
);
if (i === currentPage) {
button.classList.add('frosted-dark', 'text-white');
} else {
button.addEventListener('click', () => {
currentPage = i;
fetchAllIdeas();
});
}
paginationContainer.appendChild(button);
}
} // The Pagination Overall is currently very bad for performance because it is client side and there may be a better way to do this via client side but it works for now
// Filter ideas by name
function filterIdeasByName() {
const nameFilter = nameFilterInput.value.trim().toLowerCase();
const ideas = Array.from(allIdeasList.children);
ideas.forEach((idea) => {
const username = idea
.querySelector('.bg-amber-600')
.textContent.toLowerCase();
if (username.includes(nameFilter) || nameFilter === '') {
idea.style.display = 'block';
} else {
idea.style.display = 'none';
}
});
}
// Submit new idea to the server
async function submitIdea() {
const idea = ideaInput.value.trim();
const tag = tagInput.value.trim();
if (idea !== '' && tag !== 'tagselect') {
try {
const response = await axios.post('http://localhost:8000/api/ideas', {
text: idea,
tag,
username: displayName,
});
const newIdea = response.data.data;
ideaInput.value = '';
tagInput.value = '';
fetchMyIdeas(); // Refresh the list of ideas
fetchAllIdeas(); // Refresh the list of ideas
} catch (error) {
console.error(error);
}
}
}
// Event Listeners
saveDisplayNameBtn.addEventListener('click', saveDisplayName);
generatorTab.addEventListener('click', () =>
showTabContent(generatorContent)
);
closeDisplayNameBtn.addEventListener('click', () => {
modal.classList.add('hidden');
isModalVisible = false;
});
// Show add idea tab
addIdeaTab.addEventListener('click', () => showTabContent(addIdeaContent));
// Show my ideas
myIdeasTab.addEventListener('click', () => {
if (!isModalVisible) {
fetchMyIdeas();
showTabContent(myIdeasContent);
}
});
// Show all ideas
allIdeasTab.addEventListener('click', () => {
if (!isModalVisible) {
fetchAllIdeas();
showTabContent(allIdeasContent);
tagFilterSelect.addEventListener('change', fetchAllIdeas);
nameFilterInput.addEventListener('input', filterIdeasByName);
}
});
// If on all ideas tab run fetchAllIdeas on enter keypress
nameFilterInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
fetchAllIdeas();
}
});
// Generate random idea
generateBtn.addEventListener('click', fetchRandomIdea);
// Submit new idea
submitIdeaBtn.addEventListener('click', submitIdea);
// Change user
const changeUserBtn = document.getElementById('changeUserBtn');
changeUserBtn.addEventListener('click', promptDisplayName);
if (localStorage.getItem('displayName')) {
displayName = localStorage.getItem('displayName');
isModalVisible = false;
modal.classList.add('hidden');
document.getElementById(
'currentUser'
).innerHTML = `<p>User: ${displayName}`; // Update the current user element
} else {
promptDisplayName();
}
showLoadingScreen(); // Show loading screen on app initialization
fetchMyIdeas().then(() => {
fetchAllIdeas(); // Fetch all ideas after my ideas are fetched
hideLoadingScreen(); // Hide loading screen when all data is fetched
});
});
// Hide navbar on scroll down and show on scroll up smoothly
let prevScrollPos = window.pageYOffset;
window.onscroll = function () {
const currentScrollPos = window.pageYOffset;
if (prevScrollPos > currentScrollPos) {
document.getElementById('navbar').style.top = '0';
} else {
document.getElementById('navbar').style.top = '-100px';
}
prevScrollPos = currentScrollPos;
};
// Function to change the background to a darkened pixel art GIF based on the time of day
function changeBackgroundBasedOnTime() {
const currentDate = new Date();
const currentHour = currentDate.getHours();
document.addEventListener('mousemove', (event) => {
const bggif = document.getElementById('backgroundGifContainer');
// Calculate the new background position based on the mouse coordinates
const mouseX = event.clientX;
const mouseY = event.clientY;
const offsetX = mouseX / bggif.offsetWidth;
const offsetY = mouseY / bggif.offsetHeight;
// Update the background position using CSS transform property
bggif.style.transform = `translate(-${offsetX * 30}px, -${offsetY * 30}px)`;
}); // Very bad for performance :( but looks nice
// Define the URLs of the pixel art GIFs for different times of day
const backgroundGifs = {
morning: 'url(https://media.tenor.com/RRhijk6pHAoAAAAC/good-morning.gif)',
afternoon:
'url(https://64.media.tumblr.com/d216cb89703e2eef4191146b2ef6f34a/tumblr_prjou88Lvu1soktugo1_1280.gif)',
evening:
'url(https://64.media.tumblr.com/141a1667d0345d9f898a0389761cde17/f540a8cc0997a926-41/s1280x1920/ec704b6dbec3dbd020566385f8766469c37a5b64.gif)',
night:
'url(https://media.tenor.com/TIUfh_o9hIUAAAAC/minimoss-pixel-art.gif)',
};
// Set the background image based on the current time of day
if (currentHour >= 5 && currentHour < 12) {
document.getElementById('backgroundGif').style.backgroundImage =
backgroundGifs.morning; // in 12 hour format = 5am to 12pm
} else if (currentHour >= 12 && currentHour < 18) {
document.getElementById('backgroundGif').style.backgroundImage =
backgroundGifs.afternoon; // in 12 hour format = 12pm to 6pm
} else if (currentHour >= 18 && currentHour < 22) {
document.getElementById('backgroundGif').style.backgroundImage =
backgroundGifs.evening; // in 12 hour format = 6pm to 10pm
} else {
document.getElementById('backgroundGif').style.backgroundImage =
backgroundGifs.night; // in 12 hour format = 10pm to 5am
}
}
// Call the function to change the background based on the current time
changeBackgroundBasedOnTime();
// Secret Code Modal
// the secret code
const secretCode = 'secret12345';
// key sequence tracker
let enteredCode = '';
const secretCodeModal = document.getElementById('secretCodeModal');
const secretCodeInput = document.getElementById('secretCodeInput');
// Add event listener to the submit button
document.getElementById('submitSecretCodeBtn').addEventListener('click', () => {
checkSecretCode();
});
// Add event listener to the close button
document
.getElementById('closeSecretCodeModalBtn')
.addEventListener('click', () => {
closeSecretCodeModal();
});
// Add event listener to the document for keypress events
document.addEventListener('keypress', (event) => {
// Get the pressed key
const key = String.fromCharCode(event.keyCode);
enteredCode += key;
// Reset the enteredCode if the sequence is wrong
if (!secretCode.startsWith(enteredCode)) {
enteredCode = '';
}
// Check if the enteredCode matches the secretCode
if (enteredCode === secretCode) {
// Clear the enteredCode
enteredCode = '';
// Open the secret code modal
openSecretCodeModal();
}
});
// Function to check the entered secret code
function checkSecretCode() {
if (secretCodeInput.value === secretCode) {
// Clear the enteredCode
enteredCode = '';
// Open the secret code modal
openSecretCodeModal();
} else {
// Clear the enteredCode and the input field
enteredCode = '';
secretCodeInput.value = '';
// Perform any action to handle incorrect code
console.log('Incorrect secret code');
}
}
// Function to open the secret code modal
function openSecretCodeModal() {
secretCodeModal.classList.remove('hidden');
}
// Function to close the secret code modal
function closeSecretCodeModal() {
secretCodeModal.classList.add('hidden');
}