Files
orion/app/modules/tenancy/static/admin/js/select-platform.js
Samir Boulahtit a77a8a3a98
All checks were successful
CI / ruff (push) Successful in 12s
CI / pytest (push) Successful in 50m57s
CI / validate (push) Successful in 24s
CI / dependency-scanning (push) Successful in 29s
CI / docs (push) Successful in 40s
CI / deploy (push) Successful in 51s
feat: multi-module improvements across merchant, store, i18n, and customer systems
- Fix platform-grouped merchant sidebar menu with core items at root level
- Add merchant store management (detail page, create store, team page)
- Fix store settings 500 error by removing dead stripe/API tab
- Move onboarding translations to module-owned locale files
- Fix onboarding banner i18n with server-side rendering + context inheritance
- Refactor login language selectors to use languageSelector() function (LANG-002)
- Move HTTPException handling to global exception handler in merchant routes (API-003)
- Add language selector to all login pages and portal headers
- Fix customer module: drop order stats from customer model, add to orders module
- Fix admin menu config visibility for super admin platform context
- Fix storefront auth and layout issues
- Add missing i18n translations for onboarding steps (en/fr/de/lb)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 23:48:25 +01:00

188 lines
7.0 KiB
JavaScript

// static/admin/js/select-platform.js
// Platform selection page for admins (platform admins and super admins)
const platformLog = window.LogConfig ? window.LogConfig.createLogger('PLATFORM_SELECT') : console;
function selectPlatform() {
return {
dark: false,
loading: true,
selecting: false,
error: null,
platforms: [],
isSuperAdmin: false,
currentPlatformId: null,
async init() {
platformLog.info('=== PLATFORM SELECTION PAGE INITIALIZING ===');
// Prevent multiple initializations
if (window._platformSelectInitialized) {
platformLog.warn('Platform selection page already initialized, skipping...');
return;
}
window._platformSelectInitialized = true;
// Set theme
this.dark = localStorage.getItem('theme') === 'dark';
// Check if user is logged in
const token = localStorage.getItem('admin_token');
if (!token) {
platformLog.warn('No token found, redirecting to login');
window.location.href = '/admin/login';
return;
}
// Load accessible platforms
await this.loadPlatforms();
},
async loadPlatforms() {
this.loading = true;
this.error = null;
try {
platformLog.info('Fetching accessible platforms...');
const response = await apiClient.get('/admin/auth/accessible-platforms');
platformLog.debug('Platforms response:', response);
this.isSuperAdmin = response.is_super_admin === true;
this.platforms = response.platforms || [];
this.currentPlatformId = response.current_platform_id || null;
if (!this.isSuperAdmin && !response.requires_platform_selection && this.platforms.length === 1) {
// Only one platform assigned, auto-select it
platformLog.info('Single platform assigned, auto-selecting...');
await this.choosePlatform(this.platforms[0]);
return;
}
platformLog.info(`Loaded ${this.platforms.length} platforms`);
} catch (error) {
platformLog.error('Failed to load platforms:', error);
if (error.message && error.message.includes('401')) {
// Token expired or invalid
window.location.href = '/admin/login';
return;
}
this.error = error.message || 'Failed to load platforms. Please try again.';
} finally {
this.loading = false;
}
},
isCurrentPlatform(platform) {
return this.currentPlatformId && this.currentPlatformId === platform.id;
},
async choosePlatform(platform) {
if (this.selecting) return;
this.selecting = true;
this.error = null;
platformLog.info(`Selecting platform: ${platform.code}`);
try {
const response = await apiClient.post(
`/admin/auth/select-platform?platform_id=${platform.id}`
);
platformLog.debug('Platform selection response:', response);
if (response.access_token) {
// Store new token with platform context
localStorage.setItem('admin_token', response.access_token);
localStorage.setItem('token', response.access_token);
// Store selected platform info
localStorage.setItem('admin_platform', JSON.stringify({
id: platform.id,
code: platform.code,
name: platform.name
}));
// Update user data if provided
if (response.user) {
localStorage.setItem('admin_user', JSON.stringify(response.user));
}
platformLog.info('Platform selected successfully, redirecting to dashboard...');
// Redirect to dashboard or last visited page
const lastPage = localStorage.getItem('admin_last_visited_page');
const redirectTo = (lastPage && lastPage.startsWith('/admin/') && !lastPage.includes('/login') && !lastPage.includes('/select-platform'))
? lastPage
: '/admin/dashboard';
window.location.href = redirectTo;
} else {
throw new Error('No token received from server');
}
} catch (error) {
platformLog.error('Platform selection failed:', error);
this.error = error.message || 'Failed to select platform. Please try again.';
this.selecting = false;
}
},
async deselectPlatform() {
if (this.selecting) return;
this.selecting = true;
this.error = null;
platformLog.info('Deselecting platform (returning to global mode)...');
try {
const response = await apiClient.post('/admin/auth/deselect-platform');
if (response.access_token) {
// Store new token without platform context
localStorage.setItem('admin_token', response.access_token);
localStorage.setItem('token', response.access_token);
// Remove platform info
localStorage.removeItem('admin_platform');
platformLog.info('Platform deselected, redirecting to dashboard...');
window.location.href = '/admin/dashboard';
} else {
throw new Error('No token received from server');
}
} catch (error) {
platformLog.error('Platform deselection failed:', error);
this.error = error.message || 'Failed to deselect platform. Please try again.';
this.selecting = false;
}
},
async logout() {
platformLog.info('Logging out...');
try {
await apiClient.post('/admin/auth/logout');
} catch (error) {
platformLog.error('Logout API error:', error);
} finally {
localStorage.removeItem('admin_token');
localStorage.removeItem('admin_user');
localStorage.removeItem('admin_platform');
localStorage.removeItem('token');
window.location.href = '/admin/login';
}
},
toggleDarkMode() {
this.dark = !this.dark;
localStorage.setItem('theme', this.dark ? 'dark' : 'light');
}
};
}
platformLog.info('Platform selection module loaded');