Files
orion/static/admin/js/users.js

140 lines
4.1 KiB
JavaScript

function adminUsers() {
return {
// 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
init() {
Logger.info('Users page initialized', 'USERS');
this.loadUsers();
this.loadStats();
},
// Load users from API
async loadUsers() {
this.loading = true;
try {
const params = new URLSearchParams({
page: this.pagination.page,
per_page: this.pagination.per_page,
...this.filters
});
const response = await ApiClient.get(`/admin/users?${params}`);
if (response.items) {
this.users = response.items;
this.pagination.total = response.total;
this.pagination.pages = response.pages;
}
} catch (error) {
Logger.error('Failed to load users', 'USERS', error);
Utils.showToast('Failed to load users', 'error');
} finally {
this.loading = false;
}
},
// Load statistics
async loadStats() {
try {
const response = await ApiClient.get('/admin/users/stats');
if (response) {
this.stats = response;
}
} catch (error) {
Logger.error('Failed to load stats', 'USERS', error);
}
},
// Search with debounce
debouncedSearch: Utils.debounce(function() {
this.pagination.page = 1;
this.loadUsers();
}, 500),
// Pagination
nextPage() {
if (this.pagination.page < this.pagination.pages) {
this.pagination.page++;
this.loadUsers();
}
},
previousPage() {
if (this.pagination.page > 1) {
this.pagination.page--;
this.loadUsers();
}
},
// Actions
viewUser(user) {
Logger.info('View user', 'USERS', user);
// TODO: Open view modal
},
editUser(user) {
Logger.info('Edit user', 'USERS', user);
// TODO: Open edit modal
},
async toggleUserStatus(user) {
const action = user.is_active ? 'deactivate' : 'activate';
if (!confirm(`Are you sure you want to ${action} ${user.username}?`)) {
return;
}
try {
await ApiClient.put(`/admin/users/${user.id}/status`, {
is_active: !user.is_active
});
Utils.showToast(`User ${action}d successfully`, 'success');
this.loadUsers();
this.loadStats();
} catch (error) {
Logger.error(`Failed to ${action} user`, 'USERS', error);
Utils.showToast(`Failed to ${action} user`, 'error');
}
},
async deleteUser(user) {
if (!confirm(`Are you sure you want to delete ${user.username}? This action cannot be undone.`)) {
return;
}
try {
await ApiClient.delete(`/admin/users/${user.id}`);
Utils.showToast('User deleted successfully', 'success');
this.loadUsers();
this.loadStats();
} catch (error) {
Logger.error('Failed to delete user', 'USERS', error);
Utils.showToast('Failed to delete user', 'error');
}
},
openCreateModal() {
Logger.info('Open create user modal', 'USERS');
// TODO: Open create modal
}
};
}