28 lines
1.1 KiB
JavaScript
28 lines
1.1 KiB
JavaScript
// Every page imports this first: it requires a signed-in user and fills the sidebar.
|
|
// `user` is the current user (the module waits for it, so importers can use it at once).
|
|
|
|
import { logout, redirectToLogin } from './auth.js';
|
|
import { api, h, userName } from './common.js';
|
|
|
|
const ROLES = { trainer: 'Тренер', trainee: 'Тренирующийся' };
|
|
|
|
export const user = await api('/auth/me/');
|
|
|
|
/** A training can be changed by its trainee (a trainer only sees it) and by a superuser */
|
|
export function canEditTraining(traineeId) {
|
|
return user.is_superuser || user.id === traineeId;
|
|
}
|
|
|
|
for (const element of document.querySelectorAll('[data-superuser-only]')) {
|
|
element.hidden = !user.is_superuser;
|
|
}
|
|
|
|
const signOut = h('button', { type: 'button', class: 'btn btn-sm btn-outline-light mt-2' }, 'Выйти');
|
|
signOut.addEventListener('click', async () => {
|
|
await logout();
|
|
await redirectToLogin();
|
|
});
|
|
document.getElementById('user-box')?.replaceChildren(
|
|
h('div', { class: 'text-white text-truncate' }, userName(user)),
|
|
h('div', {}, ROLES[user.role] + (user.is_superuser ? ', администратор' : '')),
|
|
signOut);
|