|
1
|
"use client" |
|
2
|
|
|
3
|
import clsx from "clsx" |
|
4
|
import { useEffect, useRef, useState } from "react" |
|
5
|
import { subscribeToNewsletter } from "../../../utils/subscribe" |
|
6
|
|
|
7
|
type HomepageNewsletterFormProps = { |
|
8
|
className?: string |
|
9
|
autoFocus?: boolean |
|
10
|
} |
|
11
|
|
|
12
|
const HomepageNewsletterForm = ({ |
|
13
|
className, |
|
14
|
autoFocus = false, |
|
15
|
}: HomepageNewsletterFormProps) => { |
|
16
|
const inputRef = useRef<HTMLInputElement>(null) |
|
17
|
const [email, setEmail] = useState("") |
|
18
|
const [message, setMessage] = useState<{ |
|
19
|
type: "error" | "success" |
|
20
|
text: string |
|
21
|
} | null>(null) |
|
22
|
const [loading, setLoading] = useState(false) |
|
23
|
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { |
|
24
|
e.preventDefault() |
|
25
|
setLoading(true) |
|
26
|
const { success, message } = await subscribeToNewsletter(email) |
|
27
|
|
|
28
|
if (success) { |
|
29
|
setMessage({ |
|
30
|
type: "success", |
|
31
|
text: "You've been subscribed to the newsletter!", |
|
32
|
}) |
|
33
|
setEmail("") |
|
34
|
} else { |
|
35
|
setMessage({ |
|
36
|
type: "error", |
|
37
|
text: message || "An error occurred. Please try again later.", |
|
38
|
}) |
|
39
|
} |
|
40
|
|
|
41
|
setLoading(false) |
|
42
|
} |
|
43
|
useEffect(() => { |
|
44
|
if (autoFocus) { |
|
45
|
inputRef.current?.focus() |
|
46
|
} |
|
47
|
}, [autoFocus]) |
|
48
|
useEffect(() => { |
|
49
|
if (message?.type === "error") { |
|
50
|
return |
|
51
|
} |
|
52
|
|
|
53
|
const timer = setTimeout(() => { |
|
54
|
setMessage(null) |
|
55
|
}, 5000) |
|
56
|
|
|
57
|
return () => clearTimeout(timer) |
|
58
|
}, [message]) |
|
59
|
|
|
60
|
return ( |
|
61
|
<div className={clsx("flex flex-col gap-0.5", className)}> |
|
62
|
<form |
|
63
|
className={clsx( |
|
64
|
"py-[10px] px-0.75 flex gap-0.75 bg-medusa-bg-base", |
|
65
|
"shadow-elevation-card-rest dark:shadow-elevation-card-rest-dark", |
|
66
|
"rounded-sm w-full justify-between" |
|
67
|
)} |
|
68
|
onSubmit={handleSubmit} |
|
69
|
> |
|
70
|
<input |
|
71
|
className={clsx( |
|
72
|
"appearance-none text-base lg:text-compact-medium", |
|
73
|
"placeholder:text-medusa-fg-muted bg-transparent", |
|
74
|
"focus:outline-none flex-1 min-w-0" |
|
75
|
)} |
|
76
|
ref={inputRef} |
|
77
|
name="email" |
|
78
|
type="email" |
|
79
|
placeholder="Enter your email" |
|
80
|
value={email} |
|
81
|
onChange={(e) => setEmail(e.target.value)} |
|
82
|
/> |
|
83
|
<button |
|
84
|
type="submit" |
|
85
|
className={clsx( |
|
86
|
"appearance-none bg-transparent p-0 text-compact-medium-plus", |
|
87
|
"text-medusa-fg-base disabled:text-medusa-fg-disabled flex-shrink-0 whitespace-nowrap", |
|
88
|
"hover:text-medusa-fg-subtle cursor-pointer" |
|
89
|
)} |
|
90
|
disabled={loading} |
|
91
|
> |
|
92
|
Subscribe |
|
93
|
</button> |
|
94
|
</form> |
|
95
|
{message && <span className="text-medusa-fg-subtle">{message.text}</span>} |
|
96
|
</div> |
|
97
|
) |
|
98
|
} |
|
99
|
|
|
100
|
export default HomepageNewsletterForm |