fix settings page csrf
This commit is contained in:
@@ -196,140 +196,6 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const eventTypeFilter = document.getElementById('eventTypeFilter');
|
||||
const dateRangeFilter = document.getElementById('dateRangeFilter');
|
||||
const userFilter = document.getElementById('userFilter');
|
||||
const clearFiltersBtn = document.getElementById('clearFilters');
|
||||
const eventsTableBody = document.getElementById('eventsTableBody');
|
||||
const currentPageSpan = document.getElementById('currentPage');
|
||||
const totalPagesSpan = document.getElementById('totalPages');
|
||||
const prevPageBtn = document.getElementById('prevPage');
|
||||
const nextPageBtn = document.getElementById('nextPage');
|
||||
|
||||
let currentPage = 1;
|
||||
let totalPages = parseInt(totalPagesSpan.textContent);
|
||||
let isFetching = false;
|
||||
|
||||
// Function to update the URL with filter parameters
|
||||
function updateURL() {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
params.set('event_type', eventTypeFilter.value);
|
||||
params.set('date_range', dateRangeFilter.value);
|
||||
params.set('user_id', userFilter.value);
|
||||
params.set('page', currentPage);
|
||||
window.history.replaceState({}, '', `${window.location.pathname}?${params.toString()}`);
|
||||
}
|
||||
|
||||
// Function to fetch filtered events
|
||||
function fetchEvents() {
|
||||
if (isFetching) return;
|
||||
isFetching = true;
|
||||
|
||||
const params = new URLSearchParams({
|
||||
event_type: eventTypeFilter.value,
|
||||
date_range: dateRangeFilter.value,
|
||||
user_id: userFilter.value,
|
||||
page: currentPage,
|
||||
ajax: 'true' // Add this to indicate it's an AJAX request
|
||||
});
|
||||
|
||||
fetch(`${window.location.pathname}?${params.toString()}`, {
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
return response.text();
|
||||
})
|
||||
.then(html => {
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(html, 'text/html');
|
||||
const newTableBody = doc.getElementById('eventsTableBody');
|
||||
|
||||
if (newTableBody) {
|
||||
eventsTableBody.innerHTML = newTableBody.innerHTML;
|
||||
|
||||
// Update pagination
|
||||
const newCurrentPage = parseInt(doc.getElementById('currentPage').textContent);
|
||||
const newTotalPages = parseInt(doc.getElementById('totalPages').textContent);
|
||||
currentPage = newCurrentPage;
|
||||
totalPages = newTotalPages;
|
||||
currentPageSpan.textContent = currentPage;
|
||||
totalPagesSpan.textContent = totalPages;
|
||||
|
||||
// Update pagination buttons
|
||||
prevPageBtn.disabled = currentPage === 1;
|
||||
nextPageBtn.disabled = currentPage === totalPages;
|
||||
|
||||
// Update URL
|
||||
updateURL();
|
||||
} else {
|
||||
console.error('Could not find events table in response');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error fetching events:', error);
|
||||
// Optionally show an error message to the user
|
||||
})
|
||||
.finally(() => {
|
||||
isFetching = false;
|
||||
});
|
||||
}
|
||||
|
||||
// Add event listeners for filters with debounce
|
||||
let filterTimeout;
|
||||
function debouncedFetch() {
|
||||
clearTimeout(filterTimeout);
|
||||
filterTimeout = setTimeout(fetchEvents, 300);
|
||||
}
|
||||
|
||||
eventTypeFilter.addEventListener('change', debouncedFetch);
|
||||
dateRangeFilter.addEventListener('change', debouncedFetch);
|
||||
userFilter.addEventListener('change', debouncedFetch);
|
||||
|
||||
// Add event listeners for pagination
|
||||
prevPageBtn.addEventListener('click', () => {
|
||||
if (currentPage > 1) {
|
||||
currentPage--;
|
||||
fetchEvents();
|
||||
}
|
||||
});
|
||||
|
||||
nextPageBtn.addEventListener('click', () => {
|
||||
if (currentPage < totalPages) {
|
||||
currentPage++;
|
||||
fetchEvents();
|
||||
}
|
||||
});
|
||||
|
||||
// Add event listener for clear filters
|
||||
clearFiltersBtn.addEventListener('click', () => {
|
||||
eventTypeFilter.value = '';
|
||||
dateRangeFilter.value = '24h';
|
||||
userFilter.value = '';
|
||||
currentPage = 1;
|
||||
fetchEvents();
|
||||
});
|
||||
|
||||
// Initialize filters from URL parameters
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
eventTypeFilter.value = params.get('event_type') || '';
|
||||
dateRangeFilter.value = params.get('date_range') || '24h';
|
||||
userFilter.value = params.get('user_id') || '';
|
||||
currentPage = parseInt(params.get('page')) || 1;
|
||||
|
||||
// Initial fetch if filters are set
|
||||
if (eventTypeFilter.value || dateRangeFilter.value !== '24h' || userFilter.value) {
|
||||
fetchEvents();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% endmacro %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
Reference in New Issue
Block a user