diff --git a/public/src/admin/dashboard/bug-logs.js b/public/src/admin/dashboard/bug-logs.js index c16d722db8..5af84ccb95 100644 --- a/public/src/admin/dashboard/bug-logs.js +++ b/public/src/admin/dashboard/bug-logs.js @@ -1,6 +1,59 @@ 'use strict'; -define('admin/dashboard/bug-logs', [], () => { +define('admin/dashboard/bug-logs', ['jquery', 'api'], ($, api) => { + const BugLogs = {}; + BugLogs.init = () => { + // Fetch and display bug logs + fetchBugLogs(); + // Handle bug report submission + $('#submit-bug-report').on('click', submitBugReport); + }; + + function fetchBugLogs() { + api.get('/api/admin/get-bug-log') + .then((data) => { + const bugLogsContainer = $('#bug-logs-container'); + bugLogsContainer.empty(); + + if (data.bugLogs && data.bugLogs.length > 0) { + data.bugLogs.forEach((log) => { + const logElement = $('
').addClass('bug-log'); + logElement.append($('

').text(`User: ${log.user}`)); + logElement.append($('

').text(`Description: ${log.description}`)); + logElement.append($('

').text(`Timestamp: ${log.timestamp}`)); + bugLogsContainer.append(logElement); + }); + } else { + bugLogsContainer.append($('

').text('No bug logs found.')); + } + }) + .catch((err) => { + console.error('Error fetching bug logs:', err); + $('#bug-logs-container').append($('

').text('Error fetching bug logs.')); + }); + } + + function submitBugReport() { + const description = $('#bug-report-description').val().trim(); + + if (!description) { + alert('Description is required'); + return; + } + + api.post('/api/admin/submit-bug-report', { description }) + .then(() => { + alert('Bug report submitted successfully'); + $('#bug-report-description').val(''); + fetchBugLogs(); + }) + .catch((err) => { + console.error('Error submitting bug report:', err); + alert('Error submitting bug report'); + }); + } + + return BugLogs; }); diff --git a/src/controllers/admin/dashboard.js b/src/controllers/admin/dashboard.js index 222b8b863f..b264937ea7 100644 --- a/src/controllers/admin/dashboard.js +++ b/src/controllers/admin/dashboard.js @@ -390,6 +390,44 @@ dashboardController.getSearches = async (req, res) => { }); }; +const bugLogs = []; + dashboardController.getBugLogs = async function (req, res) { - res.render('admin/dashboard/bug-logs', {}); + console.log('getbuglogs'); // Add logging + try { + // Sanitize and format bug logs before rendering + const sanitizedBugLogs = bugLogs.map(log => ({ + user: validator.escape(String(log.user)), + description: validator.escape(String(log.description)), + timestamp: new Date(log.timestamp).toISOString(), + })); + + // Pass the sanitized bug logs to the view for rendering + res.render('admin/dashboard/bug-logs', { bugLogs: sanitizedBugLogs }); + } catch (error) { + console.error('Error fetching bug logs:', error); // Log the error for debugging + res.status(500).json({ message: 'Internal server error' }); + } +}; + + +dashboardController.submitBugReport = async function (req, res) { + try { + const { description } = req.body; + if (!description) { + return res.status(400).json({ message: 'Description is required' }); + } + + const sanitizedDescription = validator.escape(description); + const timestamp = Date.now(); + const user = req.user ? req.user.username : 'Anonymous'; // Assuming req.user contains the user information + + // Add the bug report to the in-memory array + bugLogs.push({ user, description: sanitizedDescription, timestamp }); + + res.status(201).json({ message: 'Bug report submitted successfully' }); + } catch (error) { + console.error('Error submitting bug report:', error); // Log the error for debugging + res.status(500).json({ message: 'Internal server error' }); + } }; diff --git a/src/views/bug-report-form.tpl b/src/views/bug-report-form.tpl index 3ef158b38c..2d4d29933d 100644 --- a/src/views/bug-report-form.tpl +++ b/src/views/bug-report-form.tpl @@ -4,6 +4,7 @@ Bug Report Form +