import { useEffect, useState } from 'preact/hooks'; import { httpGet, httpPost } from '../../lib/http'; import { pageProgressMessage } from '../../stores/page'; import UploadProfilePicture from './UploadProfilePicture'; export function UpdateProfileForm() { const [name, setName] = useState(''); const [avatar, setAvatar] = useState(''); const [email, setEmail] = useState(''); const [github, setGithub] = useState(''); const [twitter, setTwitter] = useState(''); const [linkedin, setLinkedin] = useState(''); const [website, setWebsite] = useState(''); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(''); const handleSubmit = async (e: Event) => { e.preventDefault(); setIsLoading(true); setError(''); setSuccess(''); const { response, error } = await httpPost( `${import.meta.env.PUBLIC_API_URL}/v1-update-profile`, { name, github: github || undefined, linkedin: linkedin || undefined, twitter: twitter || undefined, website: website || undefined, } ); if (error || !response) { setIsLoading(false); setError(error?.message || 'Something went wrong'); return; } await loadProfile(); setSuccess('Profile updated successfully'); }; const loadProfile = async () => { // Set the loading state setIsLoading(true); const { error, response } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-me` ); if (error || !response) { setIsLoading(false); setError(error?.message || 'Something went wrong'); return; } const { name, email, links, avatar } = response; setName(name); setEmail(email); setGithub(links?.github || ''); setLinkedin(links?.linkedin || ''); setTwitter(links?.twitter || ''); setWebsite(links?.website || ''); setAvatar(avatar || ''); setIsLoading(false); }; // Make a request to the backend to fill in the form with the current values useEffect(() => { loadProfile().finally(() => { pageProgressMessage.set(''); }); }, []); return (
Update your profile details below.