Fixing vendor dashboard area

This commit is contained in:
2025-11-21 23:15:25 +01:00
parent 5aff76a27e
commit 86f1e16ef2
38 changed files with 312 additions and 433 deletions

View File

@@ -3,8 +3,23 @@
* Vendor dashboard page logic
*/
// ✅ Use centralized logger
const vendorDashLog = window.LogConfig.loggers.dashboard;
console.log('[VENDOR DASHBOARD] Loading...');
console.log('[VENDOR DASHBOARD] data function exists?', typeof data);
function vendorDashboard() {
console.log('[VENDOR DASHBOARD] vendorDashboard() called');
console.log('[VENDOR DASHBOARD] data function exists inside?', typeof data);
return {
// ✅ Inherit base layout state (includes vendorCode, dark mode, menu states)
...data(),
// ✅ Set page identifier
currentPage: 'dashboard',
loading: false,
error: '',
stats: {
@@ -17,6 +32,18 @@ function vendorDashboard() {
recentProducts: [],
async init() {
// Guard against multiple initialization
if (window._vendorDashboardInitialized) {
return;
}
window._vendorDashboardInitialized = true;
// IMPORTANT: Call parent init first to set vendorCode from URL
const parentInit = data().init;
if (parentInit) {
await parentInit.call(this);
}
await this.loadDashboardData();
},
@@ -26,31 +53,40 @@ function vendorDashboard() {
try {
// Load stats
// NOTE: apiClient prepends /api/v1, and vendor context middleware handles vendor detection
// So we just call /vendor/dashboard/stats → becomes /api/v1/vendor/dashboard/stats
const statsResponse = await apiClient.get(
`/api/v1/vendors/${this.vendorCode}/stats`
`/vendor/dashboard/stats`
);
this.stats = statsResponse;
// Map API response to stats (similar to admin dashboard pattern)
this.stats = {
products_count: statsResponse.products?.total || 0,
orders_count: statsResponse.orders?.total || 0,
customers_count: statsResponse.customers?.total || 0,
revenue: statsResponse.revenue?.total || 0
};
// Load recent orders
const ordersResponse = await apiClient.get(
`/api/v1/vendors/${this.vendorCode}/orders?limit=5&sort=created_at:desc`
`/vendor/orders?limit=5&sort=created_at:desc`
);
this.recentOrders = ordersResponse.items || [];
// Load recent products
const productsResponse = await apiClient.get(
`/api/v1/vendors/${this.vendorCode}/products?limit=5&sort=created_at:desc`
`/vendor/products?limit=5&sort=created_at:desc`
);
this.recentProducts = productsResponse.items || [];
logInfo('Dashboard data loaded', {
vendorDashLog.info('Dashboard data loaded', {
stats: this.stats,
orders: this.recentOrders.length,
products: this.recentProducts.length
});
} catch (error) {
logError('Failed to load dashboard data', error);
vendorDashLog.error('Failed to load dashboard data', error);
this.error = 'Failed to load dashboard data. Please try refreshing the page.';
} finally {
this.loading = false;

View File

@@ -4,7 +4,13 @@
* Provides common data and methods for all vendor pages
*/
// ✅ Use centralized logger
const vendorLog = window.LogConfig.log;
console.log('[VENDOR INIT-ALPINE] Loading...');
function data() {
console.log('[VENDOR INIT-ALPINE] data() function called');
return {
dark: false,
isSideMenuOpen: false,
@@ -46,11 +52,12 @@ function data() {
if (!this.vendorCode) return;
try {
const response = await apiClient.get(`/api/v1/vendors/${this.vendorCode}`);
// apiClient prepends /api/v1, so /vendor/{code} → /api/v1/vendor/{code}
const response = await apiClient.get(`/vendor/${this.vendorCode}`);
this.vendor = response;
logDebug('Vendor info loaded', this.vendor);
vendorLog.debug('Vendor info loaded', this.vendor);
} catch (error) {
logError('Failed to load vendor info', error);
vendorLog.error('Failed to load vendor info', error);
}
},
@@ -90,13 +97,23 @@ function data() {
},
async handleLogout() {
console.log('🚪 Logging out vendor user...');
try {
await apiClient.post('/api/v1/vendor/auth/logout');
// Call logout API
await apiClient.post('/vendor/auth/logout');
console.log('✅ Logout API called successfully');
} catch (error) {
logError('Logout error', error);
console.error('⚠️ Logout API error (continuing anyway):', error);
} finally {
localStorage.removeItem('accessToken');
// Clear all tokens and data
console.log('🧹 Clearing tokens...');
localStorage.removeItem('vendor_token');
localStorage.removeItem('currentUser');
localStorage.removeItem('vendorCode');
localStorage.clear(); // Clear everything to be safe
console.log('🔄 Redirecting to login...');
window.location.href = `/vendor/${this.vendorCode}/login`;
}
}

View File

@@ -105,9 +105,11 @@ function vendorLogin() {
vendorLoginLog.info('Login successful!');
vendorLoginLog.debug('Storing authentication data...');
localStorage.setItem('accessToken', response.access_token);
// Store token with correct key that apiClient expects
localStorage.setItem('vendor_token', response.access_token);
localStorage.setItem('currentUser', JSON.stringify(response.user));
localStorage.setItem('vendorCode', this.vendorCode);
vendorLoginLog.debug('Token stored as vendor_token in localStorage');
this.success = 'Login successful! Redirecting...';
vendorLoginLog.info('Redirecting to vendor dashboard...');