-
Notifications
You must be signed in to change notification settings - Fork 24
Update BookEvent.tsx #61
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,8 @@ | ||||||||||||||||||||||||||||||||||||
| 'use client'; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import {useState} from "react"; | ||||||||||||||||||||||||||||||||||||
| const [error, setError] = useState<string | null>(null); | ||||||||||||||||||||||||||||||||||||
| const [isSubmitting, setIsSubmitting] = useState(false); | ||||||||||||||||||||||||||||||||||||
| import {createBooking} from "@/lib/actions/booking.actions"; | ||||||||||||||||||||||||||||||||||||
| import posthog from "posthog-js"; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -9,40 +11,52 @@ const BookEvent = ({ eventId, slug }: { eventId: string, slug: string;}) => { | |||||||||||||||||||||||||||||||||||
| const [submitted, setSubmitted] = useState(false); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const handleSubmit = async (e: React.FormEvent) => { | ||||||||||||||||||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||||||||||||||||||
| setIsSubmitting(true); | ||||||||||||||||||||||||||||||||||||
| setError(null); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const { success } = await createBooking({ eventId, slug, email }); | ||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||
| const response = await createBooking({ eventId, slug, email }); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if(success) { | ||||||||||||||||||||||||||||||||||||
| setSubmitted(true); | ||||||||||||||||||||||||||||||||||||
| posthog.capture('event_booked', { eventId, slug, email }) | ||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||
| console.error('Booking creation failed') | ||||||||||||||||||||||||||||||||||||
| posthog.captureException('Booking creation failed') | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if (response.success) { | ||||||||||||||||||||||||||||||||||||
| setSubmitted(true); | ||||||||||||||||||||||||||||||||||||
| posthog.capture('event_booked', { eventId, slug, email }); | ||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||
| setError(response.error || "An unexpected error occurred. Please try again."); | ||||||||||||||||||||||||||||||||||||
| posthog.captureException('Booking creation failed'); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||
| setError("A network error occurred. Please try again."); | ||||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||||
| setIsSubmitting(false); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||
| <div id="book-event"> | ||||||||||||||||||||||||||||||||||||
| {submitted ? ( | ||||||||||||||||||||||||||||||||||||
| <p className="text-sm">Thank you for signing up!</p> | ||||||||||||||||||||||||||||||||||||
| ): ( | ||||||||||||||||||||||||||||||||||||
| <form onSubmit={handleSubmit}> | ||||||||||||||||||||||||||||||||||||
| <div> | ||||||||||||||||||||||||||||||||||||
| <label htmlFor="email">Email Address</label> | ||||||||||||||||||||||||||||||||||||
| <input | ||||||||||||||||||||||||||||||||||||
| type="email" | ||||||||||||||||||||||||||||||||||||
| value={email} | ||||||||||||||||||||||||||||||||||||
| onChange={(e) => setEmail(e.target.value)} | ||||||||||||||||||||||||||||||||||||
| id="email" | ||||||||||||||||||||||||||||||||||||
| placeholder="Enter your email address" | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| <button type="submit" className="button-submit">Submit</button> | ||||||||||||||||||||||||||||||||||||
| </form> | ||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| export default BookEvent | ||||||||||||||||||||||||||||||||||||
| {submitted ? ( | ||||||||||||||||||||||||||||||||||||
| <p className="text-sm">Thank you for signing up!</p> | ||||||||||||||||||||||||||||||||||||
| ) : ( | ||||||||||||||||||||||||||||||||||||
| <form onSubmit={handleSubmit}> | ||||||||||||||||||||||||||||||||||||
| <div> | ||||||||||||||||||||||||||||||||||||
| <label htmlFor="email">Email Address</label> | ||||||||||||||||||||||||||||||||||||
| <input | ||||||||||||||||||||||||||||||||||||
| type="email" | ||||||||||||||||||||||||||||||||||||
| value={email} | ||||||||||||||||||||||||||||||||||||
| onChange={(e) => setEmail(e.target.value)} | ||||||||||||||||||||||||||||||||||||
| id="email" | ||||||||||||||||||||||||||||||||||||
| placeholder="Enter your email address" | ||||||||||||||||||||||||||||||||||||
| disabled={isSubmitting} // 1. Freeze input when submitting | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding The email input has 📋 Suggested enhancement <input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
id="email"
placeholder="Enter your email address"
+ required
disabled={isSubmitting}
/>📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||
| {/* 2. Show the red error message under the input if an error occurs */} | ||||||||||||||||||||||||||||||||||||
| {error && <p className="text-red-500 text-xs mt-1">{error}</p>} | ||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| {/* 3. Disable the button and change text dynamically */} | ||||||||||||||||||||||||||||||||||||
| <button type="submit" className="button-submit" disabled={isSubmitting}> | ||||||||||||||||||||||||||||||||||||
| {isSubmitting ? "Submitting..." : "Submit"} | ||||||||||||||||||||||||||||||||||||
| </button> | ||||||||||||||||||||||||||||||||||||
| </form> | ||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||
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.
Critical: React hooks called outside component function.
Lines 4-5 declare
useStatehooks at the module/top level, before theBookEventcomponent function is defined on line 9. This violates React's Rules of Hooks and will cause an immediate runtime crash with "Invalid hook call. Hooks can only be called inside the body of a function component."Additionally, having imports on lines 6-7 after executable statements (lines 4-5) is invalid JavaScript syntax.
These hooks must be moved inside the
BookEventcomponent function body.🔧 Required fix: Move hooks inside component
'use client'; import {useState} from "react"; -const [error, setError] = useState<string | null>(null); -const [isSubmitting, setIsSubmitting] = useState(false); import {createBooking} from "`@/lib/actions/booking.actions`"; import posthog from "posthog-js"; const BookEvent = ({ eventId, slug }: { eventId: string, slug: string;}) => { const [email, setEmail] = useState(''); const [submitted, setSubmitted] = useState(false); + const [error, setError] = useState<string | null>(null); + const [isSubmitting, setIsSubmitting] = useState(false); const handleSubmit = async (e: React.FormEvent) => {🤖 Prompt for AI Agents