1
1
"use client" ;
2
2
import React , { FormEvent , FocusEvent , useState } from "react" ;
3
- import { EmailResponse } from "@api/emails/route.schema" ;
3
+ import { useApiThrottle } from "@hooks" ;
4
+ import { createEmailRequest } from "@api/emails/route.client" ;
5
+ import { Spinner } from "./skeleton" ;
4
6
5
7
const LandingFooter = ( ) => {
6
8
const [ email , setEmail ] = useState < string > ( "" ) ;
7
- const [ eList , setEList ] = useState < Set < string > > ( new Set < string > ( ) ) ;
8
9
9
10
const [ buttonText , setButtonText ] = useState < string > ( "Join E-List" ) ;
10
11
const [ buttonStyle , setButtonStyle ] = useState < string > (
11
12
"bg-dark-teal hover:-translate-y-0.5"
12
13
) ;
13
14
14
- function resetButton ( target : FocusEvent < HTMLInputElement > ) {
15
- setButtonStyle ( "bg-dark-teal hover:-translate-y-0.5" ) ;
16
- setButtonText ( "Join E-List" ) ;
17
- }
18
-
19
- const onEmailSubmit = async ( event : FormEvent ) => {
20
- event . preventDefault ( ) ;
21
-
22
- // if box isn't empty and content is a valid email, add it to the set
23
-
24
- if ( email ) {
25
- eList . add ( email ) ;
26
- setEList ( eList ) ;
27
-
28
- // use SendGrid 'subscribe' route to send a test email
29
- const res = await fetch ( "/api/emails" , {
30
- body : JSON . stringify ( { email : email } ) ,
31
- method : "POST" ,
32
- } ) ;
33
-
34
- const responseObject = EmailResponse . parse ( await res . json ( ) ) ;
35
- if ( responseObject . code == "INVALID_EMAIL" ) {
15
+ const { fetching, fn : throttleCreateEmailRequest } = useApiThrottle ( {
16
+ fn : createEmailRequest ,
17
+ callback : ( responseObject ) => {
18
+ if ( responseObject . code === "INVALID_EMAIL" ) {
36
19
setButtonText ( "Invalid Email" ) ;
37
20
setButtonStyle ( "bg-tag-rust" ) ; // no hover
38
21
return ;
39
- } else if ( responseObject . code == "DUPLICATE_EMAIL" ) {
22
+ } else if ( responseObject . code === "DUPLICATE_EMAIL" ) {
40
23
setButtonText ( "Duplicate Email" ) ;
41
24
setButtonStyle ( "bg-tag-rust" ) ; // no hover
42
25
return ;
43
26
}
44
27
45
28
setButtonText ( "Subscribed!" ) ;
46
29
setButtonStyle ( "bg-dark-teal" ) ; // no hover
30
+ } ,
31
+ } ) ;
32
+
33
+ function resetButton ( target : FocusEvent < HTMLInputElement > ) {
34
+ setButtonStyle ( "bg-dark-teal hover:-translate-y-0.5" ) ;
35
+ setButtonText ( "Join E-List" ) ;
36
+ }
37
+
38
+ const onEmailSubmit = async ( event : FormEvent ) => {
39
+ event . preventDefault ( ) ;
40
+
41
+ if ( email !== "" ) {
42
+ await throttleCreateEmailRequest ( { body : { email : email } } ) ;
47
43
} else {
48
44
setButtonText ( "Invalid Email" ) ;
49
45
setButtonStyle ( "bg-tag-rust" ) ; // no hover
@@ -66,29 +62,33 @@ const LandingFooter = () => {
66
62
is doing:
67
63
</ p >
68
64
</ div >
69
- < form
70
- className = "mx-auto flex w-3/4 flex-col justify-center gap-[20px] md:flex-row"
71
- method = "post"
72
- onSubmit = { onEmailSubmit }
73
- autoComplete = "off"
74
- >
75
- < input
76
- className = "text-gray relative h-[60px] rounded-xl bg-white px-4
77
- placeholder-dark-gray shadow-md focus:border-dark-teal focus:outline-none md:w-3/4"
78
- name = "email"
79
- placeholder = "Enter your e-mail address"
80
- onFocus = { resetButton }
81
- onChange = { ( event ) => {
82
- setEmail ( event . target . value ) ;
83
- } }
84
- />
85
- < button
86
- className = { `${ buttonStyle } w-auto rounded-xl px-4 py-4 duration-150` }
87
- type = "submit"
65
+ { ! fetching ? (
66
+ < form
67
+ className = "mx-auto flex w-3/4 flex-col justify-center gap-[20px] md:flex-row"
68
+ method = "post"
69
+ onSubmit = { onEmailSubmit }
70
+ autoComplete = "off"
88
71
>
89
- < span className = "align-center text-white" > { buttonText } </ span >
90
- </ button >
91
- </ form >
72
+ < input
73
+ className = "text-gray relative h-[60px] rounded-xl bg-white px-4
74
+ placeholder-dark-gray shadow-md focus:border-dark-teal focus:outline-none md:w-3/4"
75
+ name = "email"
76
+ placeholder = "Enter your e-mail address"
77
+ onFocus = { resetButton }
78
+ onChange = { ( event ) => {
79
+ setEmail ( event . target . value ) ;
80
+ } }
81
+ />
82
+ < button
83
+ className = { `${ buttonStyle } w-auto rounded-xl px-4 py-4 duration-150` }
84
+ type = "submit"
85
+ >
86
+ < span className = "align-center text-white" > { buttonText } </ span >
87
+ </ button >
88
+ </ form >
89
+ ) : (
90
+ < Spinner />
91
+ ) }
92
92
</ div >
93
93
) ;
94
94
} ;
0 commit comments