diff --git a/src/pages/account/changeemail.tsx b/src/pages/account/changeemail.tsx index 8ac5bb4..1d32f83 100644 --- a/src/pages/account/changeemail.tsx +++ b/src/pages/account/changeemail.tsx @@ -1,46 +1,72 @@ -import React, { useState } from "react"; -import Panel from "../../components/Panel"; -import FormWrapper, { FormButton, FormField } from "../../components/FormWrapper"; -import { fetchApi, FetchResult } from "../../lib/request"; -import { withSessionSsr } from "../../lib/session"; -import { changeEmailSchema } from "../../schemas/ChangeEmail"; - -const fields: FormField[] = [ +import React from "react"; +import Panel from "@component/Panel"; +import { withSessionSsr } from "@lib/session"; +import { trpc } from "@util/trpc"; +import { useForm, SubmitHandler } from "react-hook-form"; +import { z } from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useFormFeedback } from "@hook/useFormFeedback"; +import { Container, VStack, Wrap } from "@chakra-ui/react"; +import TextInput from "@component/TextInput"; +import Button from "@component/Button"; +import { FormField } from "@component/FormField"; + +const fields = [ { type: "email", name: "newEmail", - label: { text: "E-mail Address" }, + label: "E-mail Address", }, { type: "password", name: "password", - label: { text: "Password" }, + label: "Password", }, ]; -const buttons: FormButton[] = [ - { type: "submit", btnColorType: "primary", value: "Submit" }, - { href: "/account", value: "Back" }, -]; +const schema = z.object({ + newEmail: z.string().email({ message: "Invalid email address" }), + password: z.string().min(6, { message: "Password must be at least 6 characters long" }), +}); export default function ChangeEmail() { - const [response, setResponse] = useState(undefined); + const { + register, + handleSubmit, + reset, + formState: { errors, isValid, isSubmitting }, + } = useForm>({ + resolver: zodResolver(schema), + }); + const { handleResponse, showResponse } = useFormFeedback(); + const changeEmail = trpc.account.changeEmail.useMutation(); - const onSubmit = async (values: any, { resetForm }: any) => { - const response = await fetchApi("POST", "/api/account/changeemail", { - data: { - email: values.newEmail, - password: values.password, - }, + const onSubmit: SubmitHandler> = async ({ newEmail, password }) => { + handleResponse(async () => { + await changeEmail.mutateAsync({ newEmail, password }); + showResponse("Email changed.", "success"); }); - setResponse(response); - resetForm(); + reset(); }; return ( - +
+ + + {fields.map((field) => ( + + + + ))} + +