import { SubmitHandler, useForm } from "react-hook-form"; import { Text } from "preact-i18n"; import { useContext, useState } from "preact/hooks"; import { Category, Error, Modal } from "@revoltchat/ui"; import { noopTrue } from "../../../lib/js"; import FormField from "../../../pages/login/FormField"; import { AppContext } from "../../revoltjs/RevoltClient"; import { takeError } from "../../revoltjs/util"; import { ModalProps } from "../types"; 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 default function ModifyAccount({ field, ...props }: ModalProps<"modify_account">) { const client = useContext(AppContext); const [processing, setProcessing] = useState(false); const { handleSubmit, register, errors } = useForm(); const [error, setError] = useState(undefined); const onSubmit: SubmitHandler = async ({ password, new_username, new_email, new_password, }) => { if (processing) return; setProcessing(true); try { if (field === "email") { await client.api.patch("/auth/account/change/email", { current_password: password, email: new_email, }); props.onClose(); } else if (field === "password") { await client.api.patch("/auth/account/change/password", { current_password: password, password: new_password, }); props.onClose(); } else if (field === "username") { await client.api.patch("/users/@me/username", { username: new_username, password, }); props.onClose(); } } catch (err) { setError(takeError(err)); setProcessing(false); } }; return ( } disabled={processing} actions={[ { confirmation: true, onClick: () => void handleSubmit(onSubmit)(), children: field === "email" ? ( ) : ( ), }, { onClick: noopTrue, children: , palette: "plain", }, ]}> {/* Preact / React typing incompatabilities */}
{ e.preventDefault(); handleSubmit( onSubmit, // eslint-disable-next-line @typescript-eslint/no-explicit-any )(e as any); }}> {field === "email" && ( )} {field === "password" && ( )} {field === "username" && ( )} {error && ( } /> )}
); }