rss-reader/static/js/app.js

91 lines
2.7 KiB
JavaScript
Raw Normal View History

2025-02-02 01:23:21 -08:00
// Logout functionality
document.getElementById('logoutButton').addEventListener('click', async () => {
try {
const response = await fetch('/logout', {
method: 'POST',
});
if (response.ok) {
window.location.href = '/login';
}
} catch (error) {
console.error('Logout failed:', error);
}
});
// Modal functionality
const modal = document.getElementById('addFeedModal');
const addFeedButton = document.getElementById('addFeedButton');
const cancelButton = document.getElementById('cancelAddFeed');
const confirmButton = document.getElementById('confirmAddFeed');
const addFeedForm = document.getElementById('addFeedForm');
2025-02-02 02:11:13 -08:00
const errorMessage = document.getElementById('feedErrorMessage');
2025-02-02 01:23:21 -08:00
function showModal() {
modal.classList.add('show');
2025-02-02 02:11:13 -08:00
errorMessage.style.display = 'none';
addFeedForm.reset();
2025-02-02 01:23:21 -08:00
}
function hideModal() {
modal.classList.remove('show');
2025-02-02 02:11:13 -08:00
errorMessage.style.display = 'none';
2025-02-02 01:23:21 -08:00
addFeedForm.reset();
}
2025-02-02 02:11:13 -08:00
function showError(message) {
errorMessage.textContent = message;
errorMessage.style.display = 'block';
}
2025-02-02 01:23:21 -08:00
addFeedButton.addEventListener('click', showModal);
cancelButton.addEventListener('click', hideModal);
// Close modal when clicking outside
modal.addEventListener('click', (e) => {
if (e.target === modal) {
hideModal();
}
});
confirmButton.addEventListener('click', async () => {
2025-02-02 02:11:13 -08:00
const name = document.getElementById('feedName').value.trim();
const url = document.getElementById('feedUrl').value.trim();
if (!name || !url) {
showError('Please fill in all fields');
2025-02-02 01:23:21 -08:00
return;
}
2025-02-02 02:11:13 -08:00
try {
const response = await fetch('/feeds', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name, url }),
});
if (response.ok) {
hideModal();
// TODO: Update the feed list in the UI
// For now, we'll just reload the page
window.location.reload();
} else {
const errorText = await response.text();
switch (response.status) {
case 409:
showError('You already have this feed added');
break;
case 422:
showError('Invalid feed URL. Please make sure it starts with http:// or https://');
break;
default:
showError('Failed to add feed. Please try again.');
}
}
} catch (error) {
console.error('Add feed failed:', error);
showError('An error occurred. Please try again.');
}
2025-02-02 01:23:21 -08:00
});