/* eslint-disable @next/next/no-img-element */ "use client"; import { UserInfo } from "@/ldap"; import React from "react"; import styles from "./AboutMe.module.css"; import AvatarChanger from "@/components/AvatarChanger"; import Input, { Label } from "@/components/Input"; import { Form, Formik, FormikHelpers } from "formik"; import { AboutMeFormValues, PasswordUpdateFormValues, aboutMeSchema, passwordUpdateSchema } from "@/schemas"; import PrettyForm from "@/components/PrettyForm"; import Toast from "@/components/Toast"; import { AuthProviderState } from "@/auth/AuthProvider"; import inputStyles from "@/components/Input.module.css"; import Connection from "@/components/Connection"; type UpdateResponse = { ok: boolean; error?: string; }; export default function AboutMe({ info, providers: [discordState, githubState] }: { info: UserInfo; providers: AuthProviderState[]; }) { // TODO: Reimplement password changing. const [globalError, setGlobalError] = React.useState(null); const [madeProfileChanges, setMadeChanges] = React.useState(false); const [madePasswordChanges, setMadePasswordChanges] = React.useState(false); const initialValues: AboutMeFormValues = { username: info.username, displayName: info.displayName, email: info.email, avatar: info.avatar }; async function handleFormSubmit( { displayName, email, avatar }: AboutMeFormValues, { setSubmitting }: FormikHelpers ) { setMadeChanges(false); setSubmitting(true); const req = await fetch("/api/update", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ displayName, email, avatar: avatar != null ? avatar.split(",")[1] : null }) }); setSubmitting(false); try { const res: UpdateResponse = await req.json(); if (!res.ok && res.error !== null) { switch (res.error) { case "avatarBig": break; } } setMadeChanges(true); } catch { console.error(req); } } const [passwordError, setPasswordError] = React.useState(null); const initialPasswordValues: PasswordUpdateFormValues = { password: "", newPassword: "", confirmPassword: "" }; async function handlePasswordSubmit( { password, newPassword }: PasswordUpdateFormValues, { setFieldError, setSubmitting }: FormikHelpers ) { setMadePasswordChanges(false); setSubmitting(true); const req = await fetch("/api/changePassword", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ currentPassword: password, newPassword: newPassword }) }); setSubmitting(false); try { const res: UpdateResponse = await req.json(); if (!res.ok && res.error !== null) { switch (res.error) { case "incorrectPassword": setFieldError("password", "Incorrect password."); break; } } setMadePasswordChanges(true); } catch { console.error(req); } } return (
{({ isSubmitting }) => (
( fieldProps.form.setFieldValue("avatar", newBlob) } vertical /> )} />

{info.username}

{madeProfileChanges ? ( Saved your changes. ) : null}
)}
{/* {({ isSubmitting }) => (
{madePasswordChanges ? ( Changed your password. ) : null}
)}

Connections

{providers.map((provider) => ( ))}
{ document.cookie = "ticket=; expires=" + new Date().toUTCString() + "; path=/"; window.location.href = "/"; }} />*/}
); }