/* eslint-disable @next/next/no-img-element */ "use client"; import { UserInfo } from "@/ldap"; import React, { HTMLInputTypeAttribute, InputHTMLAttributes } from "react"; import styles from "./AboutMe.module.css"; import AvatarChanger from "@/components/AvatarChanger"; import Input from "@/components/Input"; import { Form, Formik, FormikHelpers } from "formik"; import { AboutMeFormValues, PasswordUpdateFormValues, aboutMeSchema, passwordUpdateSchema } from "@/schemas"; import PrettyForm from "@/components/PrettyForm"; type UpdateResponse = { ok: boolean; error?: string; }; // TODO skip do your magic type InputProps = { label: string; name: string; type: HTMLInputTypeAttribute; error?: string; displayImage?: string; } & InputHTMLAttributes; async function fileAsBase64(f: File) { const reader = new FileReader(); reader.readAsArrayBuffer(f); return new Promise((resolve, reject) => { reader.onload = () => { const result = reader.result as ArrayBuffer; const buffer = Buffer.from(result); resolve(buffer.toString("base64")); }; reader.onerror = () => reject(reader.error); }); } export default function AboutMe({ info }: { info: UserInfo }) { const [globalError, setGlobalError] = React.useState(null); const initialValues: AboutMeFormValues = { username: info.username, displayName: info.displayName, email: info.email, avatar: info.avatar }; async function handleFormSubmit( { displayName, email, avatar }: AboutMeFormValues, { setSubmitting }: FormikHelpers ) { 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; } } } 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 ) { console.log(password, newPassword); 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; } } } catch { console.error(req); } } return (

{info.username}

{({ isSubmitting }) => (
( fieldProps.form.setFieldValue("avatar", newBlob) } /> )} /> )}

Change password

{({ isSubmitting }) => (
)}

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