import { apiCall, classNames, vaidatePassword as validatePassword, } from "@/lib"; import zustand, { User } from "@/Zustand"; import { Button, Checkbox, Form, Input, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader, } from "@nextui-org/react"; import { FormEvent, useEffect, useState } from "react"; export default function EditUser(props: { isOpen: boolean; user?: User; onOpenChange: (isOpen: boolean) => void; onSuccess: () => void; }) { const [name, setName] = useState(props.user?.userName); const [admin, setAdmin] = useState(props.user?.admin); const [password, setPassword] = useState(""); const pwErrors = validatePassword(password); // set the states on value changes useEffect(() => { if (props.user !== undefined) { setName(props.user.userName); setAdmin(props.user.admin); // reset the password setPassword(""); } }, [props.user]); // update the user in the backend async function updateUser(e: FormEvent) { const formData = Object.fromEntries(new FormData(e.currentTarget)); const data = { ...formData, userName: props.user?.userName, admin: formData.admin !== undefined, }; // if we modify ourself, set admin to true since it isn't included in the form data because the checkbox is disabled data.admin ||= props.user?.userName === zustand.getState().user?.userName; const result = await apiCall("PATCH", "users", undefined, data); if (result.ok) { // if we updated ourself if (props.user?.userName === zustand.getState().user?.userName) { zustand.setState({ user: null }); } props.onSuccess(); } } return ( {props.user !== undefined ? (

Edit User{" "} {props.user.userName}

{ e.preventDefault(); updateUser(e); }} > 0 ? "warning" : "default"} name="password" value={password} onValueChange={setPassword} isInvalid={password.length > 0 && pwErrors.length > 0} errorMessage={
    {pwErrors.map((e, ii) => (
  • {e}
  • ))}
} /> Admin
) : null}
); }