Complete profanity filter UI integration
- Added tab-based admin interface with separate Profanity Filter tab - Implemented custom word management (add/delete words with severity levels) - Added profanity filter testing interface for real-time validation - Integrated with profanity database API endpoints - Added comprehensive CSS styling for new UI components - Full admin interface for managing custom profanity words and categories
This commit is contained in:
parent
c7f39e4939
commit
71ddcc9a5c
2 changed files with 390 additions and 23 deletions
199
public/admin.js
199
public/admin.js
|
@ -71,6 +71,53 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
loadLocations();
|
||||
});
|
||||
|
||||
// Tab navigation logic
|
||||
const tabs = document.querySelectorAll('.tab-btn');
|
||||
const contents = document.querySelectorAll('.tab-content');
|
||||
|
||||
tabs.forEach(tab => {
|
||||
tab.addEventListener('click', function () {
|
||||
tabs.forEach(t => t.classList.remove('active'));
|
||||
contents.forEach(c => c.classList.remove('active'));
|
||||
this.classList.add('active');
|
||||
document.querySelector(`#${this.dataset.tab}-tab`).classList.add('active');
|
||||
|
||||
// Load data for the active tab
|
||||
if (this.dataset.tab === 'profanity') {
|
||||
loadProfanityWords();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Profanity management handlers
|
||||
const addProfanityForm = document.getElementById('add-profanity-form');
|
||||
const testProfanityForm = document.getElementById('test-profanity-form');
|
||||
const profanityTableBody = document.getElementById('profanity-tbody');
|
||||
|
||||
if (addProfanityForm) {
|
||||
addProfanityForm.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
const word = document.getElementById('new-word').value.trim();
|
||||
const severity = document.getElementById('new-severity').value;
|
||||
const category = document.getElementById('new-category').value.trim() || 'custom';
|
||||
|
||||
if (!word) return;
|
||||
|
||||
await addProfanityWord(word, severity, category);
|
||||
});
|
||||
}
|
||||
|
||||
if (testProfanityForm) {
|
||||
testProfanityForm.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
const text = document.getElementById('test-text').value.trim();
|
||||
|
||||
if (!text) return;
|
||||
|
||||
await testProfanityFilter(text);
|
||||
});
|
||||
}
|
||||
|
||||
function showLoginSection() {
|
||||
loginSection.style.display = 'block';
|
||||
adminSection.style.display = 'none';
|
||||
|
@ -462,6 +509,158 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
return reportTime.toLocaleDateString();
|
||||
}
|
||||
|
||||
// Profanity management functions
|
||||
async function loadProfanityWords() {
|
||||
if (!profanityTableBody) return;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/profanity/words', {
|
||||
headers: { 'Authorization': `Bearer ${authToken}` }
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
logout('Session expired. Please log in again.');
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
displayProfanityWords(data.words || []);
|
||||
} else {
|
||||
console.error('Failed to load profanity words:', data.error);
|
||||
profanityTableBody.innerHTML = '<tr><td colspan="6">Failed to load words</td></tr>';
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading profanity words:', error);
|
||||
profanityTableBody.innerHTML = '<tr><td colspan="6">Error loading words</td></tr>';
|
||||
}
|
||||
}
|
||||
|
||||
function displayProfanityWords(words) {
|
||||
if (!profanityTableBody) return;
|
||||
|
||||
if (words.length === 0) {
|
||||
profanityTableBody.innerHTML = '<tr><td colspan="6">No custom words added yet</td></tr>';
|
||||
return;
|
||||
}
|
||||
|
||||
profanityTableBody.innerHTML = words.map(word => `
|
||||
<tr>
|
||||
<td>${word.id}</td>
|
||||
<td><code>${escapeHtml(word.word)}</code></td>
|
||||
<td><span class="severity-${word.severity}">${word.severity}</span></td>
|
||||
<td>${word.category || 'N/A'}</td>
|
||||
<td>${new Date(word.created_at).toLocaleDateString()}</td>
|
||||
<td>
|
||||
<button class="action-btn danger" onclick="deleteProfanityWord(${word.id})">
|
||||
🗑️ Delete
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
async function addProfanityWord(word, severity, category) {
|
||||
try {
|
||||
const response = await fetch('/api/admin/profanity/words', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${authToken}`
|
||||
},
|
||||
body: JSON.stringify({ word, severity, category })
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
// Clear form
|
||||
document.getElementById('new-word').value = '';
|
||||
document.getElementById('new-category').value = 'custom';
|
||||
document.getElementById('new-severity').value = 'medium';
|
||||
|
||||
// Reload words list
|
||||
loadProfanityWords();
|
||||
|
||||
console.log('Word added successfully');
|
||||
} else {
|
||||
alert('Failed to add word: ' + data.error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error adding profanity word:', error);
|
||||
alert('Error adding word. Please try again.');
|
||||
}
|
||||
}
|
||||
|
||||
async function testProfanityFilter(text) {
|
||||
const resultsDiv = document.getElementById('test-results');
|
||||
if (!resultsDiv) return;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/profanity/test', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${authToken}`
|
||||
},
|
||||
body: JSON.stringify({ text })
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
if (data.isProfane) {
|
||||
resultsDiv.className = 'test-results profane';
|
||||
resultsDiv.innerHTML = `
|
||||
<strong>⚠️ Profanity Detected!</strong><br>
|
||||
Detected words: ${data.detectedWords.map(w => `<code>${escapeHtml(w)}</code>`).join(', ')}<br>
|
||||
Filtered text: "${escapeHtml(data.filteredText)}"
|
||||
`;
|
||||
} else {
|
||||
resultsDiv.className = 'test-results clean';
|
||||
resultsDiv.innerHTML = '<strong>✅ Text is clean!</strong><br>No profanity detected.';
|
||||
}
|
||||
} else {
|
||||
resultsDiv.className = 'test-results empty';
|
||||
resultsDiv.innerHTML = 'Error testing text: ' + data.error;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error testing profanity filter:', error);
|
||||
resultsDiv.className = 'test-results empty';
|
||||
resultsDiv.innerHTML = 'Error testing text. Please try again.';
|
||||
}
|
||||
}
|
||||
|
||||
// Make deleteProfanityWord available globally for onclick handlers
|
||||
window.deleteProfanityWord = async function(wordId) {
|
||||
if (!confirm('Are you sure you want to delete this word?')) return;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/admin/profanity/words/${wordId}`, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': `Bearer ${authToken}` }
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
loadProfanityWords();
|
||||
console.log('Word deleted successfully');
|
||||
} else {
|
||||
const data = await response.json();
|
||||
alert('Failed to delete word: ' + data.error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting profanity word:', error);
|
||||
alert('Error deleting word. Please try again.');
|
||||
}
|
||||
};
|
||||
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
// Initialize theme toggle
|
||||
initializeTheme();
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue