File size: 1,578 Bytes
63fb39f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | // Shared utility functions
const formatDate = (dateString) => {
const options = { year: 'numeric', month: 'short', day: 'numeric' };
return new Date(dateString).toLocaleDateString(undefined, options);
};
const calculateProgress = (current, target) => {
return Math.min(Math.round((current / target) * 100), 100);
};
// Auth state management
let currentUser = null;
const setCurrentUser = (user) => {
currentUser = user;
localStorage.setItem('currentUser', JSON.stringify(user));
updateUIForUserRole();
};
const getCurrentUser = () => {
if (!currentUser && localStorage.getItem('currentUser')) {
currentUser = JSON.parse(localStorage.getItem('currentUser'));
}
return currentUser;
};
const logout = () => {
currentUser = null;
localStorage.removeItem('currentUser');
window.location.href = '/';
};
const updateUIForUserRole = () => {
const user = getCurrentUser();
if (!user) return;
// Show/hide elements based on role
document.querySelectorAll('[data-role="admin"]').forEach(el => {
el.style.display = user.role === 'admin' ? 'block' : 'none';
});
document.querySelectorAll('[data-role="manager"]').forEach(el => {
el.style.display = user.role === 'manager' ? 'block' : 'none';
});
document.querySelectorAll('[data-role="employee"]').forEach(el => {
el.style.display = user.role === 'employee' ? 'block' : 'none';
});
};
// Initialize app when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
updateUIForUserRole();
}); |