feat: auto-upload photo after item creation if image_processing provided
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
import toast from 'react-hot-toast';
|
||||
import { inventoryApi } from '@/lib/api';
|
||||
import { CropBounds } from './useCropHandles';
|
||||
|
||||
@@ -10,6 +11,12 @@ interface ItemFormData {
|
||||
barcode?: string;
|
||||
part_number?: string;
|
||||
box_label?: string;
|
||||
extractedImageBlob?: Blob;
|
||||
imageProcessing?: {
|
||||
crop_bounds?: { x: number; y: number; width: number; height: number };
|
||||
rotation_degrees?: number;
|
||||
confidence?: number;
|
||||
};
|
||||
}
|
||||
|
||||
interface UploadedPhoto {
|
||||
@@ -151,8 +158,11 @@ export function useItemCreate(): UseItemCreateReturn {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Extract image data if provided
|
||||
const { extractedImageBlob, imageProcessing, ...itemData } = formData;
|
||||
|
||||
// Create item first (without photo)
|
||||
const createdItem = await inventoryApi.createItem(userId, formData);
|
||||
const createdItem = await inventoryApi.createItem(userId, itemData);
|
||||
|
||||
if (!createdItem.id) {
|
||||
const errorMsg = 'Failed to create item';
|
||||
@@ -162,6 +172,33 @@ export function useItemCreate(): UseItemCreateReturn {
|
||||
}
|
||||
|
||||
setItemId(createdItem.id);
|
||||
|
||||
// AUTO-UPLOAD PHOTO if we have both extractedImageBlob and imageProcessing
|
||||
if (extractedImageBlob && imageProcessing && createdItem.id) {
|
||||
try {
|
||||
const formDataUpload = new FormData();
|
||||
formDataUpload.append('file', extractedImageBlob);
|
||||
|
||||
// If crop bounds are set, add them to the request
|
||||
if (imageProcessing.crop_bounds) {
|
||||
const cropBoundsStr = JSON.stringify(imageProcessing.crop_bounds);
|
||||
formDataUpload.append('crop_bounds', cropBoundsStr);
|
||||
}
|
||||
|
||||
await inventoryApi.uploadItemPhoto(createdItem.id, formDataUpload);
|
||||
|
||||
toast.success('Item created + photo saved');
|
||||
} catch (photoErr) {
|
||||
console.warn('Photo upload failed, but item created:', photoErr);
|
||||
toast.warning('Item created (photo upload skipped)');
|
||||
}
|
||||
} else if (extractedImageBlob || imageProcessing) {
|
||||
// Only one of the two is provided, so skip photo upload
|
||||
toast.success('Item created');
|
||||
} else {
|
||||
toast.success('Item created');
|
||||
}
|
||||
|
||||
setIsLoading(false);
|
||||
|
||||
return createdItem;
|
||||
|
||||
Reference in New Issue
Block a user