-
Notifications
You must be signed in to change notification settings - Fork 138
Add error handling for newsletter subscription and enhance footer styles #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,10 @@ | ||
| import React, {type ReactNode, useState, useEffect} from 'react'; | ||
| import React, { type ReactNode, useState, useEffect } from "react"; | ||
| import Link from "@docusaurus/Link"; | ||
| import type {Props} from '@theme/Footer/Layout'; | ||
| import './enhanced-footer.css'; | ||
| import Counter from './Counter'; | ||
| import type { Props } from "@theme/Footer/Layout"; | ||
| import "./enhanced-footer.css"; | ||
| import Counter from "./Counter"; | ||
| import { createPortal } from "react-dom"; | ||
|
|
||
|
|
||
| // Dynamic stats interface | ||
| interface FooterStats { | ||
| activeUsers: string; | ||
|
|
@@ -14,6 +13,8 @@ interface FooterStats { | |
| supportHours: string; | ||
| } | ||
|
|
||
| const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
|
|
||
| export default function FooterLayout({ | ||
| style, | ||
| links, | ||
|
|
@@ -22,14 +23,15 @@ export default function FooterLayout({ | |
| }: Props): ReactNode { | ||
| const [currentYear, setCurrentYear] = useState(new Date().getFullYear()); | ||
| const [stats, setStats] = useState<FooterStats>({ | ||
| activeUsers: '50K+', | ||
| tutorials: '200+', | ||
| successRate: '95%', | ||
| supportHours: '24/7' | ||
| activeUsers: "50K+", | ||
| tutorials: "200+", | ||
| successRate: "95%", | ||
| supportHours: "24/7", | ||
| }); | ||
| const [email, setEmail] = useState(''); | ||
| const [email, setEmail] = useState(""); | ||
| const [isSubscribed, setIsSubscribed] = useState(false); | ||
| const [showToast, setShowToast] = useState(false); | ||
| const [error, setError] = useState(""); | ||
|
|
||
| useEffect(() => { | ||
| // Simulate real-time stats updates | ||
|
|
@@ -44,36 +46,46 @@ export default function FooterLayout({ | |
| activeUsers: `${Math.floor((baseUsers + randomGrowth) / 1000)}K+`, | ||
| tutorials: `${baseTutorials + Math.floor(randomGrowth / 10)}+`, | ||
| successRate: `${95 + Math.floor(Math.random() * 3)}%`, | ||
| supportHours: '24/7' | ||
| supportHours: "24/7", | ||
| }); | ||
| } catch (error) { | ||
| console.log('Using fallback stats'); | ||
| console.log("Using fallback stats"); | ||
| } | ||
| }; | ||
|
|
||
| fetchStats(); | ||
| const interval = setInterval(fetchStats, 30000); // Update every 30 seconds | ||
|
|
||
| return () => clearInterval(interval); | ||
| }, []); | ||
|
|
||
| const handleSubscribe = (e: React.FormEvent) => { | ||
| e.preventDefault(); | ||
| if (email) { | ||
| setIsSubscribed(true); | ||
| setShowToast(true); | ||
|
|
||
| // Hide toast after 3 seconds | ||
| setTimeout(() => { | ||
| setShowToast(false); | ||
| }, 3000); | ||
|
|
||
| // Reset form after 3 seconds | ||
| setTimeout(() => { | ||
| setIsSubscribed(false); | ||
| setEmail(''); | ||
| }, 3000); | ||
|
|
||
| if (!email) { | ||
| setError("Email is required"); | ||
| return; | ||
| } | ||
|
|
||
| if (!emailRegex.test(email)) { | ||
| setError("Please enter a valid email address"); | ||
| return; | ||
| } | ||
|
|
||
| setError(""); | ||
| setIsSubscribed(true); | ||
| setShowToast(true); | ||
|
|
||
| // Hide toast after 3 seconds | ||
| setTimeout(() => { | ||
| setShowToast(false); | ||
| }, 3000); | ||
|
|
||
| // Reset form after 3 seconds | ||
| setTimeout(() => { | ||
| setIsSubscribed(false); | ||
| setEmail(""); | ||
| }, 3000); | ||
| }; | ||
|
|
||
| return ( | ||
|
|
@@ -343,7 +355,10 @@ export default function FooterLayout({ | |
| placeholder="[email protected]" | ||
| className="newsletter-input" | ||
| value={email} | ||
| onChange={(e) => setEmail(e.target.value)} | ||
| onChange={(e) => { | ||
| setEmail(e.target.value); | ||
| setError(""); | ||
| }} | ||
| required | ||
| /> | ||
| <button | ||
|
|
@@ -355,6 +370,7 @@ export default function FooterLayout({ | |
| > | ||
| {isSubscribed ? "✓ Subscribed!" : "Subscribe Now →"} | ||
| </button> | ||
| {error && <p className="error-text">{error}</p>} | ||
| </form> | ||
| <div className="newsletter-stats"> | ||
| <span className="newsletter-stat"> | ||
|
|
@@ -461,4 +477,4 @@ export default function FooterLayout({ | |
| </div> | ||
| </footer> | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message is not being cleared when the user starts typing in the email field. Consider adding an onChange handler that clears the error state to provide immediate feedback when the user begins correcting their input.