feat: add media library picker for product images

- Add admin media API endpoints for vendor media management
- Create reusable media_picker_modal macro in modals.html
- Create mediaPickerMixin Alpine.js helper for media selection
- Update product create/edit forms with media picker UI
- Support main image + additional images selection
- Add upload functionality within the picker modal
- Update vendor_product_service to handle additional_images
- Add additional_images field to Pydantic schemas

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-08 02:16:55 +01:00
parent 5e188cd253
commit 5271ecb378
10 changed files with 1207 additions and 42 deletions

View File

@@ -24,6 +24,9 @@ function adminVendorProductCreate() {
// Inherit base layout state
...data(),
// Include media picker functionality (vendor ID getter will be bound via loadMediaLibrary override)
...mediaPickerMixin(() => null, false),
// Set page identifier
currentPage: 'vendor-products',
@@ -53,8 +56,9 @@ function adminVendorProductCreate() {
currency: 'EUR',
tax_rate_percent: 17,
availability: '',
// Image
// Images
primary_image_url: '',
additional_images: [],
// Status
is_active: true,
is_featured: false,
@@ -204,8 +208,10 @@ function adminVendorProductCreate() {
tax_rate_percent: this.form.tax_rate_percent !== null
? parseInt(this.form.tax_rate_percent) : 17,
availability: this.form.availability || null,
// Image
// Images
primary_image_url: this.form.primary_image_url?.trim() || null,
additional_images: this.form.additional_images?.length > 0
? this.form.additional_images : null,
// Status
is_active: this.form.is_active,
is_featured: this.form.is_featured,
@@ -230,6 +236,168 @@ function adminVendorProductCreate() {
} finally {
this.saving = false;
}
},
// === Media Picker Overrides ===
// These override the mixin methods to use proper form context
/**
* Load media library for the selected vendor
*/
async loadMediaLibrary() {
const vendorId = this.form?.vendor_id;
if (!vendorId) {
adminVendorProductCreateLog.warn('Media picker: No vendor ID selected');
return;
}
this.mediaPickerState.loading = true;
this.mediaPickerState.skip = 0;
try {
const params = new URLSearchParams({
skip: '0',
limit: this.mediaPickerState.limit.toString(),
media_type: 'image',
});
if (this.mediaPickerState.search) {
params.append('search', this.mediaPickerState.search);
}
const response = await apiClient.get(
`/admin/media/vendors/${vendorId}?${params.toString()}`
);
this.mediaPickerState.media = response.media || [];
this.mediaPickerState.total = response.total || 0;
} catch (error) {
adminVendorProductCreateLog.error('Failed to load media library:', error);
Utils.showToast('Failed to load media library', 'error');
} finally {
this.mediaPickerState.loading = false;
}
},
/**
* Load more media (pagination)
*/
async loadMoreMedia() {
const vendorId = this.form?.vendor_id;
if (!vendorId) return;
this.mediaPickerState.loading = true;
this.mediaPickerState.skip += this.mediaPickerState.limit;
try {
const params = new URLSearchParams({
skip: this.mediaPickerState.skip.toString(),
limit: this.mediaPickerState.limit.toString(),
media_type: 'image',
});
if (this.mediaPickerState.search) {
params.append('search', this.mediaPickerState.search);
}
const response = await apiClient.get(
`/admin/media/vendors/${vendorId}?${params.toString()}`
);
this.mediaPickerState.media = [
...this.mediaPickerState.media,
...(response.media || [])
];
} catch (error) {
adminVendorProductCreateLog.error('Failed to load more media:', error);
} finally {
this.mediaPickerState.loading = false;
}
},
/**
* Upload a new media file
*/
async uploadMediaFile(event) {
const file = event.target.files?.[0];
if (!file) return;
const vendorId = this.form?.vendor_id;
if (!vendorId) {
Utils.showToast('Please select a vendor first', 'error');
return;
}
if (!file.type.startsWith('image/')) {
Utils.showToast('Please select an image file', 'error');
return;
}
if (file.size > 10 * 1024 * 1024) {
Utils.showToast('Image must be less than 10MB', 'error');
return;
}
this.mediaPickerState.uploading = true;
try {
const formData = new FormData();
formData.append('file', file);
const response = await apiClient.postFormData(
`/admin/media/vendors/${vendorId}/upload?folder=products`,
formData
);
if (response.success && response.media) {
this.mediaPickerState.media.unshift(response.media);
this.mediaPickerState.total++;
this.toggleMediaSelection(response.media);
Utils.showToast('Image uploaded successfully', 'success');
}
} catch (error) {
adminVendorProductCreateLog.error('Failed to upload image:', error);
Utils.showToast(error.message || 'Failed to upload image', 'error');
} finally {
this.mediaPickerState.uploading = false;
event.target.value = '';
}
},
/**
* Set main image from media picker
*/
setMainImage(media) {
this.form.primary_image_url = media.url;
adminVendorProductCreateLog.info('Main image set:', media.url);
},
/**
* Add additional images from media picker
*/
addAdditionalImages(mediaList) {
const newUrls = mediaList.map(m => m.url);
this.form.additional_images = [
...this.form.additional_images,
...newUrls
];
adminVendorProductCreateLog.info('Additional images added:', newUrls);
},
/**
* Remove an additional image by index
*/
removeAdditionalImage(index) {
this.form.additional_images.splice(index, 1);
},
/**
* Clear the main image
*/
clearMainImage() {
this.form.primary_image_url = '';
}
};
}

