Files
orion/static/js/vendor/dashboard.js

113 lines
3.9 KiB
JavaScript

// Vendor Dashboard Component
function vendorDashboard() {
return {
currentUser: {},
vendor: null,
vendorRole: '',
currentSection: 'dashboard',
loading: false,
stats: {
products_count: 0,
orders_count: 0,
customers_count: 0,
revenue: 0
},
init() {
if (!this.checkAuth()) {
return;
}
this.loadDashboard();
},
checkAuth() {
const token = localStorage.getItem('vendor_token');
const user = localStorage.getItem('vendor_user');
const vendorContext = localStorage.getItem('vendor_context');
const vendorRole = localStorage.getItem('vendor_role');
if (!token || !user || !vendorContext) {
// Get vendor code from URL
const vendorCode = this.getVendorCodeFromUrl();
const redirectUrl = vendorCode ?
`/vendor/${vendorCode}/login` :
'/static/vendor/login.html';
window.location.href = redirectUrl;
return false;
}
try {
this.currentUser = JSON.parse(user);
this.vendor = JSON.parse(vendorContext);
this.vendorRole = vendorRole || 'Member';
return true;
} catch (e) {
console.error('Error parsing stored data:', e);
localStorage.removeItem('vendor_token');
localStorage.removeItem('vendor_user');
localStorage.removeItem('vendor_context');
localStorage.removeItem('vendor_role');
window.location.href = '/static/vendor/login.html';
return false;
}
},
getVendorCodeFromUrl() {
// Try to get vendor code from URL path
const pathParts = window.location.pathname.split('/').filter(p => p);
const vendorIndex = pathParts.indexOf('vendor');
if (vendorIndex !== -1 && pathParts[vendorIndex + 1]) {
const code = pathParts[vendorIndex + 1];
if (!['login', 'dashboard', 'admin', 'products', 'orders'].includes(code.toLowerCase())) {
return code.toUpperCase();
}
}
// Fallback to query parameter
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('vendor');
},
async handleLogout() {
const confirmed = await Utils.confirm(
'Are you sure you want to logout?',
'Confirm Logout'
);
if (confirmed) {
localStorage.removeItem('vendor_token');
localStorage.removeItem('vendor_user');
localStorage.removeItem('vendor_context');
localStorage.removeItem('vendor_role');
Utils.showToast('Logged out successfully', 'success', 2000);
setTimeout(() => {
window.location.href = `/vendor/${this.vendor.vendor_code}/login`;
}, 500);
}
},
async loadDashboard() {
this.loading = true;
try {
// In future slices, load actual dashboard data
// const data = await apiClient.get(`/vendor/dashboard/stats`);
// this.stats = data;
// For now, show placeholder data
this.stats = {
products_count: 0,
orders_count: 0,
customers_count: 0,
revenue: 0
};
} catch (error) {
console.error('Failed to load dashboard:', error);
Utils.showToast('Failed to load dashboard data', 'error');
} finally {
this.loading = false;
}
}
}
}