Files
orion/static/admin/js/platform-homepage.js
Samir Boulahtit 265c71f597 fix: resolve all JS architecture violations (JS-005 through JS-009)
Fixed 89 violations across vendor, admin, and shared JavaScript files:

JS-008 (raw fetch → apiClient):
- Added postFormData() and getBlob() methods to api-client.js
- Updated inventory.js, messages.js to use apiClient.postFormData()
- Added noqa for file downloads that need response headers

JS-009 (window.showToast → Utils.showToast):
- Updated admin/messages.js, notifications.js, vendor/messages.js
- Replaced alert() in customers.js

JS-006 (async error handling):
- Added try/catch to all async init() and reload() methods
- Fixed vendor: billing, dashboard, login, messages, onboarding
- Fixed shared: feature-store, upgrade-prompts
- Fixed admin: all page components

JS-005 (init guards):
- Added initialization guards to prevent duplicate init() calls
- Pattern: if (window._componentInitialized) return;

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 21:32:19 +01:00

156 lines
6.2 KiB
JavaScript

// noqa: js-006 - async init pattern is safe, loadData has try/catch
// static/admin/js/platform-homepage.js
// Use centralized logger
const platformHomepageLog = window.LogConfig.loggers.platformHomepage || window.LogConfig.createLogger('platformHomepage');
// ============================================
// PLATFORM HOMEPAGE MANAGER FUNCTION
// ============================================
function platformHomepageManager() {
return {
// Inherit base layout functionality from init-alpine.js
...data(),
// Page identifier for sidebar active state
currentPage: 'platform-homepage',
// Platform homepage specific state
page: null,
loading: false,
saving: false,
error: null,
successMessage: null,
// Initialize
async init() {
platformHomepageLog.info('=== PLATFORM HOMEPAGE MANAGER INITIALIZING ===');
// Prevent multiple initializations
if (window._platformHomepageInitialized) {
platformHomepageLog.warn('Platform homepage manager already initialized, skipping...');
return;
}
window._platformHomepageInitialized = true;
platformHomepageLog.group('Loading platform homepage');
await this.loadPlatformHomepage();
platformHomepageLog.groupEnd();
platformHomepageLog.info('=== PLATFORM HOMEPAGE MANAGER INITIALIZATION COMPLETE ===');
},
// Load platform homepage from API
async loadPlatformHomepage() {
this.loading = true;
this.error = null;
try {
platformHomepageLog.info('Fetching platform homepage...');
// Fetch all platform pages
const response = await apiClient.get('/admin/content-pages/platform?include_unpublished=true');
platformHomepageLog.debug('API Response:', response);
if (!response) {
throw new Error('Invalid API response');
}
// Handle response - API returns array directly
const pages = Array.isArray(response) ? response : (response.data || response.items || []);
// Find the platform_homepage page
const homepage = pages.find(page => page.slug === 'platform_homepage');
if (!homepage) {
platformHomepageLog.warn('Platform homepage not found, creating default...');
// Initialize with default values
this.page = {
id: null,
slug: 'platform_homepage',
title: 'Welcome to Our Multi-Vendor Marketplace',
content: '<p>Connect vendors with customers worldwide. Build your online store and reach millions of shoppers.</p>',
template: 'default',
content_format: 'html',
meta_description: 'Leading multi-vendor marketplace platform. Connect with thousands of vendors and discover millions of products.',
meta_keywords: 'marketplace, multi-vendor, e-commerce, online shopping',
is_published: false,
show_in_header: false,
show_in_footer: false,
display_order: 0
};
} else {
this.page = { ...homepage };
platformHomepageLog.info('Platform homepage loaded:', this.page);
}
} catch (err) {
platformHomepageLog.error('Error loading platform homepage:', err);
this.error = err.message || 'Failed to load platform homepage';
} finally {
this.loading = false;
}
},
// Save platform homepage
async savePage() {
if (this.saving) return;
this.saving = true;
this.error = null;
this.successMessage = null;
try {
platformHomepageLog.info('Saving platform homepage...');
const payload = {
slug: 'platform_homepage',
title: this.page.title,
content: this.page.content,
content_format: this.page.content_format || 'html',
template: this.page.template,
meta_description: this.page.meta_description,
meta_keywords: this.page.meta_keywords,
is_published: this.page.is_published,
show_in_header: false, // Homepage never in header
show_in_footer: false, // Homepage never in footer
display_order: 0,
vendor_id: null // Platform default
};
platformHomepageLog.debug('Payload:', payload);
let response;
if (this.page.id) {
// Update existing page
response = await apiClient.put(`/admin/content-pages/${this.page.id}`, payload);
platformHomepageLog.info('Platform homepage updated');
} else {
// Create new page
response = await apiClient.post('/admin/content-pages/platform', payload);
platformHomepageLog.info('Platform homepage created');
}
if (response) {
// Handle response - API returns object directly
const pageData = response.data || response;
this.page = { ...pageData };
this.successMessage = 'Platform homepage saved successfully!';
// Clear success message after 3 seconds
setTimeout(() => {
this.successMessage = null;
}, 3000);
}
} catch (err) {
platformHomepageLog.error('Error saving platform homepage:', err);
this.error = err.message || 'Failed to save platform homepage';
} finally {
this.saving = false;
}
}
};
}