View File

@@ -28,6 +28,9 @@ function adminVendorProductEdit() {
// Inherit base layout state
...data(),
// Include media picker functionality (vendor ID comes from loaded product)
...mediaPickerMixin(() => null, false),
// Set page identifier
currentPage: 'vendor-products',
@@ -60,8 +63,9 @@ function adminVendorProductEdit() {
currency: 'EUR',
tax_rate_percent: 17,
availability: '',
// Image
// Images
primary_image_url: '',
additional_images: [],
// Product type & status
is_digital: false,
is_active: true,
@@ -127,8 +131,9 @@ function adminVendorProductEdit() {
currency: response.currency || 'EUR',
tax_rate_percent: response.tax_rate_percent ?? 17,
availability: response.availability || '',
// Image
// Images
primary_image_url: response.primary_image_url || '',
additional_images: response.additional_images || [],
// Product type & status
is_digital: response.is_digital ?? false,
is_active: response.is_active ?? true,
@@ -241,8 +246,10 @@ function adminVendorProductEdit() {
tax_rate_percent: this.form.tax_rate_percent !== null && this.form.tax_rate_percent !== ''
? parseInt(this.form.tax_rate_percent) : null,
availability: this.form.availability || null,
// Image
// Images
primary_image_url: this.form.primary_image_url?.trim() || null,
additional_images: this.form.additional_images?.length > 0
? this.form.additional_images : null,
// Status
is_digital: this.form.is_digital,
is_active: this.form.is_active,
@@ -271,6 +278,168 @@ function adminVendorProductEdit() {
} finally {
this.saving = false;
}
},
// === Media Picker Overrides ===
// These override the mixin methods to use proper form/product context
/**
* Load media library for the product's vendor
*/
async loadMediaLibrary() {
const vendorId = this.product?.vendor_id;
if (!vendorId) {
adminVendorProductEditLog.warn('Media picker: No vendor ID available');
return;
}
this.mediaPickerState.loading = true;
this.mediaPickerState.skip = 0;
try {
const params = new URLSearchParams({
skip: '0',
limit: this.mediaPickerState.limit.toString(),
media_type: 'image',
});
if (this.mediaPickerState.search) {
params.append('search', this.mediaPickerState.search);
}
const response = await apiClient.get(
`/admin/media/vendors/${vendorId}?${params.toString()}`
);
this.mediaPickerState.media = response.media || [];
this.mediaPickerState.total = response.total || 0;
} catch (error) {
adminVendorProductEditLog.error('Failed to load media library:', error);
Utils.showToast('Failed to load media library', 'error');
} finally {
this.mediaPickerState.loading = false;
}
},
/**
* Load more media (pagination)
*/
async loadMoreMedia() {
const vendorId = this.product?.vendor_id;
if (!vendorId) return;
this.mediaPickerState.loading = true;
this.mediaPickerState.skip += this.mediaPickerState.limit;
try {
const params = new URLSearchParams({
skip: this.mediaPickerState.skip.toString(),
limit: this.mediaPickerState.limit.toString(),
media_type: 'image',
});
if (this.mediaPickerState.search) {
params.append('search', this.mediaPickerState.search);
}
const response = await apiClient.get(
`/admin/media/vendors/${vendorId}?${params.toString()}`
);
this.mediaPickerState.media = [
...this.mediaPickerState.media,
...(response.media || [])
];
} catch (error) {
adminVendorProductEditLog.error('Failed to load more media:', error);
} finally {
this.mediaPickerState.loading = false;
}
},
/**
* Upload a new media file
*/
async uploadMediaFile(event) {
const file = event.target.files?.[0];
if (!file) return;
const vendorId = this.product?.vendor_id;
if (!vendorId) {
Utils.showToast('No vendor associated with this product', 'error');
return;
}
if (!file.type.startsWith('image/')) {
Utils.showToast('Please select an image file', 'error');
return;
}
if (file.size > 10 * 1024 * 1024) {
Utils.showToast('Image must be less than 10MB', 'error');
return;
}
this.mediaPickerState.uploading = true;
try {
const formData = new FormData();
formData.append('file', file);
const response = await apiClient.postFormData(
`/admin/media/vendors/${vendorId}/upload?folder=products`,
formData
);
if (response.success && response.media) {
this.mediaPickerState.media.unshift(response.media);
this.mediaPickerState.total++;
this.toggleMediaSelection(response.media);
Utils.showToast('Image uploaded successfully', 'success');
}
} catch (error) {
adminVendorProductEditLog.error('Failed to upload image:', error);
Utils.showToast(error.message || 'Failed to upload image', 'error');
} finally {
this.mediaPickerState.uploading = false;
event.target.value = '';
}
},
/**
* Set main image from media picker
*/
setMainImage(media) {
this.form.primary_image_url = media.url;
adminVendorProductEditLog.info('Main image set:', media.url);
},
/**
* Add additional images from media picker
*/
addAdditionalImages(mediaList) {
const newUrls = mediaList.map(m => m.url);
this.form.additional_images = [
...this.form.additional_images,
...newUrls
];
adminVendorProductEditLog.info('Additional images added:', newUrls);
},
/**
* Remove an additional image by index
*/
removeAdditionalImage(index) {
this.form.additional_images.splice(index, 1);
},
/**
* Clear the main image
*/
clearMainImage() {
this.form.primary_image_url = '';
}
};
}