From 54b5a4b0a39f72309794581de4fcc6c12d22e1b3 Mon Sep 17 00:00:00 2001 From: notmd Date: Tue, 17 Jan 2023 10:13:53 +0700 Subject: [PATCH] use `react-hook-form` everywhere --- website/src/pages/account/edit.tsx | 77 ++++++++++++++++++------------ website/src/pages/auth/signin.tsx | 44 ++++++++++------- 2 files changed, 74 insertions(+), 47 deletions(-) diff --git a/website/src/pages/account/edit.tsx b/website/src/pages/account/edit.tsx index 497e8238..fe8e8981 100644 --- a/website/src/pages/account/edit.tsx +++ b/website/src/pages/account/edit.tsx @@ -2,27 +2,11 @@ import { Button, Input, InputGroup } from "@chakra-ui/react"; import Head from "next/head"; import Router from "next/router"; import { useSession } from "next-auth/react"; -import React, { useState } from "react"; +import React from "react"; +import { Control, useForm, useWatch } from "react-hook-form"; export default function Account() { const { data: session } = useSession(); - const [username, setUsername] = useState(""); - - const updateUser = async (e: React.SyntheticEvent) => { - e.preventDefault(); - try { - const body = { username }; - await fetch("/api/username", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(body), - }); - session.user.name = username; - await Router.push("/account"); - } catch (error) { - console.error(error); - } - }; if (!session) { return; @@ -39,21 +23,52 @@ export default function Account() {

{session.user.name || "No username"}

-
- - setUsername(e.target.value)} - placeholder="Edit Username" - type="text" - value={username} - > - - -
+
); } + +const EditForm = () => { + const { data: session } = useSession(); + + const updateUser = async ({ username }: { username: string }) => { + try { + const body = { username }; + await fetch("/api/username", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + }); + session.user.name = username; + await Router.push("/account"); + } catch (error) { + console.error(error); + } + }; + + const { register, handleSubmit, control } = useForm<{ username: string }>({ + defaultValues: { + username: session?.user.name, + }, + }); + + return ( +
+ + + + +
+ ); +}; + +const SubmitButton = ({ control }: { control: Control<{ username: string }> }) => { + const username = useWatch({ control, name: "username" }); + return ( + + ); +}; diff --git a/website/src/pages/auth/signin.tsx b/website/src/pages/auth/signin.tsx index ab1c74ca..99488d81 100644 --- a/website/src/pages/auth/signin.tsx +++ b/website/src/pages/auth/signin.tsx @@ -6,11 +6,12 @@ import Link from "next/link"; import { useRouter } from "next/router"; import { ClientSafeProvider, getProviders, signIn } from "next-auth/react"; import React, { useEffect, useRef, useState } from "react"; +import { useForm } from "react-hook-form"; import { FaBug, FaDiscord, FaEnvelope, FaGithub } from "react-icons/fa"; import { AuthLayout } from "src/components/AuthLayout"; import { Footer } from "src/components/Footer"; import { Header } from "src/components/Header"; -import { RoleSelect } from "src/components/RoleSelect"; +import { Role, RoleSelect } from "src/components/RoleSelect"; export type SignInErrorTypes = | "Signin" @@ -60,15 +61,14 @@ function Signin({ providers }: SigninProps) { } }, [router]); - const signinWithEmail = (ev: React.FormEvent) => { - ev.preventDefault(); - signIn(email.id, { callbackUrl: "/dashboard", email: emailEl.current.value }); + const signinWithEmail = (data: { email: string }) => { + signIn(email.id, { callbackUrl: "/dashboard", email: data.email }); }; const { colorMode } = useColorMode(); const bgColorClass = colorMode === "light" ? "bg-gray-50" : "bg-chakra-gray-900"; const buttonBgColor = colorMode === "light" ? "#2563eb" : "#2563eb"; - + const { register, handleSubmit } = useForm<{ email: string }>(); return (
@@ -79,7 +79,7 @@ function Signin({ providers }: SigninProps) { {credentials && } {email && ( -
+ }> Continue with Email @@ -174,23 +174,35 @@ const SigninButton = (props: ButtonProps) => { ); }; +interface DebugSigninFormData { + username: string; + role: Role; +} + const DebugSigninForm = ({ credentials, bgColorClass }: { credentials: ClientSafeProvider; bgColorClass: string }) => { - const debugUsernameEl = useRef(null); - const roleRef = useRef(null); - function signinWithDebugCredentials(ev: React.FormEvent) { - ev.preventDefault(); + const { register, handleSubmit } = useForm({ + defaultValues: { + role: "general", + username: "dev", + }, + }); + + function signinWithDebugCredentials(data: DebugSigninFormData) { signIn(credentials.id, { callbackUrl: "/dashboard", - username: debugUsernameEl.current.value, - role: roleRef.current.value, + ...data, }); } + return ( - + For Debugging Only - - + + }>Continue with Debug User