/**
* Модуль для загрузки и отображения профиля сотрудника
* Использует API для получения данных с корректными глобальными URL
*/
export class EmployeeProfile {
constructor(employeeId, options = {}) {
this.employeeId = employeeId;
this.apiUrl = `/api/v1/employees/${employeeId}/`;
this.postsApiUrl = '/api/v1/posts/';
// Селекторы контейнеров
this.avatarContainer = options.avatarContainer || '#employeeAvatar';
this.nameContainer = options.nameContainer || '#employeeName';
this.postsContainer = options.postsContainer || '#employeePosts';
}
/**
* Инициализация - загрузка и рендеринг
*/
async init() {
try {
// Загружаем данные сотрудника
const employee = await this.fetchEmployee();
if (employee) {
this.renderAvatar(employee);
this.renderName(employee);
}
// Загружаем посты сотрудника
await this.loadPosts();
} catch (error) {
console.error('Failed to load employee profile:', error);
}
}
/**
* Загрузка данных сотрудника через API
*/
async fetchEmployee() {
const response = await fetch(this.apiUrl);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
}
/**
* Рендеринг аватара
*/
renderAvatar(employee) {
const container = document.querySelector(this.avatarContainer);
if (!container) return;
const avatarHtml = this.createAvatarHtml(employee);
container.innerHTML = avatarHtml;
}
/**
* Создание HTML аватара
*/
createAvatarHtml(employee) {
if (employee.avatar) {
return `
`;
} else {
const initial = (employee.first_name || employee.last_name || '?').charAt(0).toUpperCase();
return `