import { SubmitHandler, useForm } from "react-hook-form"; import { Text } from "preact-i18n"; import { useContext, useState } from "preact/hooks"; import Modal from "../../../components/ui/Modal"; import Overline from "../../../components/ui/Overline"; import FormField from "../../../pages/login/FormField"; import { AppContext } from "../../revoltjs/RevoltClient"; import { takeError } from "../../revoltjs/util"; interface Props { onClose: () => void; field: "username" | "email" | "password"; } interface FormInputs { password: string; new_email: string; new_username: string; new_password: string; // TODO: figure out if this is correct or not // it wasn't in the types before this was typed but the element itself was there current_password?: string; } export function ModifyAccountModal({ onClose, field }: Props) { const client = useContext(AppContext); const { handleSubmit, register, errors } = useForm(); const [error, setError] = useState(undefined); const onSubmit: SubmitHandler = async ({ password, new_username, new_email, new_password, }) => { try { if (field === "email") { await client.req("POST", "/auth/change/email", { password, new_email, }); onClose(); } else if (field === "password") { await client.req("POST", "/auth/change/password", { password, new_password, }); onClose(); } else if (field === "username") { await client.req("PATCH", "/users/id/username", { username: new_username, password, }); onClose(); } } catch (err) { setError(takeError(err)); } }; return ( } actions={[ { confirmation: true, onClick: handleSubmit(onSubmit), children: field === "email" ? ( ) : ( ), }, { onClick: onClose, children: , }, ]}> {/* Preact / React typing incompatabilities */}
}> {field === "email" && ( )} {field === "password" && ( )} {field === "username" && ( )} {error && ( )}
); }