// app/add-item/actions.js 'use server'; import { supabase } from '@/lib/supabaseClient'; // Assuming alias @ is configured for root import { revalidatePath } from 'next/cache'; import { redirect } from 'next/navigation'; export async function createItem(formData) { const BUCKET_NAME = 'item_images'; if (!supabase) { return { error: 'Supabase client is not initialized. Cannot create item.' }; } const pictureFile = formData.get('picture_file'); let pictureUrl = null; if (pictureFile && pictureFile.size > 0) { if (!supabase.storage) { return { error: 'Supabase storage client is not available.' }; } const fileName = `public/${Date.now()}-${pictureFile.name.replace(/[^a-zA-Z0-9.]/g, '_')}`; const { data: uploadData, error: uploadError } = await supabase.storage .from(BUCKET_NAME) .upload(fileName, pictureFile); if (uploadError) { console.error('Supabase storage upload error:', uploadError); return { error: `Failed to upload image: ${uploadError.message}` }; } const { data: publicUrlData } = supabase.storage .from(BUCKET_NAME) .getPublicUrl(fileName); if (!publicUrlData || !publicUrlData.publicUrl) { console.error('Supabase storage getPublicUrl error: No publicUrl found'); // Optionally, you might want to delete the uploaded file here if getting URL fails // await supabase.storage.from(BUCKET_NAME).remove([fileName]); return { error: 'Failed to get image public URL after upload.' }; } pictureUrl = publicUrlData.publicUrl; } else { // Handle case where no file is uploaded but form still has 'picture_url' field (e.g. from old version) // Or simply rely on pictureUrl being null if no file is chosen. const legacyPictureUrl = formData.get('picture_url'); if (legacyPictureUrl && typeof legacyPictureUrl === 'string' && legacyPictureUrl.startsWith('http')) { pictureUrl = legacyPictureUrl; // Keep old URL if provided and no new file } } const newItem = { title: formData.get('title'), type: formData.get('type'), rating: formData.get('rating') ? parseInt(formData.get('rating'), 10) : null, notes: formData.get('notes'), picture_url: pictureUrl, // created_at will be set by default in Supabase or can be added here if needed }; // Basic validation if (!newItem.title || !newItem.type) { return { error: 'Title and Type are required.' }; } if (newItem.rating !== null && (newItem.rating < 1 || newItem.rating > 5)) { return { error: 'Rating must be between 1 and 5.' }; } try { const { data, error } = await supabase .from('items') .insert([newItem]) .select(); // .select() to get the inserted data back if (error) { console.error('Supabase insert error:', error); return { error: `Failed to create item: ${error.message}` }; } console.log('Item created successfully:', data); revalidatePath('/'); // Revalidate the homepage to show the new item // No explicit redirect here, form can handle success message return { success: true, message: 'Item added successfully!', createdItem: data ? data[0] : null }; } catch (e) { console.error('Error in createItem action:', e); return { error: 'An unexpected error occurred.' }; } }