// app/edit-item/[id]/actions.js 'use server'; import { supabase } from '@/lib/supabaseClient'; import { revalidatePath } from 'next/cache'; export async function updateItem(formData) { if (!supabase) { return { error: 'Supabase client is not initialized. Cannot update item.' }; } const id = formData.get('id'); if (!id) { return { error: 'Item ID is required for updating.' }; } // The picture_url is now received directly from the client // after client-side upload to Supabase Storage if a new image was selected const updatedItem = { title: formData.get('title'), type: formData.get('type'), rating: formData.get('rating') ? parseInt(formData.get('rating'), 10) : null, notes: formData.get('notes'), picture_url: formData.get('picture_url'), }; // Basic validation if (!updatedItem.title || !updatedItem.type) { return { error: 'Title and Type are required.' }; } if (updatedItem.rating !== null && (updatedItem.rating < 1 || updatedItem.rating > 5)) { return { error: 'Rating must be between 1 and 5.' }; } try { const { data, error } = await supabase .from('items') .update(updatedItem) .eq('id', id) .select(); // .select() to get the updated data back if (error) { console.error('Supabase update error:', error); return { error: `Failed to update item: ${error.message}` }; } console.log('Item updated successfully:', data); revalidatePath('/'); // Revalidate the homepage to show the updated item return { success: true, message: 'Item updated successfully!', updatedItem: data ? data[0] : null }; } catch (e) { console.error('Error in updateItem action:', e); return { error: 'An unexpected error occurred.' }; } }