From bd8aea46b15ddd4071284fbc237243da70eda016 Mon Sep 17 00:00:00 2001 From: Vishesh 'ironeagle' Bangotra Date: Fri, 14 Nov 2025 23:29:44 +0530 Subject: [PATCH] upload working for avatar --- src/blog/components/Profile.tsx | 40 ++++++++++++++++++++++++++------- src/blog/providers/Author.tsx | 39 +++++++++++++++----------------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/blog/components/Profile.tsx b/src/blog/components/Profile.tsx index 9b9cd4c..6b91a54 100644 --- a/src/blog/components/Profile.tsx +++ b/src/blog/components/Profile.tsx @@ -17,18 +17,22 @@ interface ProfileProps { } export default function Profile({ onBack }: ProfileProps) { - const { currentUser, loading, error, logout, updateProfile } = useAuth(); + const { currentUser, loading, error, logout, updateProfile, updateAvatar } = useAuth(); const [formData, setFormData] = React.useState({ username: currentUser?.username || '', name: currentUser?.name || '', email: currentUser?.email || '', avatar: currentUser?.avatar || '', }); + + const [avatarFile, setAvatarFile] = React.useState(null); + const [uploadingAvatar, setUploadingAvatar] = React.useState(false); const [success, setSuccess] = React.useState(null); const [saving, setSaving] = React.useState(false); React.useEffect(() => { if (currentUser) setFormData(currentUser); + console.log("Current User:", currentUser); }, [currentUser]); const handleChange = (e: React.ChangeEvent) => { @@ -36,6 +40,21 @@ export default function Profile({ onBack }: ProfileProps) { setFormData((prev) => ({ ...prev, [name]: value })); }; + const handleAvatarUpload = async (file: File) => { + setUploadingAvatar(true); + + try { + const updated = await updateAvatar(file); + if (updated) { + setFormData((prev) => ({ ...prev, avatar: updated.avatar })); + } + } catch (err) { + console.error("Avatar upload failed:", err); + } finally { + setUploadingAvatar(false); + } + }; + const handleSave = async () => { if (!currentUser) return; @@ -107,13 +126,18 @@ export default function Profile({ onBack }: ProfileProps) { alt={formData.name || formData.username} sx={{ width: 64, height: 64 }} /> - + + = ({ children const arrayBuffer = await file.arrayBuffer(); const binary = new Uint8Array(arrayBuffer); - const res = await fetch('/media/upload', { - method: 'POST', - headers: { - 'Content-Type': file.type, - 'Content-Disposition': `attachment; filename="${file.name}"`, - }, - body: binary, - }); + const res = await api.post( + "/uploads", + binary, + { + headers: { + "Content-Type": file.type, + "Content-Disposition": `attachment; filename="${file.name}"`, + }, + } + ); - if (!res.ok) throw new Error('Avatar upload failed'); - - const data = await res.json(); - return data.url; // "/media/uploads/uuid.jpg" - } catch (err) { - console.error('Failed to upload avatar:', err); + return res.data.url; + } catch (err: any) { + console.error("Avatar upload failed:", err); + setError(err.response?.data?.detail || "Failed to upload avatar"); return null; } }; @@ -146,18 +146,15 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children if (!currentUser) return; const url = await uploadAvatar(file); - if (!url) { - setError("Failed to upload avatar"); - return; - } + if (!url) return; - // Update the user's avatar - const updated = await updateProfile({ + // Now update the author document in DB + const updatedUser = await updateProfile({ ...currentUser, avatar: url, }); - return updated; + return updatedUser; }; /** 🔹 On mount, try to fetch user if token exists */