feat: enhance vendor product edit form with all mandatory fields

- Add translations support with language tabs (EN, FR, DE, LU)
- Add product identifiers: vendor SKU with auto-generate, brand, GTIN, GTIN type
- Add pricing fields: price (incl. VAT), sale price, currency, VAT rate
- Add primary image field with preview
- Add product status (active, featured) checkboxes
- Add optional supplier info section
- Pre-populate form with existing product data from API
- Add form validation (isFormValid method)
- Make is_digital read-only (derived from marketplace product)

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-25 12:58:00 +01:00
parent 63396ea6b6
commit b25d119899
4 changed files with 456 additions and 104 deletions

View File

@@ -1,7 +1,7 @@
// static/admin/js/vendor-product-edit.js
/**
* Admin vendor product edit page logic
* Edit vendor product information and overrides
* Edit vendor product information with translations
*/
const adminVendorProductEditLog = window.LogConfig.loggers.adminVendorProductEdit ||
@@ -16,6 +16,14 @@ function adminVendorProductEdit() {
const pathParts = window.location.pathname.split('/');
const productId = parseInt(pathParts[pathParts.length - 2]); // /vendor-products/{id}/edit
// Default translations structure
const defaultTranslations = () => ({
en: { title: '', description: '' },
fr: { title: '', description: '' },
de: { title: '', description: '' },
lu: { title: '', description: '' }
});
return {
// Inherit base layout state
...data(),
@@ -31,22 +39,35 @@ function adminVendorProductEdit() {
saving: false,
error: '',
// Product data
// Product data from API
product: null,
// Active language tab
activeLanguage: 'en',
// Form data
form: {
title: '',
brand: '',
// Translations by language
translations: defaultTranslations(),
// Product identifiers
vendor_sku: '',
brand: '',
gtin: '',
price_override: null,
currency_override: '',
availability: '',
gtin_type: 'ean13',
// Pricing
price: null,
sale_price: null,
currency: 'EUR',
tax_rate_percent: 17,
// Image
primary_image_url: '',
// Product type & status
is_digital: false,
is_active: true,
is_featured: false,
is_digital: false,
description: ''
// Optional supplier info
supplier: '',
cost: null
},
async init() {
@@ -76,22 +97,46 @@ function adminVendorProductEdit() {
const response = await apiClient.get(`/admin/vendor-products/${this.productId}`);
this.product = response;
adminVendorProductEditLog.info('Loaded product:', response);
// Populate translations from vendor_translations
const translations = defaultTranslations();
if (response.vendor_translations) {
for (const lang of ['en', 'fr', 'de', 'lu']) {
if (response.vendor_translations[lang]) {
translations[lang] = {
title: response.vendor_translations[lang].title || '',
description: response.vendor_translations[lang].description || ''
};
}
}
}
// Populate form with current values
this.form = {
title: response.title || '',
brand: response.brand || '',
translations: translations,
// Product identifiers
vendor_sku: response.vendor_sku || '',
brand: response.brand || '',
gtin: response.gtin || '',
price_override: response.price_override || null,
currency_override: response.currency_override || '',
availability: response.availability || '',
gtin_type: response.gtin_type || 'ean13',
// Pricing (convert cents to euros if stored as cents)
price: response.price || null,
sale_price: response.sale_price || null,
currency: response.currency || 'EUR',
tax_rate_percent: response.tax_rate_percent ?? 17,
// Image
primary_image_url: response.primary_image_url || '',
// Product type & status
is_digital: response.is_digital ?? false,
is_active: response.is_active ?? true,
is_featured: response.is_featured ?? false,
is_digital: response.is_digital ?? false,
description: response.description || ''
// Optional supplier info
supplier: response.supplier || '',
cost: response.cost || null
};
adminVendorProductEditLog.info('Loaded product:', this.product.id);
adminVendorProductEditLog.info('Form initialized:', this.form);
} catch (error) {
adminVendorProductEditLog.error('Failed to load product:', error);
this.error = error.message || 'Failed to load product details';
@@ -100,28 +145,112 @@ function adminVendorProductEdit() {
}
},
/**
* Check if form is valid (all mandatory fields filled)
*/
isFormValid() {
// English title and description are required
if (!this.form.translations.en.title?.trim()) return false;
if (!this.form.translations.en.description?.trim()) return false;
// Product identifiers
if (!this.form.vendor_sku?.trim()) return false;
if (!this.form.brand?.trim()) return false;
if (!this.form.gtin?.trim()) return false;
if (!this.form.gtin_type) return false;
// Pricing
if (this.form.price === null || this.form.price === '' || this.form.price < 0) return false;
if (!this.form.currency) return false;
if (this.form.tax_rate_percent === null || this.form.tax_rate_percent === '') return false;
// Image
if (!this.form.primary_image_url?.trim()) return false;
return true;
},
/**
* Generate a unique vendor SKU
* Format: XXXX_XXXX_XXXX (includes vendor_id for uniqueness)
*/
generateSku() {
const vendorId = this.product?.vendor_id || 0;
// Generate random alphanumeric segments
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const generateSegment = (length) => {
let result = '';
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
};
// First segment includes vendor ID (padded)
const vendorSegment = vendorId.toString().padStart(4, '0').slice(-4);
// Generate SKU: VID + random + random
const sku = `${vendorSegment}_${generateSegment(4)}_${generateSegment(4)}`;
this.form.vendor_sku = sku;
adminVendorProductEditLog.info('Generated SKU:', sku);
},
/**
* Save product changes
*/
async saveProduct() {
if (!this.isFormValid()) {
Utils.showToast('Please fill in all required fields', 'error');
return;
}
this.saving = true;
try {
// Build translations object for API
const translations = {};
for (const lang of ['en', 'fr', 'de', 'lu']) {
const t = this.form.translations[lang];
// Only include if there's actual content
if (t.title?.trim() || t.description?.trim()) {
translations[lang] = {
title: t.title?.trim() || null,
description: t.description?.trim() || null
};
}
}
// Build update payload
const payload = {
title: this.form.title || null,
brand: this.form.brand || null,
vendor_sku: this.form.vendor_sku || null,
gtin: this.form.gtin || null,
price_override: this.form.price_override ? parseFloat(this.form.price_override) : null,
currency_override: this.form.currency_override || null,
availability: this.form.availability || null,
translations: Object.keys(translations).length > 0 ? translations : null,
// Product identifiers
vendor_sku: this.form.vendor_sku?.trim() || null,
brand: this.form.brand?.trim() || null,
gtin: this.form.gtin?.trim() || null,
gtin_type: this.form.gtin_type || null,
// Pricing
price: this.form.price !== null && this.form.price !== ''
? parseFloat(this.form.price) : null,
sale_price: this.form.sale_price !== null && this.form.sale_price !== ''
? parseFloat(this.form.sale_price) : null,
currency: this.form.currency || null,
tax_rate_percent: this.form.tax_rate_percent !== null && this.form.tax_rate_percent !== ''
? parseInt(this.form.tax_rate_percent) : null,
// Image
primary_image_url: this.form.primary_image_url?.trim() || null,
// Status (is_digital is derived from marketplace product, not editable)
is_active: this.form.is_active,
is_featured: this.form.is_featured,
is_digital: this.form.is_digital,
description: this.form.description || null
// Optional supplier info
supplier: this.form.supplier?.trim() || null,
cost: this.form.cost !== null && this.form.cost !== ''
? parseFloat(this.form.cost) : null
};
adminVendorProductEditLog.info('Saving payload:', payload);
await apiClient.patch(`/admin/vendor-products/${this.productId}`, payload);
adminVendorProductEditLog.info('Product saved:', this.productId);