diff --git a/app/(default)/pbctf/page.tsx b/app/(default)/pbctf/page.tsx index f0749f7..a5c92c4 100644 --- a/app/(default)/pbctf/page.tsx +++ b/app/(default)/pbctf/page.tsx @@ -10,13 +10,13 @@ import { useRouter } from "next/navigation"; const PBCTFRegisterPage = () => { const router = useRouter(); - // useEffect(() => { - // onAuthStateChanged(auth, (user) => { - // if (!user) { - // router.push("/login"); - // } - // }); - // }, [router]); + useEffect(() => { + onAuthStateChanged(auth, (user) => { + if (!user) { + router.push("/login"); + } + }); + }, [router]); return (
diff --git a/components/forms/pbctfForm.tsx b/components/forms/pbctfForm.tsx index ab442e4..ffa847f 100644 --- a/components/forms/pbctfForm.tsx +++ b/components/forms/pbctfForm.tsx @@ -31,7 +31,9 @@ type FormData = { const PBCTFForm: React.FC = () => { const [isSuccess, setSuccess] = useState(false); - const [participationType, setParticipationType] = useState<"solo" | "duo">("solo"); + const [participationType, setParticipationType] = useState<"solo" | "duo">( + "solo" + ); const [isSubmitting, setIsSubmitting] = useState(false); const [usnError, setUsnError] = useState(null); const [token, setToken] = useState(); @@ -73,20 +75,38 @@ const PBCTFForm: React.FC = () => { }; const checkUsnUniqueness = async (usn: string): Promise => { - const q = query(collection(db, "pbctf_registrations"), - where("participant1.usn", "==", usn)); + const q = query( + collection(db, "pbctf_registrations"), + where("participant1.usn", "==", usn) + ); const querySnapshot = await getDocs(q); - + if (!querySnapshot.empty) { return false; } - const q2 = query(collection(db, "pbctf_registrations"), - where("participant2.usn", "==", usn)); + const q2 = query( + collection(db, "pbctf_registrations"), + where("participant2.usn", "==", usn) + ); const querySnapshot2 = await getDocs(q2); return querySnapshot2.empty; }; + useEffect(() => { + const getRecaptcha = async () => { + grecaptcha.enterprise.ready(async () => { + const token = await grecaptcha.enterprise.execute( + process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY + ); + + if (token) { + setTokenFunc(token); + } + }); + }; + getRecaptcha(); + }, []); const onSubmit: SubmitHandler = async (data) => { if (isSubmitting) return; @@ -94,55 +114,55 @@ const PBCTFForm: React.FC = () => { setUsnError(null); try { - - grecaptcha.enterprise.ready(async () => { - const token = await grecaptcha.enterprise.execute( - process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY - ); - setTokenFunc(token); - }); - const recaptcha_token = token; - const response = await fetch("/api/registration/pbctf", { - method: "POST", - body: JSON.stringify({recaptcha_token}), - }); + if (token) { + const response = await fetch("/api/registration/pbctf", { + method: "POST", + body: JSON.stringify({ recaptcha_token }), + }); - const res = await response.json(); + const res = await response.json(); - if (!response.ok || res.error) { - toast.error(res.message); - return; - } - - // Check if USNs are the same for duo participation - if (data.participationType === "duo" && data.participant2 && data.participant1.usn === data.participant2.usn) { - setUsnError("USNs for Participant 1 and Participant 2 cannot be the same"); - setIsSubmitting(false); - return; - } + if (!response.ok || res.error) { + toast.error(res.message); + return; + } - // Check USN uniqueness for participant1 - const isUnique1 = await checkUsnUniqueness(data.participant1.usn); - if (!isUnique1) { - setUsnError("USN for Participant 1 already exists"); - setIsSubmitting(false); - return; - } + // Check if USNs are the same for duo participation + if ( + data.participationType === "duo" && + data.participant2 && + data.participant1.usn === data.participant2.usn + ) { + setUsnError( + "USNs for Participant 1 and Participant 2 cannot be the same" + ); + setIsSubmitting(false); + return; + } - // Check USN uniqueness for participant2 if it exists - if (data.participationType === "duo" && data.participant2) { - const isUnique2 = await checkUsnUniqueness(data.participant2.usn); - if (!isUnique2) { - setUsnError("USN for Participant 2 already exists"); + // Check USN uniqueness for participant1 + const isUnique1 = await checkUsnUniqueness(data.participant1.usn); + if (!isUnique1) { + setUsnError("USN for Participant 1 already exists"); setIsSubmitting(false); return; } - } - // If all checks pass, submit the form - await addDoc(collection(db, "pbctf_registrations"), data); - setSuccess(true); + // Check USN uniqueness for participant2 if it exists + if (data.participationType === "duo" && data.participant2) { + const isUnique2 = await checkUsnUniqueness(data.participant2.usn); + if (!isUnique2) { + setUsnError("USN for Participant 2 already exists"); + setIsSubmitting(false); + return; + } + } + + // If all checks pass, submit the form + await addDoc(collection(db, "pbctf_registrations"), data); + setSuccess(true); + } } catch (error) { console.error("Error submitting form:", error); } finally { @@ -388,4 +408,4 @@ const PBCTFForm: React.FC = () => { ); }; -export default PBCTFForm; \ No newline at end of file +export default PBCTFForm; diff --git a/components/forms/recruitmentForm.tsx b/components/forms/recruitmentForm.tsx index e7f6b97..63096fe 100644 --- a/components/forms/recruitmentForm.tsx +++ b/components/forms/recruitmentForm.tsx @@ -1,7 +1,7 @@ "use client"; import "../../app/css/additional-styles/utility-patterns.css"; import "../../app/css/additional-styles/theme.css"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { useForm, SubmitHandler } from "react-hook-form"; import { years, branches } from "@/lib/constants/dropdownOptions"; import Success from "./success"; @@ -40,34 +40,42 @@ const RecruitmentForm: React.FC = () => { setDisplay(true); }; - const onSubmit: SubmitHandler = async (data: any) => { - - if (isSubmitting) return; - setIsSubmitting(true); - try { + useEffect(() => { + const getRecaptcha = async () => { grecaptcha.enterprise.ready(async () => { const token = await grecaptcha.enterprise.execute( process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY ); - setTokenFunc(token); + + if (token) { + setTokenFunc(token); + } }); + }; + getRecaptcha(); + }, []); + const onSubmit: SubmitHandler = async (data: any) => { + if (isSubmitting) return; + setIsSubmitting(true); + try { data.recaptcha_token = token; + if (token) { + const response = await fetch("/api/registration/recruitment", { + method: "POST", + body: JSON.stringify(data), + }); - const response = await fetch("/api/registration/recruitment", { - method: "POST", - body: JSON.stringify(data), - }); + const res = await response.json(); - const res = await response.json(); + if (!response.ok || res.error) { + console.log(response.json); + toast.error(res.message); + return; + } - if (!response.ok || res.error) { - console.log(response.json); - toast.error(res.message); - return; + setSuccess(true); } - - setSuccess(true); } catch (error) { console.error("Error submitting form:", error); toast.error(getErrorMessage(error));