Skip to content

Commit a4d9b5c

Browse files
removed static backend uri in newsletter section.
1 parent b693598 commit a4d9b5c

2 files changed

Lines changed: 13 additions & 53 deletions

File tree

client/src/components/HomePageComponents/NewsLetter.jsx

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,15 @@ import { FaPaperPlane } from "react-icons/fa";
44
import { gsap } from "gsap";
55
import { ScrollTrigger } from "gsap/ScrollTrigger";
66
import { motion } from "framer-motion";
7+
import NewsletterSubscribeInput from "../NewsletterSubscribeInput";
78

89
gsap.registerPlugin(ScrollTrigger);
910

1011
const NewsLetter = () => {
1112
const { theme } = useTheme();
1213
const isDark = theme === 'dark';
13-
const [email, setEmail] = useState("");
1414
const sectionRef = useRef(null);
1515

16-
const handleSubmit = (e) => {
17-
e.preventDefault();
18-
// Here you would typically handle the newsletter subscription
19-
alert(`Thank you for subscribing with: ${email}`);
20-
setEmail("");
21-
};
22-
2316
useEffect(() => {
2417
if (!sectionRef.current) return;
2518

@@ -55,32 +48,7 @@ const NewsLetter = () => {
5548
Subscribe to our newsletter and never miss new courses and learning opportunities
5649
</p>
5750

58-
<form
59-
onSubmit={handleSubmit}
60-
className="flex flex-col sm:flex-row gap-4 max-w-xl mx-auto"
61-
>
62-
<input
63-
type="email"
64-
placeholder="Enter your email"
65-
value={email}
66-
onChange={(e) => setEmail(e.target.value)}
67-
required
68-
className={`
69-
flex-grow px-5 py-3 rounded-2xl shadow-md bg-transparent border-none outline-none focus:ring-2 focus:ring-primary focus:ring-offset-2
70-
${isDark ? 'text-dark-text-primary placeholder-dark-text-secondary' : 'text-light-text-primary placeholder-light-text-secondary'}
71-
transition-all duration-300
72-
`}
73-
/>
74-
<motion.button
75-
type="submit"
76-
whileHover={{ scale: 1.05 }}
77-
whileTap={{ scale: 0.98 }}
78-
className="bg-primary hover:bg-primary-dark text-white py-3 px-6 rounded-xl transition-all duration-300 flex items-center justify-center gap-2 font-semibold shadow-lg"
79-
>
80-
<span>Subscribe</span>
81-
<FaPaperPlane />
82-
</motion.button>
83-
</form>
51+
<NewsletterSubscribeInput isDark={isDark} />
8452
</div>
8553
</div>
8654
</section>

client/src/components/NewsletterSubscribeInput.jsx

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import { useState } from "react";
22
import PropTypes from "prop-types";
3+
import { useAuth } from "../store/auth";
4+
import { toast } from "react-toastify";
35

46
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
57

68
function NewsletterSubscribeInput({ isDark }) {
9+
const {API}=useAuth();
710
const [email, setEmail] = useState("");
8-
const [status, setStatus] = useState({ type: "idle", message: "" });
911
const [loading, setLoading] = useState(false);
1012

1113
const handleSubscribe = async () => {
1214
if (!EMAIL_REGEX.test(email)) {
13-
setStatus({ type: "error", message: "Please enter a valid email." });
15+
toast.error("Please enter a valid email.");
1416
return;
1517
}
1618
try {
1719
setLoading(true);
18-
setStatus({ type: "idle", message: "" });
1920
const res = await fetch(
20-
"http://localhost:5050/api/newsletter/subscribe",
21+
`${API}/api/newsletter/subscribe`,
2122
{
2223
method: "POST",
2324
headers: { "Content-Type": "application/json" },
@@ -28,13 +29,13 @@ function NewsletterSubscribeInput({ isDark }) {
2829
if (!res.ok || data?.success === false) {
2930
throw new Error(data?.message || "Subscription failed");
3031
}
31-
setStatus({ type: "success", message: data?.message || "Subscribed!" });
32+
if(data?.message=="Already subscribed")
33+
toast.info(data?.message || "Subscribed!" )
34+
else
35+
toast.success(data?.message || "Subscribed!" )
3236
setEmail("");
3337
} catch (err) {
34-
setStatus({
35-
type: "error",
36-
message: err.message || "Something went wrong",
37-
});
38+
toast.error(err?.message || "Something went wrong");
3839
} finally {
3940
setLoading(false);
4041
}
@@ -59,21 +60,12 @@ function NewsletterSubscribeInput({ isDark }) {
5960
<button
6061
onClick={handleSubscribe}
6162
disabled={loading}
62-
className={`bg-primary hover:bg-primary-dark text-white px-4 py-2 rounded-lg transition-colors my-3 mx-1 shadow-md ${
63+
className={`bg-primary hover:bg-primary-dark text-white px-4 py-2 rounded-lg transition-colors shadow-md ${
6364
loading ? "opacity-70 cursor-not-allowed" : ""
6465
}`}
6566
>
6667
{loading ? "Subscribing..." : "Subscribe"}
6768
</button>
68-
{status.message && (
69-
<div
70-
className={`text-sm my-auto ${
71-
status.type === "error" ? "text-red-500" : "text-green-500"
72-
}`}
73-
>
74-
{status.message}
75-
</div>
76-
)}
7769
</div>
7870
);
7971
}

0 commit comments

Comments
 (0)