user
This commit is contained in:
27
public/assets/js/account.js
Normal file
27
public/assets/js/account.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { apiAction } from './api.js';
|
||||
import { initUserPanel, initAccountPage } from './ui-user.js';
|
||||
import { mountLogoutButton, ensureFloatingLogout } from './ui-auth.js';
|
||||
|
||||
async function ensureAuthenticated() {
|
||||
try {
|
||||
const me = await apiAction('auth.me', { method: 'GET' });
|
||||
if (!me?.ok || !me?.user) {
|
||||
window.location.href = '/login.php';
|
||||
return false;
|
||||
}
|
||||
window.__currentUser = me.user;
|
||||
document.documentElement.classList.remove('auth-pending');
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
const ok = await ensureAuthenticated();
|
||||
if (!ok) return;
|
||||
initUserPanel();
|
||||
initAccountPage();
|
||||
mountLogoutButton('#btn-logout', { redirect: '/login.php' });
|
||||
ensureFloatingLogout({ redirect: '/login.php' });
|
||||
});
|
||||
@@ -9,7 +9,6 @@ const state = {
|
||||
loading: false,
|
||||
};
|
||||
|
||||
let dialog;
|
||||
let avatarBtn;
|
||||
let profileForm;
|
||||
let passwordForm;
|
||||
@@ -18,31 +17,26 @@ let teamTable;
|
||||
let userForm;
|
||||
|
||||
export function initUserPanel() {
|
||||
dialog = document.getElementById('userDialog');
|
||||
avatarBtn = document.getElementById('btn-user');
|
||||
if (!dialog || !avatarBtn) return;
|
||||
updateAvatar();
|
||||
}
|
||||
|
||||
export function initAccountPage() {
|
||||
profileForm = document.getElementById('profileForm');
|
||||
passwordForm = document.getElementById('passwordForm');
|
||||
settingsForm = document.getElementById('settingsForm');
|
||||
teamTable = document.getElementById('teamTable');
|
||||
userForm = document.getElementById('userForm');
|
||||
|
||||
avatarBtn.addEventListener('click', () => openUserDialog());
|
||||
document.getElementById('userClose')?.addEventListener('click', () => dialog.close());
|
||||
|
||||
profileForm?.addEventListener('submit', submitProfileForm);
|
||||
passwordForm?.addEventListener('submit', submitPasswordForm);
|
||||
settingsForm?.addEventListener('submit', submitSettingsForm);
|
||||
|
||||
document.getElementById('btn-user-add')?.addEventListener('click', () => openUserForm());
|
||||
document.getElementById('userFormCancel')?.addEventListener('click', () => closeUserForm());
|
||||
userForm?.addEventListener('submit', submitUserForm);
|
||||
|
||||
profileForm?.addEventListener('submit', submitProfileForm);
|
||||
passwordForm?.addEventListener('submit', submitPasswordForm);
|
||||
settingsForm?.addEventListener('submit', submitSettingsForm);
|
||||
teamTable?.addEventListener('click', handleTeamTableClick);
|
||||
|
||||
dialog.addEventListener('close', () => closeUserForm());
|
||||
|
||||
document.querySelectorAll('[data-user-tab]').forEach(btn => {
|
||||
btn.addEventListener('click', () => switchTab(btn.getAttribute('data-user-tab')));
|
||||
});
|
||||
@@ -64,8 +58,8 @@ export function initUserPanel() {
|
||||
});
|
||||
});
|
||||
|
||||
updateAvatar();
|
||||
updateOwnerVisibility();
|
||||
switchTab(state.currentTab);
|
||||
loadAccountData();
|
||||
}
|
||||
|
||||
function isOwner() {
|
||||
@@ -98,13 +92,6 @@ function switchTab(tab) {
|
||||
});
|
||||
}
|
||||
|
||||
async function openUserDialog() {
|
||||
if (dialog.open || state.loading) return;
|
||||
dialog.showModal();
|
||||
switchTab(state.currentTab);
|
||||
await loadAccountData();
|
||||
}
|
||||
|
||||
async function loadAccountData() {
|
||||
try {
|
||||
state.loading = true;
|
||||
@@ -123,7 +110,6 @@ async function loadAccountData() {
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
toast(err.message || 'Fehler beim Laden', false);
|
||||
dialog.close();
|
||||
} finally {
|
||||
state.loading = false;
|
||||
}
|
||||
@@ -343,10 +329,6 @@ function copyToClipboard(value) {
|
||||
}
|
||||
}
|
||||
|
||||
function closeUserDialog() {
|
||||
dialog?.close();
|
||||
}
|
||||
|
||||
function escapeHtml(str) {
|
||||
return String(str || '')
|
||||
.replace(/&/g, '&')
|
||||
|
||||
Reference in New Issue
Block a user