refactor: migrate templates and static files to self-contained modules
Templates Migration: - Migrate admin templates to modules (tenancy, billing, monitoring, marketplace, etc.) - Migrate vendor templates to modules (tenancy, billing, orders, messaging, etc.) - Migrate storefront templates to modules (catalog, customers, orders, cart, checkout, cms) - Migrate public templates to modules (billing, marketplace, cms) - Keep shared templates in app/templates/ (base.html, errors/, partials/, macros/) - Migrate letzshop partials to marketplace module Static Files Migration: - Migrate admin JS to modules: tenancy (23 files), core (5 files), monitoring (1 file) - Migrate vendor JS to modules: tenancy (4 files), core (2 files) - Migrate shared JS: vendor-selector.js to core, media-picker.js to cms - Migrate storefront JS: storefront-layout.js to core - Keep framework JS in static/ (api-client, utils, money, icons, log-config, lib/) - Update all template references to use module_static paths Naming Consistency: - Rename static/platform/ to static/public/ - Rename app/templates/platform/ to app/templates/public/ - Update all extends and static references Documentation: - Update module-system.md with shared templates documentation - Update frontend-structure.md with new module JS organization Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
278
app/modules/tenancy/static/admin/js/companies.js
Normal file
278
app/modules/tenancy/static/admin/js/companies.js
Normal file
@@ -0,0 +1,278 @@
|
||||
// noqa: js-006 - async init pattern is safe, loadData has try/catch
|
||||
// static/admin/js/companies.js
|
||||
|
||||
// ✅ Use centralized logger
|
||||
const companiesLog = window.LogConfig.loggers.companies || window.LogConfig.createLogger('companies');
|
||||
|
||||
// ============================================
|
||||
// COMPANY LIST FUNCTION
|
||||
// ============================================
|
||||
function adminCompanies() {
|
||||
return {
|
||||
// Inherit base layout functionality
|
||||
...data(),
|
||||
|
||||
// Page identifier for sidebar active state
|
||||
currentPage: 'companies',
|
||||
|
||||
// Companies page specific state
|
||||
companies: [],
|
||||
stats: {
|
||||
total: 0,
|
||||
verified: 0,
|
||||
active: 0,
|
||||
totalVendors: 0
|
||||
},
|
||||
loading: false,
|
||||
error: null,
|
||||
|
||||
// Search and filters
|
||||
filters: {
|
||||
search: '',
|
||||
is_active: '',
|
||||
is_verified: ''
|
||||
},
|
||||
|
||||
// Pagination state
|
||||
pagination: {
|
||||
page: 1,
|
||||
per_page: 20,
|
||||
total: 0,
|
||||
pages: 0
|
||||
},
|
||||
|
||||
// Initialize
|
||||
async init() {
|
||||
companiesLog.info('=== COMPANIES PAGE INITIALIZING ===');
|
||||
|
||||
// Prevent multiple initializations
|
||||
if (window._companiesInitialized) {
|
||||
companiesLog.warn('Companies page already initialized, skipping...');
|
||||
return;
|
||||
}
|
||||
window._companiesInitialized = true;
|
||||
|
||||
// Load platform settings for rows per page
|
||||
if (window.PlatformSettings) {
|
||||
this.pagination.per_page = await window.PlatformSettings.getRowsPerPage();
|
||||
}
|
||||
|
||||
companiesLog.group('Loading companies data');
|
||||
await this.loadCompanies();
|
||||
companiesLog.groupEnd();
|
||||
|
||||
companiesLog.info('=== COMPANIES PAGE INITIALIZATION COMPLETE ===');
|
||||
},
|
||||
|
||||
// Debounced search
|
||||
debouncedSearch() {
|
||||
if (this._searchTimeout) {
|
||||
clearTimeout(this._searchTimeout);
|
||||
}
|
||||
this._searchTimeout = setTimeout(() => {
|
||||
companiesLog.info('Search triggered:', this.filters.search);
|
||||
this.pagination.page = 1;
|
||||
this.loadCompanies();
|
||||
}, 300);
|
||||
},
|
||||
|
||||
// Computed: Get companies for current page (already paginated from server)
|
||||
get paginatedCompanies() {
|
||||
return this.companies;
|
||||
},
|
||||
|
||||
// Computed: Total number of pages
|
||||
get totalPages() {
|
||||
return this.pagination.pages;
|
||||
},
|
||||
|
||||
// Computed: Start index for pagination display
|
||||
get startIndex() {
|
||||
if (this.pagination.total === 0) return 0;
|
||||
return (this.pagination.page - 1) * this.pagination.per_page + 1;
|
||||
},
|
||||
|
||||
// Computed: End index for pagination display
|
||||
get endIndex() {
|
||||
const end = this.pagination.page * this.pagination.per_page;
|
||||
return end > this.pagination.total ? this.pagination.total : end;
|
||||
},
|
||||
|
||||
// Computed: Generate page numbers array with ellipsis
|
||||
get pageNumbers() {
|
||||
const pages = [];
|
||||
const totalPages = this.totalPages;
|
||||
const current = this.pagination.page;
|
||||
|
||||
if (totalPages <= 7) {
|
||||
// Show all pages if 7 or fewer
|
||||
for (let i = 1; i <= totalPages; i++) {
|
||||
pages.push(i);
|
||||
}
|
||||
} else {
|
||||
// Always show first page
|
||||
pages.push(1);
|
||||
|
||||
if (current > 3) {
|
||||
pages.push('...');
|
||||
}
|
||||
|
||||
// Show pages around current page
|
||||
const start = Math.max(2, current - 1);
|
||||
const end = Math.min(totalPages - 1, current + 1);
|
||||
|
||||
for (let i = start; i <= end; i++) {
|
||||
pages.push(i);
|
||||
}
|
||||
|
||||
if (current < totalPages - 2) {
|
||||
pages.push('...');
|
||||
}
|
||||
|
||||
// Always show last page
|
||||
pages.push(totalPages);
|
||||
}
|
||||
|
||||
return pages;
|
||||
},
|
||||
|
||||
// Load companies with search and pagination
|
||||
async loadCompanies() {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
companiesLog.info('Fetching companies from API...');
|
||||
|
||||
const params = new URLSearchParams();
|
||||
params.append('skip', (this.pagination.page - 1) * this.pagination.per_page);
|
||||
params.append('limit', this.pagination.per_page);
|
||||
|
||||
if (this.filters.search) {
|
||||
params.append('search', this.filters.search);
|
||||
}
|
||||
if (this.filters.is_active) {
|
||||
params.append('is_active', this.filters.is_active);
|
||||
}
|
||||
if (this.filters.is_verified) {
|
||||
params.append('is_verified', this.filters.is_verified);
|
||||
}
|
||||
|
||||
const response = await apiClient.get(`/admin/companies?${params}`);
|
||||
|
||||
if (response.companies) {
|
||||
this.companies = response.companies;
|
||||
this.pagination.total = response.total;
|
||||
this.pagination.pages = Math.ceil(response.total / this.pagination.per_page);
|
||||
|
||||
// Calculate stats from all companies (need separate call for accurate stats)
|
||||
this.stats.total = response.total;
|
||||
this.stats.verified = this.companies.filter(c => c.is_verified).length;
|
||||
this.stats.active = this.companies.filter(c => c.is_active).length;
|
||||
this.stats.totalVendors = this.companies.reduce((sum, c) => sum + (c.vendor_count || 0), 0);
|
||||
|
||||
companiesLog.info(`Loaded ${this.companies.length} companies (total: ${response.total})`);
|
||||
} else {
|
||||
companiesLog.warn('No companies in response');
|
||||
this.companies = [];
|
||||
}
|
||||
} catch (error) {
|
||||
companiesLog.error('Failed to load companies:', error);
|
||||
this.error = error.message || 'Failed to load companies';
|
||||
this.companies = [];
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
// Edit company
|
||||
editCompany(companyId) {
|
||||
companiesLog.info('Edit company:', companyId);
|
||||
// TODO: Navigate to edit page
|
||||
window.location.href = `/admin/companies/${companyId}/edit`;
|
||||
},
|
||||
|
||||
// Delete company
|
||||
async deleteCompany(company) {
|
||||
if (company.vendor_count > 0) {
|
||||
companiesLog.warn('Cannot delete company with vendors');
|
||||
Utils.showToast(`Cannot delete "${company.name}" because it has ${company.vendor_count} vendor(s). Please delete or reassign the vendors first.`, 'warning');
|
||||
return;
|
||||
}
|
||||
|
||||
const confirmed = confirm(
|
||||
`Are you sure you want to delete "${company.name}"?\n\nThis action cannot be undone.`
|
||||
);
|
||||
|
||||
if (!confirmed) {
|
||||
companiesLog.info('Delete cancelled by user');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
companiesLog.info('Deleting company:', company.id);
|
||||
|
||||
await apiClient.delete(`/admin/companies/${company.id}?confirm=true`);
|
||||
|
||||
companiesLog.info('Company deleted successfully');
|
||||
|
||||
// Reload companies
|
||||
await this.loadCompanies();
|
||||
await this.loadStats();
|
||||
|
||||
Utils.showToast(`Company "${company.name}" deleted successfully`, 'success');
|
||||
} catch (error) {
|
||||
companiesLog.error('Failed to delete company:', error);
|
||||
Utils.showToast(`Failed to delete company: ${error.message}`, 'error');
|
||||
}
|
||||
},
|
||||
|
||||
// Pagination methods
|
||||
previousPage() {
|
||||
if (this.pagination.page > 1) {
|
||||
this.pagination.page--;
|
||||
companiesLog.info('Previous page:', this.pagination.page);
|
||||
this.loadCompanies();
|
||||
}
|
||||
},
|
||||
|
||||
nextPage() {
|
||||
if (this.pagination.page < this.totalPages) {
|
||||
this.pagination.page++;
|
||||
companiesLog.info('Next page:', this.pagination.page);
|
||||
this.loadCompanies();
|
||||
}
|
||||
},
|
||||
|
||||
goToPage(pageNum) {
|
||||
if (pageNum !== '...' && pageNum >= 1 && pageNum <= this.totalPages) {
|
||||
this.pagination.page = pageNum;
|
||||
companiesLog.info('Go to page:', this.pagination.page);
|
||||
this.loadCompanies();
|
||||
}
|
||||
},
|
||||
|
||||
// Format date for display
|
||||
formatDate(dateString) {
|
||||
if (!dateString) return 'N/A';
|
||||
try {
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleDateString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric'
|
||||
});
|
||||
} catch (e) {
|
||||
companiesLog.error('Date parsing error:', e);
|
||||
return dateString;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Register logger for configuration
|
||||
if (!window.LogConfig.loggers.companies) {
|
||||
window.LogConfig.loggers.companies = window.LogConfig.createLogger('companies');
|
||||
}
|
||||
|
||||
companiesLog.info('✅ Companies module loaded');
|
||||
Reference in New Issue
Block a user