diff --git a/components/JoinParty.tsx b/components/JoinParty.tsx index 587ddc1..d5bb805 100644 --- a/components/JoinParty.tsx +++ b/components/JoinParty.tsx @@ -1,21 +1,20 @@ "use client"; -import { FormEvent } from "react"; +import { useContext } from "react"; +import { SubmitHandler, useForm } from "react-hook-form"; import { useSWRConfig } from "swr"; +import { ErrorContext } from "./App"; + +type Inputs = { + code: string; +}; export default function JoinParty() { const { mutate } = useSWRConfig(); + const { register, handleSubmit } = useForm(); + const { setError } = useContext(ErrorContext); - async function onSubmit(event: FormEvent) { - event.preventDefault(); - - const data = new FormData(event.currentTarget); - const value = data.get("code"); - if (!value) { - throw new Error(); - } - - const code = value.toString().toLowerCase(); + const onSubmit: SubmitHandler = async ({ code }) => { const res = await fetch("/api/party/join", { method: "POST", headers: { @@ -25,18 +24,17 @@ export default function JoinParty() { }); if (!res.ok) { - throw new Error(await res.json()); + setError(await res.json()); + return; } mutate("/api/user"); - } + }; return ( -
+ =12.22.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/react-hook-form" + }, + "peerDependencies": { + "react": "^16.8.0 || ^17 || ^18" + } + }, "node_modules/react-is": { "version": "16.13.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", diff --git a/package.json b/package.json index 194e2f9..4b97eb0 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "next-themes": "^0.2.1", "react": "^18", "react-dom": "^18", + "react-hook-form": "^7.50.1", "swr": "^2.2.5" }, "devDependencies": {