import { AllString, classNames, getTasks, validatePassword as validatePassword, } from "@/lib"; import zustand, { User, UserAddModify } from "@/Zustand"; import { Checkbox, CheckboxGroup, Form, Input, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader, } from "@heroui/react"; import { useAsyncList } from "@react-stately/data"; import React, { FormEvent, useState } from "react"; export default function UserEditor(props: { header: React.ReactNode; footer: React.ReactNode; value?: User; isPasswordRequired?: boolean; isOpen?: boolean; onOpenChange?: (isOpen: boolean) => void; onSubmit: (user: UserAddModify) => void; }) { const [name, setName] = useState(props.value?.userName ?? ""); const [password, setPassword] = useState(""); const [admin, setAdmin] = useState(props.value?.admin ?? false); const [possibleTasks, setPossibleTasks] = useState( props.value?.possibleTasks.map((t) => t.toString()) ?? [], ); const tasks = useAsyncList({ async load() { return { items: await getTasks(), }; }, }); const pwErrors = validatePassword(password); // update the user in the backend async function submit(e: FormEvent) { const formData = Object.fromEntries( new FormData(e.currentTarget), ) as AllString; const data = { ...formData, possibleTasks: possibleTasks.map((t) => parseInt(t)), 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.value?.userName === zustand.getState().user?.userName; props.onSubmit(data); } return (
{ e.preventDefault(); submit(e); }} >

{props.header}

0 && !!props.value ? "warning" : "default" } name="password" variant="bordered" value={password} onValueChange={setPassword} isInvalid={password.length > 0 && pwErrors.length > 0} errorMessage={
    {pwErrors.map((e, ii) => (
  • {e}
  • ))}
} /> Admin {tasks.items.map((task) => ( {task.taskName} ))}
{props.footer}
); }