Add make/remove admin button in admin dashboard
This commit is contained in:
parent
b6461b3d62
commit
58e86973b2
45
admin.html
45
admin.html
@ -540,9 +540,20 @@
|
|||||||
<td class="px-4 py-3 font-medium">${(u.credits || 0).toLocaleString()}</td>
|
<td class="px-4 py-3 font-medium">${(u.credits || 0).toLocaleString()}</td>
|
||||||
<td class="px-4 py-3 text-slate-400 text-sm">${formatDate(u.created_at)}</td>
|
<td class="px-4 py-3 text-slate-400 text-sm">${formatDate(u.created_at)}</td>
|
||||||
<td class="px-4 py-3">
|
<td class="px-4 py-3">
|
||||||
|
<div class="flex gap-2">
|
||||||
<button onclick="openCreditsModal('${u.id}', '${escapeHtml(u.name || u.email)}', ${u.credits || 0})" class="btn-secondary px-3 py-1 rounded text-sm">
|
<button onclick="openCreditsModal('${u.id}', '${escapeHtml(u.name || u.email)}', ${u.credits || 0})" class="btn-secondary px-3 py-1 rounded text-sm">
|
||||||
Credits
|
Credits
|
||||||
</button>
|
</button>
|
||||||
|
${u.role === 'admin' ? `
|
||||||
|
<button onclick="toggleAdmin('${u.id}', 'user')" class="px-3 py-1 rounded text-sm bg-amber-500/20 text-amber-400 hover:bg-amber-500/30 transition-colors">
|
||||||
|
Remove Admin
|
||||||
|
</button>
|
||||||
|
` : `
|
||||||
|
<button onclick="toggleAdmin('${u.id}', 'admin')" class="px-3 py-1 rounded text-sm bg-purple-500/20 text-purple-400 hover:bg-purple-500/30 transition-colors">
|
||||||
|
Make Admin
|
||||||
|
</button>
|
||||||
|
`}
|
||||||
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
`).join('');
|
`).join('');
|
||||||
@ -599,6 +610,40 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function toggleAdmin(userId, newRole) {
|
||||||
|
const user = users.find(u => u.id === userId);
|
||||||
|
const userName = user?.name || user?.email || 'this user';
|
||||||
|
|
||||||
|
const action = newRole === 'admin' ? 'promote' : 'demote';
|
||||||
|
if (!confirm(`Are you sure you want to ${action} ${userName} to ${newRole}?`)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = await moxieAuth.getToken();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/users/${userId}/role`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ role: newRole })
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data.success) {
|
||||||
|
await loadUsers();
|
||||||
|
} else {
|
||||||
|
alert('Failed to update role: ' + data.message);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Role update error:', error);
|
||||||
|
alert('Failed to update role');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function switchTab(tab) {
|
function switchTab(tab) {
|
||||||
currentTab = tab;
|
currentTab = tab;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user