diff --git a/src/App.js b/src/App.js index 886134e..a1a1041 100644 --- a/src/App.js +++ b/src/App.js @@ -11,7 +11,8 @@ import ContactForm from "./pages/Contact"; import { DefaultCarousel } from "./components/Carousel/Carousel"; import UserList from "./pages/Users"; import { PostDetailsCard } from "./components/PostDetailsCard"; - +import ConfirmUnsubscribePage from './pages/UnsubscribePage'; +import EmailVerificationPage from "./pages/EmailPage"; function App() { return (
@@ -26,6 +27,8 @@ function App() { } /> } /> } /> + } /> + } /> {/* Private routes */} }> } /> diff --git a/src/components/Navbar/Location.jsx b/src/components/Navbar/Location.jsx index 44ab7f9..73f2aab 100644 --- a/src/components/Navbar/Location.jsx +++ b/src/components/Navbar/Location.jsx @@ -14,5 +14,6 @@ export const useRouteVariables = () => { isSubscribePage: location.pathname === "/subscription", isDetailPage: location.pathname.startsWith("/post/"), hideHomeLink: location.pathname.startsWith("/posts/"), + isEmailPage: location.pathname==="/email", }; }; diff --git a/src/components/Navbar/Navbar.jsx b/src/components/Navbar/Navbar.jsx index aae1db4..f985c41 100644 --- a/src/components/Navbar/Navbar.jsx +++ b/src/components/Navbar/Navbar.jsx @@ -19,6 +19,7 @@ const Navbar = () => { isSubscribePage, isHomePage, isContactPage, + isEmailPage, } = useRouteVariables(); const { setAuth } = useAuth(); @@ -115,7 +116,8 @@ const Navbar = () => { !isAdminPage && !isSubscribePage && location.pathname !== "/contact" && - !isDetailPage && ( + !isDetailPage && + !isEmailPage && ( { + return ( +
+
+ + 📧 + +

¡Verify your email!

+

We have sent you a verification link.

+

Please check your inbox and click the link to continue.

+
+
+ ); +}; + +export default EmailVerificationPage; diff --git a/src/pages/SubscribePage.js b/src/pages/SubscribePage.js index e448856..01fe967 100644 --- a/src/pages/SubscribePage.js +++ b/src/pages/SubscribePage.js @@ -1,11 +1,20 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import axios from "axios"; import { toast } from "react-hot-toast"; import { Link } from "react-router-dom"; +import Cookies from "js-cookie"; +import { useNavigate } from "react-router-dom"; export const SubscriptionPage = () => { const [email, setEmail] = useState(""); + const navigate = useNavigate(); + useEffect(() => { + const storedEmail = Cookies.get("email"); + if (storedEmail) { + setEmail(storedEmail); + } + }, []); const handleSubscribe = async (e) => { e.preventDefault(); @@ -24,7 +33,10 @@ export const SubscriptionPage = () => { if (response.status === 200) { toast.success("Subscription successful!"); - setEmail(""); + } else if (response.status === 202) { + toast("I know you are excited but you are already subscribed!", { + icon: "😃", + }); } else { toast.error("Failed to subscribe"); } @@ -47,15 +59,18 @@ export const SubscriptionPage = () => { try { const response = await axios.post( - "http://localhost:4000/api/users/unsubscribe", + "http://localhost:4000/api/users/email-unsubscribe", { email: email, } ); if (response.status === 200) { - toast.success("Unsubscribe successful!"); - setEmail(""); + navigate("/email"); + } else if (response.status === 202) { + toast("You are no longer subscribed, please stop trying.", { + icon: "🤷‍♂️", + }); } else { toast.error("Failed to unsubscribe"); } @@ -87,9 +102,8 @@ export const SubscriptionPage = () => { Do you want to receive notifications when there is a new post?

- Enter your email address to which you wish to receive notifications - from us. If you are already subscribed and want to unsubscribe, - enter your email and click on the unsubscribe button. + If you subscribe and then want to unsubscribe you have to verify + your email (you can only subscribe your email in session)

@@ -100,6 +114,7 @@ export const SubscriptionPage = () => { required value={email} onChange={(e) => setEmail(e.target.value)} + readOnly={true} className="bg-transparent border-2 rounded-full py-4 px-6 text-[16px] leading-[22.4px] font-light placeholder:text-white text-white" placeholder="E-mail Address" /> diff --git a/src/pages/UnsubscribePage.js b/src/pages/UnsubscribePage.js new file mode 100644 index 0000000..e36d58d --- /dev/null +++ b/src/pages/UnsubscribePage.js @@ -0,0 +1,34 @@ +import React, { useEffect } from "react"; +import axios from "axios"; +import toast from "react-hot-toast"; +const UnsubscribeSuccessPage = () => { + useEffect(() => { + const params = new URLSearchParams(window.location.search); + const email = params.get("email"); + + axios + .post("http://localhost:4000/api/users/unsubscribe", { email }) + .then((response) => {}) + .catch((error) => { + toast.error(error); + }); + }, []); + + return ( +
+
+
+
+ Unsubscribe Successful +
+ +

+ You have been unsubscribed successfully. +

+
+
+
+ ); +}; + +export default UnsubscribeSuccessPage;