import { logout, updateUser } from "api/api" import { useState } from "react" import { AsyncButton } from "../AsyncButton" import { useAppContext } from "~/providers/AppProvider" export default function Account() { const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [confirmPw, setConfirmPw] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [success, setSuccess] = useState('') const { user, setUsername: setCtxUsername } = useAppContext() const logoutHandler = () => { setLoading(true) logout() .then(r => { if (r.ok) { window.location.reload() } else { r.json().then(r => setError(r.error)) } }).catch(err => setError(err)) setLoading(false) } const updateHandler = () => { if (password != "" && confirmPw === "") { setError("confirm your password before submitting") return } setError('') setSuccess('') setLoading(true) updateUser(username, password) .then(r => { if (r.ok) { setSuccess("sucessfully updated user") if (username != "") { setCtxUsername(username) } setUsername('') setPassword('') setConfirmPw('') } else { r.json().then((r) => setError(r.error)) } }).catch(err => setError(err)) setLoading(false) } return ( <>
You're logged in as {user?.username}
{success}
} {error != "" &&{error}
}