chore: fix create product
This commit is contained in:
@@ -260,6 +260,8 @@ export default function ProductsScreen() {
|
|||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
// Submit
|
// Submit
|
||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
|
// Dans ProductsScreen.tsx, remplace le bloc handleSubmit par celui-ci :
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
const err = validate();
|
const err = validate();
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -301,17 +303,23 @@ export default function ProductsScreen() {
|
|||||||
});
|
});
|
||||||
productId = editingProduct.id;
|
productId = editingProduct.id;
|
||||||
} else {
|
} else {
|
||||||
// Create product
|
// ✅ FIX : Création avec FormData au bon format pour le backend Go
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
fd.append("name", form.name.trim());
|
fd.append("name", form.name.trim());
|
||||||
fd.append("category", form.category);
|
fd.append("category", form.category);
|
||||||
fd.append("description", form.description.trim());
|
fd.append("description", form.description.trim());
|
||||||
fd.append("stock", form.stock);
|
fd.append("stock", form.stock);
|
||||||
fd.append("prices", JSON.stringify(prices));
|
|
||||||
|
|
||||||
// Attach pending media directly to creation FormData
|
// ✅ FIX PRINCIPAL : Envoyer les prix au format que Go attend
|
||||||
pendingMedia.forEach((m, i) => {
|
// Backend attend : prices[0][quantity], prices[0][price], etc.
|
||||||
fd.append("files", {
|
prices.forEach((p, i) => {
|
||||||
|
fd.append(`prices[${i}][quantity]`, String(p.quantity));
|
||||||
|
fd.append(`prices[${i}][price]`, String(p.price));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Attacher les médias en attente
|
||||||
|
pendingMedia.forEach((m) => {
|
||||||
|
fd.append("media", {
|
||||||
uri: m.uri,
|
uri: m.uri,
|
||||||
name: m.fileName,
|
name: m.fileName,
|
||||||
type: m.mimeType,
|
type: m.mimeType,
|
||||||
@@ -322,7 +330,7 @@ export default function ProductsScreen() {
|
|||||||
productId = createRes.data?.id;
|
productId = createRes.data?.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3) Upload new media for existing product edits
|
// 3) Upload new media pour les produits existants (mode edit)
|
||||||
if (editingProduct && pendingMedia.length > 0) {
|
if (editingProduct && pendingMedia.length > 0) {
|
||||||
setUploadingMedia(true);
|
setUploadingMedia(true);
|
||||||
for (const m of pendingMedia) {
|
for (const m of pendingMedia) {
|
||||||
@@ -354,7 +362,6 @@ export default function ProductsScreen() {
|
|||||||
setUploadingMedia(false);
|
setUploadingMedia(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
// Delete product
|
// Delete product
|
||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user