feat: notification dropdown panel and /notifications page
This commit is contained in:
@@ -40,6 +40,12 @@
|
||||
<span>Dashboard</span>
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a href="/notifications" class="nav-link <?= str_starts_with($_SERVER['REQUEST_URI'] ?? '', '/notifications') ? 'active' : '' ?>">
|
||||
<i class="fas fa-bell"></i>
|
||||
<span>Notifications</span>
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item nav-item-accordion <?= str_starts_with($_SERVER['REQUEST_URI'] ?? '', '/servers') ? 'active' : '' ?>">
|
||||
<a href="#" class="nav-link" onclick="toggleServerList(event)">
|
||||
<i class="fas fa-hdd"></i>
|
||||
@@ -151,6 +157,28 @@
|
||||
<i class="fas fa-bell"></i>
|
||||
<span class="notification-badge" id="notificationBadge" style="display:none">0</span>
|
||||
</button>
|
||||
<div class="notification-dropdown" id="notifDropdown">
|
||||
<div class="notif-dropdown-header">
|
||||
<span style="font-weight:600;font-size:0.9em">Notifications</span>
|
||||
<div style="display:flex;align-items:center;gap:6px">
|
||||
<button class="btn-text" id="markAllReadBtn" onclick="markAllRead()" style="display:none;font-size:0.78em;padding:2px 6px;border-radius:4px">
|
||||
<i class="fas fa-check-double"></i> Mark all read
|
||||
</button>
|
||||
<button class="btn-text" onclick="closeNotifications()" style="font-size:0.78em;padding:2px 6px;border-radius:4px">×</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="notif-dropdown-body" id="notifList">
|
||||
<div style="padding:40px 20px;text-align:center;color:var(--text-muted)">
|
||||
<i class="fas fa-spinner fa-spin" style="font-size:1.5em;margin-bottom:10px;display:block"></i>
|
||||
<span>Loading notifications...</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="notif-dropdown-footer">
|
||||
<a href="/notifications" class="btn-text" style="font-size:0.82em;color:var(--info);width:100%;text-align:center;padding:8px;display:block">
|
||||
<i class="fas fa-external-link-alt"></i> View all notifications
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -178,34 +206,6 @@
|
||||
|
||||
<div id="toastContainer" class="toast-container"></div>
|
||||
|
||||
<div id="notificationModal" class="modal">
|
||||
<div class="modal-dialog" style="max-width:480px">
|
||||
<div class="modal-header">
|
||||
<h3 style="font-size:1em;display:flex;align-items:center;gap:8px">
|
||||
<i class="fas fa-bell" style="color:var(--info)"></i> Notifications
|
||||
<span id="notifCountBadge" class="badge" style="display:none;font-size:0.75em">0</span>
|
||||
</h3>
|
||||
<div style="display:flex;align-items:center;gap:8px">
|
||||
<button class="btn btn-sm btn-text" id="markAllReadBtn" onclick="markAllRead()" style="display:none;font-size:0.82em">
|
||||
<i class="fas fa-check-double"></i> Mark all read
|
||||
</button>
|
||||
<button type="button" class="modal-close" onclick="closeNotifications()">×</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-body" style="padding:0;max-height:420px;overflow-y:auto" id="notifList">
|
||||
<div style="padding:40px 20px;text-align:center;color:var(--text-muted)">
|
||||
<i class="fas fa-spinner fa-spin" style="font-size:1.5em;margin-bottom:10px;display:block"></i>
|
||||
<span>Loading notifications...</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer" style="justify-content:center;padding:0.7rem">
|
||||
<a href="/admin/notifications" class="btn btn-sm btn-text" style="font-size:0.85em;color:var(--info)">
|
||||
<i class="fas fa-external-link-alt"></i> View all in admin panel
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/assets/js/app.js"></script>
|
||||
<?php if (isset($scripts)): ?>
|
||||
<?php foreach ($scripts as $script): ?>
|
||||
@@ -220,8 +220,8 @@ function getApiToken() {
|
||||
}
|
||||
|
||||
function toggleNotifications() {
|
||||
const modal = document.getElementById('notificationModal');
|
||||
if (modal.style.display === 'flex') {
|
||||
const dd = document.getElementById('notifDropdown');
|
||||
if (dd.classList.contains('open')) {
|
||||
closeNotifications();
|
||||
} else {
|
||||
openNotifications();
|
||||
@@ -229,16 +229,23 @@ function toggleNotifications() {
|
||||
}
|
||||
|
||||
function openNotifications() {
|
||||
const modal = document.getElementById('notificationModal');
|
||||
modal.style.display = 'flex';
|
||||
modal.onclick = function(e) {
|
||||
if (e.target === this) closeNotifications();
|
||||
};
|
||||
const dd = document.getElementById('notifDropdown');
|
||||
dd.classList.add('open');
|
||||
fetchNotifications();
|
||||
document.addEventListener('click', notifOutsideClick);
|
||||
}
|
||||
|
||||
function closeNotifications() {
|
||||
document.getElementById('notificationModal').style.display = 'none';
|
||||
const dd = document.getElementById('notifDropdown');
|
||||
dd.classList.remove('open');
|
||||
document.removeEventListener('click', notifOutsideClick);
|
||||
}
|
||||
|
||||
function notifOutsideClick(e) {
|
||||
const wrapper = document.querySelector('.notification-btn-wrapper');
|
||||
if (!wrapper.contains(e.target)) {
|
||||
closeNotifications();
|
||||
}
|
||||
}
|
||||
|
||||
function fetchNotifications() {
|
||||
@@ -351,15 +358,11 @@ function updateUnreadCount() {
|
||||
.then(d => {
|
||||
const count = d.unread_count || 0;
|
||||
const badge = document.getElementById('notificationBadge');
|
||||
const notifCount = document.getElementById('notifCountBadge');
|
||||
if (count > 0) {
|
||||
badge.textContent = count > 99 ? '99+' : count;
|
||||
badge.style.display = 'inline-flex';
|
||||
notifCount.textContent = count;
|
||||
notifCount.style.display = 'inline-flex';
|
||||
} else {
|
||||
badge.style.display = 'none';
|
||||
notifCount.style.display = 'none';
|
||||
}
|
||||
})
|
||||
.catch(function(){});
|
||||
|
||||
91
views/notifications/index.php
Normal file
91
views/notifications/index.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
$notifications = $notifications ?? [];
|
||||
$pagination = $pagination ?? [];
|
||||
$unreadCount = $unreadCount ?? 0;
|
||||
$data = $notifications ?? [];
|
||||
?>
|
||||
|
||||
<div class="page-header">
|
||||
<div class="page-header-left">
|
||||
<div class="back-link">
|
||||
<a href="/dashboard"><i class="fas fa-arrow-left"></i> Back to Dashboard</a>
|
||||
</div>
|
||||
<h1 class="page-title">Notifications</h1>
|
||||
<p class="page-subtitle">
|
||||
<?= $unreadCount ?> unread · <?= $pagination['total'] ?? 0 ?> total
|
||||
</p>
|
||||
</div>
|
||||
<div class="page-header-right">
|
||||
<?php if ($unreadCount > 0): ?>
|
||||
<button class="btn btn-sm btn-text" onclick="markAllRead()" style="font-size:0.85em">
|
||||
<i class="fas fa-check-double"></i> Mark all read
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body" style="padding:0">
|
||||
<?php if (empty($data)): ?>
|
||||
<div style="padding:60px 20px;text-align:center;color:var(--text-muted)">
|
||||
<i class="fas fa-check-circle" style="font-size:2.5em;margin-bottom:12px;display:block;color:var(--success)"></i>
|
||||
<p style="font-size:1.05em">All caught up!</p>
|
||||
<p style="font-size:0.9em">No notifications to show.</p>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<?php foreach ($data as $n): ?>
|
||||
<?php
|
||||
$typeColors = ['error' => 'danger', 'warning' => 'warning', 'info' => 'info'];
|
||||
$icons = ['error' => 'fa-times-circle', 'warning' => 'fa-exclamation-triangle', 'info' => 'fa-info-circle'];
|
||||
$tc = $typeColors[$n['type']] ?? 'info';
|
||||
$icon = $icons[$n['type']] ?? 'fa-info-circle';
|
||||
$isRead = !empty($n['is_read']);
|
||||
?>
|
||||
<div class="notif-item <?= $isRead ? 'notif-read' : 'notif-unread' ?>" data-id="<?= $n['id'] ?>">
|
||||
<div class="notif-icon notif-<?= $tc ?>"><i class="fas <?= $icon ?>"></i></div>
|
||||
<div class="notif-content">
|
||||
<div class="notif-title"><?= htmlspecialchars($n['title'] ?? '') ?></div>
|
||||
<div class="notif-message"><?= htmlspecialchars($n['message'] ?? '') ?></div>
|
||||
<div class="notif-time"><?= time_ago($n['created_at'] ?? '') ?></div>
|
||||
</div>
|
||||
<?php if (!$isRead): ?>
|
||||
<div class="notif-dot"></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if (($pagination['total_pages'] ?? 1) > 1): ?>
|
||||
<div class="pagination">
|
||||
<?php for ($i = 1; $i <= $pagination['total_pages']; $i++): ?>
|
||||
<a href="/notifications?page=<?= $i ?>" class="btn btn-sm <?= ($pagination['page'] ?? 1) === $i ? 'btn-primary' : 'btn-secondary' ?>">
|
||||
<?= $i ?>
|
||||
</a>
|
||||
<?php endfor; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<script>
|
||||
function markAllRead() {
|
||||
const token = document.querySelector('meta[name="api-token"]')?.getAttribute('content') || '';
|
||||
const headers = { 'Accept': 'application/json' };
|
||||
if (token) headers['Authorization'] = 'Bearer ' + token;
|
||||
|
||||
fetch('/api/notifications/read-all', { method: 'POST', headers })
|
||||
.then(r => r.json())
|
||||
.then(d => {
|
||||
if (d.success) {
|
||||
document.querySelectorAll('.notif-item').forEach(function(el) {
|
||||
el.classList.remove('notif-unread');
|
||||
el.classList.add('notif-read');
|
||||
const dot = el.querySelector('.notif-dot');
|
||||
if (dot) dot.remove();
|
||||
});
|
||||
location.reload();
|
||||
}
|
||||
})
|
||||
.catch(function(){});
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user