1- import { useMemo , useRef , useState } from "react" ;
21import Head from "next/head" ;
32import { z } from "zod" ;
43import { zodResolver } from "@hookform/resolvers/zod" ;
54import { useForm } from "react-hook-form" ;
65import ArrowPathIcon from "@heroicons/react/24/solid/ArrowPathIcon" ;
76
8- import Button from "@/components/controls/Button" ;
97import { Page } from "@/types/page" ;
108import { backend } from "@/utils/wretch" ;
9+ import { makeOgMeta } from "@/utils/meta/opengraph" ;
10+ import { makeSitemapMeta } from "@/utils/meta/sitemap" ;
11+ import useFormMutation from "@/hooks/useMutation" ;
12+
1113import { BackendResponse } from "@/api/models/Response" ;
14+ import { sendUnsubscribeEvent } from "@/api/analytics" ;
15+
1216import Input from "@/components/controls/Input" ;
1317import Form from "@/components/controls/Form" ;
1418import Heading from "@/components/layout/Heading" ;
15- import { makeOgMeta } from "@/utils/meta/opengraph " ;
16- import { makeSitemapMeta } from "@/utils/meta/sitemap" ;
19+ import Button from "@/components/controls/Button " ;
20+
1721
1822const unsubscribeSchema = z . object ( {
1923 email : z . string ( ) . email ( ) . min ( 3 )
@@ -28,20 +32,14 @@ const UnsubscribePage: Page = ({ pathname }) => {
2832 mode : "onChange"
2933 } ) ;
3034
31- const [ response , setResponse ] = useState < null | BackendResponse > ( null ) ;
32- const [ loading , setLoading ] = useState ( false ) ;
33-
34- const loadingRef = useRef ( loading ) ;
35- loadingRef . current = loading ;
36-
37- const submit = useMemo ( ( ) => handleSubmit ( ( { email } ) => {
38- if ( loadingRef . current ) return ;
39- setLoading ( true ) ;
40-
35+ const [ submit , response , loading ] = useFormMutation < BackendResponse , UnsubscribeSchema > ( handleSubmit , ( setResponse , { email } ) => {
4136 backend . url ( "/email/unsubscribe" )
4237 . post ( { email } )
43- . json ( ( res : BackendResponse ) => setResponse ( res ) ) ;
44- } ) , [ handleSubmit ] ) ;
38+ . json ( ( res : BackendResponse ) => {
39+ if ( res . success ) sendUnsubscribeEvent ( )
40+ setResponse ( res )
41+ } ) ;
42+ } ) ;
4543
4644 return (
4745 < >
0 commit comments