use react-hook-form
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import { Button } from "~/components/ui/Button"
|
||||
import { useStore } from "~/lib/store"
|
||||
import { Dialog } from "@headlessui/react"
|
||||
import { useEffect, useState } from "react"
|
||||
import { trpc } from "~/lib/trpc"
|
||||
import { useFormik } from "formik"
|
||||
import { useForm } from "react-hook-form"
|
||||
|
||||
export const LoginModal: React.FC = () => {
|
||||
const [loginModalOpened, setLoginModalOpened] = useStore((store) => [
|
||||
@@ -12,16 +11,17 @@ export const LoginModal: React.FC = () => {
|
||||
])
|
||||
const requestLoginLink = trpc.useMutation("auth.requestLoginLink")
|
||||
|
||||
const form = useFormik({
|
||||
initialValues: {
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
email: "",
|
||||
},
|
||||
async onSubmit(values) {
|
||||
await requestLoginLink.mutateAsync({
|
||||
email: values.email,
|
||||
url: location.href,
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
const handleSubmit = form.handleSubmit((values) => {
|
||||
requestLoginLink.mutate({
|
||||
email: values.email,
|
||||
url: location.href,
|
||||
})
|
||||
})
|
||||
|
||||
return (
|
||||
@@ -45,7 +45,7 @@ export const LoginModal: React.FC = () => {
|
||||
inbox and spam folder in case you can{`'`}t find it.
|
||||
</div>
|
||||
)}
|
||||
<form className="space-y-5" onSubmit={form.handleSubmit}>
|
||||
<form className="space-y-5" onSubmit={handleSubmit}>
|
||||
<div>
|
||||
<label
|
||||
className="block mb-1 font-medium text-zinc-600"
|
||||
@@ -55,11 +55,9 @@ export const LoginModal: React.FC = () => {
|
||||
</label>
|
||||
<input
|
||||
id="email"
|
||||
name="email"
|
||||
type="email"
|
||||
className="input is-block"
|
||||
value={form.values.email}
|
||||
onChange={form.handleChange}
|
||||
{...form.register("email")}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import React, { useEffect } from "react"
|
||||
import { useFormik } from "formik"
|
||||
import { useStore } from "~/lib/store"
|
||||
import { Button } from "../ui/Button"
|
||||
import { Modal } from "../ui/Modal"
|
||||
import toast from "react-hot-toast"
|
||||
import { trpc } from "~/lib/trpc"
|
||||
import { Input } from "../ui/Input"
|
||||
import { useForm } from "react-hook-form"
|
||||
|
||||
export const SubscribeModal: React.FC<{
|
||||
siteId: string
|
||||
@@ -23,25 +23,26 @@ export const SubscribeModal: React.FC<{
|
||||
const unsubscribe = trpc.useMutation("site.unsubscribe")
|
||||
const trpcContext = trpc.useContext()
|
||||
|
||||
const subscribeForm = useFormik({
|
||||
initialValues: {
|
||||
const subscribeForm = useForm({
|
||||
defaultValues: {
|
||||
newUserEmail: "",
|
||||
email: subscription?.email ?? true,
|
||||
telegram: subscription?.telegram ?? false,
|
||||
},
|
||||
onSubmit(values) {
|
||||
subscribe.mutate({
|
||||
siteId,
|
||||
email: values.email,
|
||||
telegram: values.telegram,
|
||||
newUser: isLoggedIn
|
||||
? undefined
|
||||
: {
|
||||
email: values.newUserEmail,
|
||||
url: location.href,
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
const handleSubscribe = subscribeForm.handleSubmit((values) => {
|
||||
subscribe.mutate({
|
||||
siteId,
|
||||
email: values.email,
|
||||
telegram: values.telegram,
|
||||
newUser: isLoggedIn
|
||||
? undefined
|
||||
: {
|
||||
email: values.newUserEmail,
|
||||
url: location.href,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
@@ -70,40 +71,28 @@ export const SubscribeModal: React.FC<{
|
||||
<p>Please check your inbox (and spam folder).</p>
|
||||
</div>
|
||||
) : (
|
||||
<form className="p-5" onSubmit={subscribeForm.handleSubmit}>
|
||||
<form className="p-5" onSubmit={handleSubscribe}>
|
||||
{!isLoggedIn && (
|
||||
<div className="mb-5">
|
||||
<Input
|
||||
label="Email"
|
||||
name="newUserEmail"
|
||||
type="email"
|
||||
id="email"
|
||||
isBlock
|
||||
value={subscribeForm.values.newUserEmail}
|
||||
onChange={subscribeForm.handleChange}
|
||||
required
|
||||
{...subscribeForm.register("newUserEmail", {})}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div>
|
||||
<label className="select-none flex items-center space-x-1">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={subscribeForm.values.email}
|
||||
onChange={(e) =>
|
||||
subscribeForm.setFieldValue("email", e.target.checked)
|
||||
}
|
||||
/>
|
||||
<input type="checkbox" {...subscribeForm.register("email")} />
|
||||
<span>Receive updates via Email</span>
|
||||
</label>
|
||||
</div>
|
||||
<div className="">
|
||||
<label className="select-none flex items-center space-x-1">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={subscribeForm.values.telegram}
|
||||
onChange={(e) =>
|
||||
subscribeForm.setFieldValue("telegram", e.target.checked)
|
||||
}
|
||||
/>
|
||||
<input type="checkbox" {...subscribeForm.register("telegram")} />
|
||||
<span>Receive updates via Telegram</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
+17
-10
@@ -1,14 +1,20 @@
|
||||
import clsx from "clsx"
|
||||
import { DetailedHTMLProps, InputHTMLAttributes } from "react"
|
||||
import { DetailedHTMLProps, forwardRef, InputHTMLAttributes } from "react"
|
||||
|
||||
export const Input: React.FC<
|
||||
DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement> & {
|
||||
label?: string
|
||||
addon?: string
|
||||
isBlock?: boolean
|
||||
error?: string
|
||||
}
|
||||
> = ({ label, addon, className, isBlock, error, ...inputProps }) => {
|
||||
type Props = DetailedHTMLProps<
|
||||
InputHTMLAttributes<HTMLInputElement>,
|
||||
HTMLInputElement
|
||||
> & {
|
||||
label?: string
|
||||
addon?: string
|
||||
isBlock?: boolean
|
||||
error?: string
|
||||
}
|
||||
|
||||
export const Input = forwardRef<HTMLInputElement, Props>(function Input(
|
||||
{ label, addon, className, isBlock, error, ...inputProps },
|
||||
ref
|
||||
) {
|
||||
const hasAddon = !!addon
|
||||
return (
|
||||
<div>
|
||||
@@ -20,6 +26,7 @@ export const Input: React.FC<
|
||||
<div className="flex items-center">
|
||||
<input
|
||||
{...inputProps}
|
||||
ref={ref}
|
||||
className={clsx(
|
||||
"input",
|
||||
hasAddon && `has-addon`,
|
||||
@@ -36,4 +43,4 @@ export const Input: React.FC<
|
||||
{error && <div className="text-sm mt-1 text-red-500">{error}</div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useFormik } from "formik"
|
||||
import { useEffect } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import toast from "react-hot-toast"
|
||||
import { AvatarForm } from "~/components/dashboard/AvatarForm"
|
||||
import { DashboardLayout } from "~/components/dashboard/DashboardLayout"
|
||||
@@ -12,16 +12,17 @@ export default function AccountProfilePage() {
|
||||
const viewer = trpc.useQuery(["auth.viewer"])
|
||||
const updateProfile = trpc.useMutation("user.updateProfile")
|
||||
|
||||
const form = useFormik({
|
||||
initialValues: {
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
name: "",
|
||||
username: "",
|
||||
bio: "",
|
||||
email: "",
|
||||
},
|
||||
onSubmit(values) {
|
||||
updateProfile.mutate(values)
|
||||
},
|
||||
})
|
||||
|
||||
const handleSubmit = form.handleSubmit((values) => {
|
||||
updateProfile.mutate(values)
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
@@ -33,12 +34,10 @@ export default function AccountProfilePage() {
|
||||
|
||||
useEffect(() => {
|
||||
if (viewer.data) {
|
||||
form.setValues({
|
||||
name: viewer.data.name,
|
||||
username: viewer.data.username,
|
||||
bio: viewer.data.bio || "",
|
||||
email: viewer.data.email,
|
||||
})
|
||||
form.setValue("name", viewer.data.name)
|
||||
form.setValue("username", viewer.data.username)
|
||||
form.setValue("bio", viewer.data.bio || "")
|
||||
form.setValue("email", viewer.data.email || "")
|
||||
}
|
||||
}, [viewer.data, form])
|
||||
|
||||
@@ -51,38 +50,32 @@ export default function AccountProfilePage() {
|
||||
<AvatarForm filename={viewer.data.avatar} name={viewer.data.name} />
|
||||
</div>
|
||||
)}
|
||||
<form onSubmit={form.handleSubmit}>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="mt-5">
|
||||
<Input
|
||||
label="Display Name"
|
||||
id="name"
|
||||
name="name"
|
||||
required
|
||||
type="text"
|
||||
value={form.values.name}
|
||||
onChange={form.handleChange}
|
||||
{...form.register("name")}
|
||||
/>
|
||||
</div>
|
||||
<div className="mt-5">
|
||||
<Input
|
||||
label="Username"
|
||||
id="username"
|
||||
name="username"
|
||||
required
|
||||
type="text"
|
||||
value={form.values.username}
|
||||
onChange={form.handleChange}
|
||||
{...form.register("username")}
|
||||
/>
|
||||
</div>
|
||||
<div className="mt-5">
|
||||
<Input
|
||||
label="Email"
|
||||
id="email"
|
||||
name="email"
|
||||
required
|
||||
type="email"
|
||||
value={form.values.email}
|
||||
onChange={form.handleChange}
|
||||
{...form.register("email")}
|
||||
/>
|
||||
</div>
|
||||
<div className="mt-10">
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useFormik } from "formik"
|
||||
import { useRouter } from "next/router"
|
||||
import { useEffect } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import toast from "react-hot-toast"
|
||||
import { DashboardLayout } from "~/components/dashboard/DashboardLayout"
|
||||
import { SettingsLayout } from "~/components/dashboard/SettingsLayout"
|
||||
@@ -18,16 +18,17 @@ export default function SettingsDomainsPage() {
|
||||
const ctx = trpc.useContext()
|
||||
const updateSite = trpc.useMutation("site.updateSite")
|
||||
|
||||
const form = useFormik({
|
||||
initialValues: {
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
subdomain: "",
|
||||
},
|
||||
onSubmit(values) {
|
||||
updateSite.mutate({
|
||||
site: siteResult.data!.id,
|
||||
subdomain: values.subdomain,
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
const handleSubmit = form.handleSubmit((values) => {
|
||||
updateSite.mutate({
|
||||
site: siteResult.data!.id,
|
||||
subdomain: values.subdomain,
|
||||
})
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
@@ -45,25 +46,21 @@ export default function SettingsDomainsPage() {
|
||||
|
||||
useEffect(() => {
|
||||
if (siteResult.data) {
|
||||
form.setValues({
|
||||
subdomain: siteResult.data.subdomain,
|
||||
})
|
||||
form.setValue("subdomain", siteResult.data.subdomain)
|
||||
}
|
||||
}, [form, siteResult.data])
|
||||
|
||||
return (
|
||||
<DashboardLayout>
|
||||
<SettingsLayout title="Site Settings" type="site">
|
||||
<form onSubmit={form.handleSubmit}>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="">
|
||||
<Input
|
||||
name="subdomain"
|
||||
id="subdomain"
|
||||
label="Subdomain"
|
||||
addon={`.${OUR_DOMAIN}`}
|
||||
value={form.values.subdomain}
|
||||
className="w-28"
|
||||
onChange={form.handleChange}
|
||||
{...form.register("subdomain")}
|
||||
error={updateSite.error?.message}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -7,7 +7,7 @@ import { SettingsLayout } from "~/components/dashboard/SettingsLayout"
|
||||
import { useRouter } from "next/router"
|
||||
import { DashboardLayout } from "~/components/dashboard/DashboardLayout"
|
||||
import { trpc } from "~/lib/trpc"
|
||||
import { useFormik } from "formik"
|
||||
import { useForm } from "react-hook-form"
|
||||
|
||||
export default function SiteSettingsGeneralPage() {
|
||||
const router = useRouter()
|
||||
@@ -20,28 +20,27 @@ export default function SiteSettingsGeneralPage() {
|
||||
const site = siteResult.data
|
||||
const updateSite = trpc.useMutation("site.updateSite")
|
||||
|
||||
const form = useFormik({
|
||||
initialValues: {
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
name: "",
|
||||
description: "",
|
||||
},
|
||||
onSubmit(values) {
|
||||
updateSite.mutate({
|
||||
site: subdomain,
|
||||
name: values.name,
|
||||
description: values.description,
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
const handleSubmit = form.handleSubmit((values) => {
|
||||
updateSite.mutate({
|
||||
site: subdomain,
|
||||
name: values.name,
|
||||
description: values.description,
|
||||
})
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (site) {
|
||||
form.setValues({
|
||||
name: site.name,
|
||||
description: site.description || "",
|
||||
})
|
||||
form.setValue("name", site.name)
|
||||
form.setValue("description", site.description || "")
|
||||
}
|
||||
}, [form, site])
|
||||
}, [site, form])
|
||||
|
||||
useEffect(() => {
|
||||
if (updateSite.isSuccess && updateSite.data) {
|
||||
@@ -63,16 +62,9 @@ export default function SiteSettingsGeneralPage() {
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<form onSubmit={form.handleSubmit}>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="mt-5">
|
||||
<Input
|
||||
required
|
||||
label="Name"
|
||||
id="name"
|
||||
name="name"
|
||||
value={form.values.name}
|
||||
onChange={form.handleChange}
|
||||
/>
|
||||
<Input required label="Name" id="name" {...form.register("name")} />
|
||||
</div>
|
||||
<div className="mt-5">
|
||||
<label htmlFor="description" className="label">
|
||||
@@ -81,10 +73,8 @@ export default function SiteSettingsGeneralPage() {
|
||||
<textarea
|
||||
id="description"
|
||||
className="input is-block"
|
||||
name="description"
|
||||
rows={6}
|
||||
value={form.values.description}
|
||||
onChange={form.handleChange}
|
||||
{...form.register("description")}
|
||||
/>
|
||||
</div>
|
||||
<div className="mt-5">
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useFormik } from "formik"
|
||||
import Link from "next/link"
|
||||
import { useRouter } from "next/router"
|
||||
import { useEffect } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import toast from "react-hot-toast"
|
||||
import { Button } from "~/components/ui/Button"
|
||||
import { Input } from "~/components/ui/Input"
|
||||
@@ -13,14 +13,15 @@ export default function NewSitePage() {
|
||||
const viewer = trpc.useQuery(["auth.viewer"])
|
||||
const createSite = trpc.useMutation("site.create")
|
||||
|
||||
const form = useFormik({
|
||||
initialValues: {
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
name: "",
|
||||
subdomain: "",
|
||||
},
|
||||
onSubmit(values) {
|
||||
createSite.mutate(values)
|
||||
},
|
||||
})
|
||||
|
||||
const handleSubmit = form.handleSubmit((values) => {
|
||||
createSite.mutate(values)
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
@@ -60,28 +61,27 @@ export default function NewSitePage() {
|
||||
</header>
|
||||
<div className="max-w-sm mx-auto mt-20">
|
||||
<h2 className="text-3xl mb-10 text-center">Create a new site</h2>
|
||||
<form className="space-y-5" onSubmit={form.handleSubmit}>
|
||||
<form className="space-y-5" onSubmit={handleSubmit}>
|
||||
<div>
|
||||
<Input
|
||||
name="name"
|
||||
id="name"
|
||||
label="Site Name"
|
||||
required
|
||||
isBlock
|
||||
value={form.values.name}
|
||||
onChange={form.handleChange}
|
||||
required
|
||||
maxLength={30}
|
||||
{...form.register("name", {})}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Input
|
||||
name="subdomain"
|
||||
id="subdomain"
|
||||
label="Subdomain"
|
||||
required
|
||||
isBlock
|
||||
required
|
||||
addon={`.${OUR_DOMAIN}`}
|
||||
value={form.values.subdomain}
|
||||
onChange={form.handleChange}
|
||||
minLength={2}
|
||||
maxLength={20}
|
||||
{...form.register("subdomain", {})}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
Reference in New Issue
Block a user