Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
SkySingh04 authored Sep 22, 2024
2 parents 0ffd6db + 713bf3f commit 5e63c30
Show file tree
Hide file tree
Showing 11 changed files with 3,443 additions and 269 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
64 changes: 64 additions & 0 deletions app/(default)/api/registration/pbctf/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { db } from "@/Firebase";

import { recruitValidate } from "@/lib/server/utils";

import {
addDoc,
collection,
getDocs,
limit,
query,
where,
} from "firebase/firestore";
import { NextResponse } from "next/server";

// Add a new registration
export async function POST(request: Request) {
const formData = await request.json();
const { recaptcha_token } = formData;


const recaptchaToken = recaptcha_token;

const details = {
event: {
token: recaptchaToken,
siteKey: process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY,
},
};


if (!recaptchaToken) {
return NextResponse.json(
{
message: "reCAPTCHA token not found! Try again",
error: "reCAPTCHA token not found!",
},
{
status: 500,
}
);
}

// Verify the reCATPTCHA token

const recaptchaResponse = await fetch(
`https://recaptchaenterprise.googleapis.com/v1/projects/${process.env.RECAPTCHA_PROJECT}/assessments?key=${process.env.RECAPTCHA_API_KEY}`,
{
method: "POST",
body: JSON.stringify(details),
}
);

const recaptchaResult = await recaptchaResponse.json();
console.log(recaptchaResult.riskAnalysis.score);
if (recaptchaResult.riskAnalysis.score < 0.7) {
return NextResponse.json({
message: "reCAPTCHA validation failed",
error: recaptchaResult["error-codes"],
});
}

// Return a response
return NextResponse.json({ message: "Recaptcha validated!" });
}
33 changes: 21 additions & 12 deletions app/(default)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,49 @@ import EventComponent from "@/components/eventcards";
import Leads from "@/components/leads";
import Achievements from '@/components/achievements';
import Founder from "@/components/founder";
import Announce from "@/components/announcement";
// import Announce from "@/components/announcement";


export default function Home() {
return (
<>
<Hero />
<div className="flex flex-col justify-center items-center py-10 px-5 mb-20">
<SparklesText text="Upcoming Events" className="text-4xl font-bold text-center text-gray-200 mb-4" />
<Image
<SparklesText text="Upcoming Events" className="text-4xl font-bold text-center text-gray-200 mb-4" />
{/* <Image
src={"/images/announce.png"}
alt="sih"
height={400}
width={1100}
className="rounded-3xl mt-20"
/>
/> */}
<Image
src={"/images/pbctf.png"}
alt="pbctf"
height={400}
width={1100}
className="rounded-3xl mt-20"
/>
<div className="flex md:flex-row flex-col justify-center items-center py-10 px-5">
<button className="btn-sm px-5 py-3 text-xl font-bold text-white bg-green-600 mx-3 rounded-xl mt-10">
Registrations Closed
</button>
{/* Add a download button */}
<a href="/Shortlisted.pdf" download>
<a href="/pbctf">
<button className="btn-sm px-5 py-3 text-xl font-bold text-white bg-green-600 mx-3 rounded-xl mt-10">
Register Now
</button>
</a>
{/* Add a download button */}
{/* <a href="/Shortlisted.pdf" download>
<button className="btn-sm px-5 py-3 text-xl font-bold text-white bg-green-600 mx-3 rounded-xl mt-10">
Download Shortlisted Problem Statements
</button>
</a>
</a> */}
</div>
<Announce />
{/* <Announce /> */}
</div>
<Domains />
<Activities />
<Founder />
<Leads />
<Achievements/>
<Achievements />
<EventComponent />
</>
);
Expand Down
14 changes: 7 additions & 7 deletions app/(default)/recruitment/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { useRouter } from "next/navigation";
const RegisterPage = () => {
const router = useRouter();

// useEffect(() => {
// onAuthStateChanged(auth, (user) => {
// if (!user) {
// router.push("/login");
// }
// });
// });\
useEffect(() => {
onAuthStateChanged(auth, (user) => {
if (!user) {
router.push("/login");
}
});
});
// useEffect(() => {
// router.push("/");
// })
Expand Down
2 changes: 1 addition & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function RootLayout({
<head>
<Script
src={`https://www.google.com/recaptcha/enterprise.js?render=${process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY}`}
strategy="lazyOnload" // Set reCAPTCHA ready state when the script is loaded
strategy="beforeInteractive"
/>
</head>
<body
Expand Down
107 changes: 77 additions & 30 deletions components/forms/pbctfForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { collection, addDoc, query, where, getDocs } from "firebase/firestore";
import { db } from "@/Firebase";
import { branches } from "@/lib/constants/dropdownOptions";
import { Press_Start_2P } from "next/font/google";
import toast from "react-hot-toast";

const pressStart2P = Press_Start_2P({
weight: "400",
Expand All @@ -30,9 +31,12 @@ 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>();

const [headingText, setHeadingText] = useState("");
const heading = "Be a Part of PBCTF Register Now!";
Expand Down Expand Up @@ -66,56 +70,99 @@ const PBCTFForm: React.FC = () => {
const watchUsn1 = watch("participant1.usn");
const watchUsn2 = watch("participant2.usn");

const setTokenFunc = (getToken: string) => {
setToken(getToken);
};

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 {
// 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;
}
const recaptcha_token = token;
if (token) {
const response = await fetch("/api/registration/pbctf", {
method: "POST",
body: JSON.stringify({ recaptcha_token }),
});

// Check USN uniqueness for participant1
const isUnique1 = await checkUsnUniqueness(data.participant1.usn);
if (!isUnique1) {
setUsnError("USN for Participant 1 already exists");
setIsSubmitting(false);
return;
}
const res = await response.json();

if (!response.ok || res.error) {
toast.error(res.message);
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 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 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 @@ -156,7 +203,7 @@ const PBCTFForm: React.FC = () => {
</div>
<div className="flex mx-auto items-center mt-6">
<a
href="https://chat.whatsapp.com/GmI6EGCxLInHJ8gclb1ZlS"
href="https://chat.whatsapp.com/HQejGLcEgM1EZFoPTeCUKb"
className="w-full"
>
<button className="w-full px-6 py-3 bg-green-500 hover:bg-green-600 text-white text-lg font-semibold rounded-full transition duration-300 transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-green-400 focus:ring-opacity-50">
Expand Down Expand Up @@ -361,4 +408,4 @@ const PBCTFForm: React.FC = () => {
);
};

export default PBCTFForm;
export default PBCTFForm;
Loading

0 comments on commit 5e63c30

Please sign in to comment.