diff --git a/packages/frontend/src/pages/Profile.tsx b/packages/frontend/src/pages/Profile.tsx new file mode 100644 index 0000000..8551237 --- /dev/null +++ b/packages/frontend/src/pages/Profile.tsx @@ -0,0 +1,87 @@ +import { useState } from 'react'; +import { authApi } from '../api/auth.js'; +import { ApiClientError } from '../api/client.js'; +import { useAuth } from '../stores/authStore.js'; + +export function ProfilePage() { + const { user, refreshMe } = useAuth(); + const [displayName, setDisplayName] = useState(user?.displayName ?? ''); + const [email, setEmail] = useState(user?.email ?? ''); + const [profileBusy, setProfileBusy] = useState(false); + const [profileMsg, setProfileMsg] = useState(null); + + const [currentPassword, setCurrentPassword] = useState(''); + const [newPassword, setNewPassword] = useState(''); + const [pwBusy, setPwBusy] = useState(false); + const [pwMsg, setPwMsg] = useState(null); + const [pwErr, setPwErr] = useState(null); + + async function saveProfile(e: React.FormEvent) { + e.preventDefault(); + setProfileBusy(true); setProfileMsg(null); + try { + const updates: { displayName?: string; email?: string } = {}; + if (displayName !== user?.displayName) updates.displayName = displayName; + if (email !== user?.email) updates.email = email; + if (Object.keys(updates).length === 0) { setProfileMsg('Geen wijzigingen.'); return; } + await authApi.updateProfile(updates); + await refreshMe(); + setProfileMsg(updates.email ? 'Profiel opgeslagen. Bevestig je nieuwe e-mailadres via de link in de mail.' : 'Profiel opgeslagen.'); + } catch (err) { + setProfileMsg(err instanceof ApiClientError ? err.message : 'Opslaan mislukt.'); + } finally { setProfileBusy(false); } + } + + async function changePassword(e: React.FormEvent) { + e.preventDefault(); + setPwBusy(true); setPwMsg(null); setPwErr(null); + try { + await authApi.changePassword({ currentPassword, newPassword }); + setPwMsg('Wachtwoord gewijzigd.'); + setCurrentPassword(''); setNewPassword(''); + } catch (err) { + setPwErr(err instanceof ApiClientError ? err.message : 'Wijzigen mislukt.'); + } finally { setPwBusy(false); } + } + + if (!user) return null; + + return ( +
+
+

Profiel

+

Beheer je naam, e-mailadres en wachtwoord.

+
+ +
+

Gegevens

+ + + {profileMsg &&

{profileMsg}

} + +
+ +
+

Wachtwoord

+ + + {pwMsg &&

{pwMsg}

} + {pwErr &&

{pwErr}

} + +
+
+ ); +}