From 87a33ca5b13c2b29434ac27cb4a7d7b419ab5cb8 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Mon, 29 Sep 2025 02:38:06 +0000 Subject: [PATCH] feat: Update avatar upload logic --- src/components/settings/AccountProfileTab.tsx | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/components/settings/AccountProfileTab.tsx b/src/components/settings/AccountProfileTab.tsx index 60777fea..c4daba14 100644 --- a/src/components/settings/AccountProfileTab.tsx +++ b/src/components/settings/AccountProfileTab.tsx @@ -15,7 +15,7 @@ import { useToast } from '@/hooks/use-toast'; import { useAuth } from '@/hooks/useAuth'; import { supabase } from '@/integrations/supabase/client'; import { User, Upload, Trash2 } from 'lucide-react'; -import { SimplePhotoUpload } from './SimplePhotoUpload'; +import { PhotoUpload } from '@/components/upload/PhotoUpload'; const profileSchema = z.object({ username: z.string().min(3).max(30).regex(/^[a-zA-Z0-9_-]+$/), @@ -34,6 +34,8 @@ export function AccountProfileTab() { const [loading, setLoading] = useState(false); const [avatarLoading, setAvatarLoading] = useState(false); const [showDeleteDialog, setShowDeleteDialog] = useState(false); + const [avatarUrl, setAvatarUrl] = useState(profile?.avatar_url || ''); + const [avatarImageId, setAvatarImageId] = useState(profile?.avatar_image_id || ''); const form = useForm({ resolver: zodResolver(profileSchema), @@ -83,16 +85,22 @@ export function AccountProfileTab() { } }; - const handleAvatarUpload = async (imageId: string, imageUrl: string) => { - if (!user) return; + const handleAvatarUpload = async (urls: string[], imageId?: string) => { + if (!user || !urls[0]) return; - setAvatarLoading(true); + const newAvatarUrl = urls[0]; + const newImageId = imageId || ''; + + // Update local state immediately + setAvatarUrl(newAvatarUrl); + setAvatarImageId(newImageId); + try { const { error } = await supabase .from('profiles') .update({ - avatar_image_id: imageId, - avatar_url: imageUrl, + avatar_url: newAvatarUrl, + avatar_image_id: newImageId, updated_at: new Date().toISOString() }) .eq('user_id', user.id); @@ -105,13 +113,14 @@ export function AccountProfileTab() { description: 'Your avatar has been successfully updated.' }); } catch (error: any) { + // Revert local state on error + setAvatarUrl(profile?.avatar_url || ''); + setAvatarImageId(profile?.avatar_image_id || ''); toast({ title: 'Error', description: error.message || 'Failed to update avatar', variant: 'destructive' }); - } finally { - setAvatarLoading(false); } }; @@ -142,28 +151,20 @@ export function AccountProfileTab() { {/* Profile Picture */}

Profile Picture

-
- - - - {profile?.display_name?.[0] || profile?.username?.[0] || } - - -
- - - -

- JPG, PNG or GIF. Max size 5MB. -

-
-
+ { + toast({ + title: "Upload Error", + description: error, + variant: "destructive" + }); + }} + />