feat(F-095): completed feature

This commit is contained in:
chattie
2026-08-20 22:05:54 +02:00
parent 418bdca7f4
commit 4b60db026b
11 changed files with 272 additions and 77 deletions

View File

@@ -34,17 +34,31 @@ export function ImagesSection({ productId }: ImagesSectionProps) {
if (!url.trim()) return;
setSavingUrl(true);
try {
const p = await productsApi.update(productId, {});
// Attach via images array — for now use the attach endpoint
await fetch(`/api/products/${productId}/images`, {
const uploadResponse = await fetch('/api/upload', {
method: 'POST',
headers: { 'Content-Type': 'application/json', credentials: 'include' },
body: JSON.stringify({ url: url.trim(), altText: '', role: 'gallery' }),
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ url: url.trim() }),
});
const uploadData = await uploadResponse.json().catch(() => ({})) as { url?: string; error?: string };
if (!uploadResponse.ok || !uploadData.url) {
throw new Error(uploadData.error ?? 'No se pudo descargar la imagen');
}
const attachResponse = await fetch(`/api/products/${productId}/images`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ url: uploadData.url, altText: '', role: 'gallery' }),
});
if (!attachResponse.ok) {
const attachData = await attachResponse.json().catch(() => ({})) as { message?: string; error?: string };
throw new Error(attachData.message ?? attachData.error ?? 'No se pudo adjuntar la imagen');
}
setUrlInput('');
load();
} catch {
setError('Error al añadir imagen');
} catch (cause) {
setError(cause instanceof Error ? cause.message : 'Error al añadir imagen');
} finally {
setSavingUrl(false);
}