Skip to content

Commit

Permalink
Merge pull request #75 from kamini08/main
Browse files Browse the repository at this point in the history
  • Loading branch information
SkySingh04 authored Sep 22, 2024
2 parents 9dcb568 + 9a1f623 commit 4b1646a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 72 deletions.
14 changes: 7 additions & 7 deletions app/(default)/pbctf/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="min-h-screen flex items-center justify-center py-6 px-4 sm:px-6 lg:px-8 relative overflow-hidden">
Expand Down
114 changes: 67 additions & 47 deletions components/forms/pbctfForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ type FormData = {

const PBCTFForm: React.FC = () => {
const [isSuccess, setSuccess] = useState<boolean>(false);
const [participationType, setParticipationType] = useState<"solo" | "duo">("solo");
const [participationType, setParticipationType] = useState<"solo" | "duo">(
"solo"
);
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
const [usnError, setUsnError] = useState<string | null>(null);
const [token, setToken] = useState<string | null>();
Expand Down Expand Up @@ -73,76 +75,94 @@ const PBCTFForm: React.FC = () => {
};

const checkUsnUniqueness = async (usn: string): Promise<boolean> => {
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<FormData> = async (data) => {
if (isSubmitting) return;
setIsSubmitting(true);
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 {
Expand Down Expand Up @@ -388,4 +408,4 @@ const PBCTFForm: React.FC = () => {
);
};

export default PBCTFForm;
export default PBCTFForm;
44 changes: 26 additions & 18 deletions components/forms/recruitmentForm.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -40,34 +40,42 @@ const RecruitmentForm: React.FC = () => {
setDisplay(true);
};

const onSubmit: SubmitHandler<any> = 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<any> = 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));
Expand Down

0 comments on commit 4b1646a

Please sign in to comment.