migrating vendor frontend to new architecture
This commit is contained in:
@@ -1,5 +1,16 @@
|
||||
// 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,
|
||||
@@ -22,15 +33,27 @@ function adminUsers() {
|
||||
},
|
||||
|
||||
// Initialization
|
||||
init() {
|
||||
Logger.info('Users page initialized', 'USERS');
|
||||
this.loadUsers();
|
||||
this.loadStats();
|
||||
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,
|
||||
@@ -38,15 +61,24 @@ function adminUsers() {
|
||||
...this.filters
|
||||
});
|
||||
|
||||
const response = await ApiClient.get(`/admin/users?${params}`);
|
||||
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) {
|
||||
Logger.error('Failed to load users', 'USERS', error);
|
||||
window.LogConfig.logError(error, 'Load Users');
|
||||
Utils.showToast('Failed to load users', 'error');
|
||||
} finally {
|
||||
this.loading = false;
|
||||
@@ -55,18 +87,28 @@ function adminUsers() {
|
||||
|
||||
// Load statistics
|
||||
async loadStats() {
|
||||
usersLog.info('Loading user statistics...');
|
||||
|
||||
try {
|
||||
const response = await ApiClient.get('/admin/users/stats');
|
||||
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) {
|
||||
Logger.error('Failed to load stats', 'USERS', 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),
|
||||
@@ -75,6 +117,7 @@ function adminUsers() {
|
||||
nextPage() {
|
||||
if (this.pagination.page < this.pagination.pages) {
|
||||
this.pagination.page++;
|
||||
usersLog.info('Next page:', this.pagination.page);
|
||||
this.loadUsers();
|
||||
}
|
||||
},
|
||||
@@ -82,59 +125,80 @@ function adminUsers() {
|
||||
previousPage() {
|
||||
if (this.pagination.page > 1) {
|
||||
this.pagination.page--;
|
||||
usersLog.info('Previous page:', this.pagination.page);
|
||||
this.loadUsers();
|
||||
}
|
||||
},
|
||||
|
||||
// Actions
|
||||
viewUser(user) {
|
||||
Logger.info('View user', 'USERS', user);
|
||||
usersLog.info('View user:', user.username);
|
||||
// TODO: Open view modal
|
||||
},
|
||||
|
||||
editUser(user) {
|
||||
Logger.info('Edit user', 'USERS', 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 {
|
||||
await ApiClient.put(`/admin/users/${user.id}/status`, {
|
||||
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');
|
||||
this.loadUsers();
|
||||
this.loadStats();
|
||||
usersLog.info(`User ${action}d successfully`);
|
||||
|
||||
await this.loadUsers();
|
||||
await this.loadStats();
|
||||
} catch (error) {
|
||||
Logger.error(`Failed to ${action} user`, 'USERS', 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 {
|
||||
await ApiClient.delete(`/admin/users/${user.id}`);
|
||||
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');
|
||||
this.loadUsers();
|
||||
this.loadStats();
|
||||
usersLog.info('User deleted successfully');
|
||||
|
||||
await this.loadUsers();
|
||||
await this.loadStats();
|
||||
} catch (error) {
|
||||
Logger.error('Failed to delete user', 'USERS', error);
|
||||
window.LogConfig.logError(error, 'Delete User');
|
||||
Utils.showToast('Failed to delete user', 'error');
|
||||
}
|
||||
},
|
||||
|
||||
openCreateModal() {
|
||||
Logger.info('Open create user modal', 'USERS');
|
||||
usersLog.info('Open create user modal');
|
||||
// TODO: Open create modal
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
usersLog.info('Users module loaded');
|
||||
Reference in New Issue
Block a user