import React, { useEffect, useRef, useState } from 'react'; import { Link } from 'react-router-dom'; import { ArrowSquareOutIcon, CameraIcon, CircleNotchIcon, GlobeIcon, InstagramLogoIcon, LinkedinLogoIcon, YoutubeLogoIcon } from '@phosphor-icons/react'; import { useAuth } from '../context/AuthContext'; import { Profile, ProfileInput, instructorApi, profileApi } from '../services/api'; import { CourseCoverImage } from '../components/CourseCoverImage'; import { TwoFactorSettings } from '../components/account/TwoFactorSettings'; import { Switch } from '../components/ui/Switch'; import { useToast } from '../context/ToastContext'; const emptyProfile = (user: { id: string; email: string; name: string; role: string }): Profile => ({ id: user.id, email: user.email, name: user.name, role: user.role === 'superadmin' ? 'admin' : user.role === 'professor' ? 'instructor' : 'student', avatarImageUrl: null, headline: '', bio: '', websiteUrl: null, linkedinUrl: null, instagramUrl: null, youtubeUrl: null, isPublic: true, }); export const ProfilePage: React.FC = () => { const { user, updateCurrentUser } = useAuth(); const { showToast } = useToast(); const fileInput = useRef(null); const [profile, setProfile] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [isUploading, setIsUploading] = useState(false); useEffect(() => { if (!user) return; profileApi.me().then((response) => setProfile(response.data)).catch(() => { setProfile(emptyProfile(user)); showToast('Não foi possível carregar todos os dados do perfil.', 'error'); }).finally(() => setIsLoading(false)); }, [showToast, user]); if (isLoading || !profile || !user) return
; const update = (key: K, value: Profile[K]) => setProfile((current) => current ? { ...current, [key]: value } : current); const uploadAvatar = async (file?: File) => { if (!file) return; if (!file.type.startsWith('image/')) { showToast('Selecione uma imagem JPG, PNG ou WebP.', 'error'); return; } setIsUploading(true); try { const response = await instructorApi.uploadCover(file); update('avatarImageUrl', response.data.coverImageUrl); showToast('Foto enviada. Salve o perfil para publicar a alteração.', 'success'); } catch { showToast('Não foi possível enviar a foto.', 'error'); } finally { setIsUploading(false); } }; const save = async (event: React.FormEvent) => { event.preventDefault(); const input: ProfileInput = { name: profile.name, avatarImageUrl: profile.avatarImageUrl, headline: profile.headline, bio: profile.bio, websiteUrl: profile.websiteUrl, linkedinUrl: profile.linkedinUrl, instagramUrl: profile.instagramUrl, youtubeUrl: profile.youtubeUrl, isPublic: profile.isPublic }; setIsSaving(true); try { const response = await profileApi.update(input); setProfile(response.data); updateCurrentUser({ id: user.id, email: user.email, name: response.data.name, role: response.data.role }); showToast('Perfil salvo com sucesso.', 'success'); } catch { showToast('Não foi possível salvar o perfil. Verifique os links informados.', 'error'); } finally { setIsSaving(false); } }; return

Conta

Seus dados, apresentação e links.

{profile.isPublic && Ver perfil público}
{profile.avatarImageUrl ? : profile.name.slice(0, 2).toUpperCase()} { void uploadAvatar(event.target.files?.[0]); event.currentTarget.value = ''; }} />

Dados pessoais

update('name', event.target.value)} maxLength={120} required />
update('headline', event.target.value)} maxLength={180} />