204 lines
6.7 KiB
JavaScript
204 lines
6.7 KiB
JavaScript
// static/admin/js/users.js
|
|
|
|
// ✅ Use centralized logger - ONE LINE!
|
|
const usersLog = window.LogConfig.loggers.users;
|
|
|
|
function adminUsers() {
|
|
return {
|
|
// ✅ Inherit base layout functionality
|
|
...data(),
|
|
|
|
// ✅ Set page identifier
|
|
currentPage: 'users',
|
|
|
|
// State
|
|
users: [],
|
|
loading: false,
|
|
filters: {
|
|
search: '',
|
|
role: '',
|
|
is_active: ''
|
|
},
|
|
stats: {
|
|
total: 0,
|
|
active: 0,
|
|
vendors: 0,
|
|
admins: 0
|
|
},
|
|
pagination: {
|
|
page: 1,
|
|
per_page: 10,
|
|
total: 0,
|
|
pages: 0
|
|
},
|
|
|
|
// Initialization
|
|
async init() {
|
|
usersLog.info('=== USERS PAGE INITIALIZING ===');
|
|
|
|
// Prevent multiple initializations
|
|
if (window._usersInitialized) {
|
|
usersLog.warn('Users page already initialized, skipping...');
|
|
return;
|
|
}
|
|
window._usersInitialized = true;
|
|
|
|
await this.loadUsers();
|
|
await this.loadStats();
|
|
|
|
usersLog.info('=== USERS PAGE INITIALIZATION COMPLETE ===');
|
|
},
|
|
|
|
// Load users from API
|
|
async loadUsers() {
|
|
usersLog.info('Loading users...');
|
|
this.loading = true;
|
|
|
|
try {
|
|
const params = new URLSearchParams({
|
|
page: this.pagination.page,
|
|
per_page: this.pagination.per_page,
|
|
...this.filters
|
|
});
|
|
|
|
const url = `/admin/users?${params}`;
|
|
window.LogConfig.logApiCall('GET', url, null, 'request');
|
|
|
|
const startTime = performance.now();
|
|
const response = await apiClient.get(url); // ✅ Fixed: lowercase apiClient
|
|
const duration = performance.now() - startTime;
|
|
|
|
window.LogConfig.logApiCall('GET', url, response, 'response');
|
|
window.LogConfig.logPerformance('Load Users', duration);
|
|
|
|
if (response.items) {
|
|
this.users = response.items;
|
|
this.pagination.total = response.total;
|
|
this.pagination.pages = response.pages;
|
|
usersLog.info(`Loaded ${this.users.length} users`);
|
|
}
|
|
} catch (error) {
|
|
window.LogConfig.logError(error, 'Load Users');
|
|
Utils.showToast('Failed to load users', 'error');
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
// Load statistics
|
|
async loadStats() {
|
|
usersLog.info('Loading user statistics...');
|
|
|
|
try {
|
|
const url = '/admin/users/stats';
|
|
window.LogConfig.logApiCall('GET', url, null, 'request');
|
|
|
|
const response = await apiClient.get(url); // ✅ Fixed: lowercase apiClient
|
|
|
|
window.LogConfig.logApiCall('GET', url, response, 'response');
|
|
|
|
if (response) {
|
|
this.stats = response;
|
|
usersLog.debug('Stats loaded:', this.stats);
|
|
}
|
|
} catch (error) {
|
|
window.LogConfig.logError(error, 'Load Stats');
|
|
}
|
|
},
|
|
|
|
// Search with debounce
|
|
debouncedSearch: Utils.debounce(function() {
|
|
usersLog.info('Search triggered:', this.filters.search);
|
|
this.pagination.page = 1;
|
|
this.loadUsers();
|
|
}, 500),
|
|
|
|
// Pagination
|
|
nextPage() {
|
|
if (this.pagination.page < this.pagination.pages) {
|
|
this.pagination.page++;
|
|
usersLog.info('Next page:', this.pagination.page);
|
|
this.loadUsers();
|
|
}
|
|
},
|
|
|
|
previousPage() {
|
|
if (this.pagination.page > 1) {
|
|
this.pagination.page--;
|
|
usersLog.info('Previous page:', this.pagination.page);
|
|
this.loadUsers();
|
|
}
|
|
},
|
|
|
|
// Actions
|
|
viewUser(user) {
|
|
usersLog.info('View user:', user.username);
|
|
// TODO: Open view modal
|
|
},
|
|
|
|
editUser(user) {
|
|
usersLog.info('Edit user:', user.username);
|
|
// TODO: Open edit modal
|
|
},
|
|
|
|
async toggleUserStatus(user) {
|
|
const action = user.is_active ? 'deactivate' : 'activate';
|
|
usersLog.info(`Toggle user status: ${action}`, user.username);
|
|
|
|
if (!confirm(`Are you sure you want to ${action} ${user.username}?`)) {
|
|
usersLog.info('Status toggle cancelled by user');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const url = `/admin/users/${user.id}/status`;
|
|
window.LogConfig.logApiCall('PUT', url, { is_active: !user.is_active }, 'request');
|
|
|
|
await apiClient.put(url, { // ✅ Fixed: lowercase apiClient
|
|
is_active: !user.is_active
|
|
});
|
|
|
|
Utils.showToast(`User ${action}d successfully`, 'success');
|
|
usersLog.info(`User ${action}d successfully`);
|
|
|
|
await this.loadUsers();
|
|
await this.loadStats();
|
|
} catch (error) {
|
|
window.LogConfig.logError(error, `Toggle User Status (${action})`);
|
|
Utils.showToast(`Failed to ${action} user`, 'error');
|
|
}
|
|
},
|
|
|
|
async deleteUser(user) {
|
|
usersLog.warn('Delete user requested:', user.username);
|
|
|
|
if (!confirm(`Are you sure you want to delete ${user.username}? This action cannot be undone.`)) {
|
|
usersLog.info('Delete cancelled by user');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const url = `/admin/users/${user.id}`;
|
|
window.LogConfig.logApiCall('DELETE', url, null, 'request');
|
|
|
|
await apiClient.delete(url); // ✅ Fixed: lowercase apiClient
|
|
|
|
Utils.showToast('User deleted successfully', 'success');
|
|
usersLog.info('User deleted successfully');
|
|
|
|
await this.loadUsers();
|
|
await this.loadStats();
|
|
} catch (error) {
|
|
window.LogConfig.logError(error, 'Delete User');
|
|
Utils.showToast('Failed to delete user', 'error');
|
|
}
|
|
},
|
|
|
|
openCreateModal() {
|
|
usersLog.info('Open create user modal');
|
|
// TODO: Open create modal
|
|
}
|
|
};
|
|
}
|
|
|
|
usersLog.info('Users module loaded'); |