Skip to content

Commit

Permalink
add recruitment form frontend and backend
Browse files Browse the repository at this point in the history
  • Loading branch information
kamini08 committed Aug 17, 2024
1 parent f062453 commit c9233d7
Show file tree
Hide file tree
Showing 13 changed files with 578 additions and 6 deletions.
77 changes: 77 additions & 0 deletions app/(default)/api/registration/recruitment/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { db } from "@/Firebase";
import { recruitValidate } from "@/lib/utils";
import { addDoc, collection, getDocs } from "firebase/firestore";
import { NextResponse } from "next/server";

// Verify reCAPTCHA Token
async function verifyRecaptcha(token: string) {
const secretKey = process.env.RECAPTCHA_SECRET_KEY;
const response = await fetch(
`https://www.google.com/recaptcha/api/siteverify`,
{
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
secret: secretKey || "",
response: token,
}),
}
);
const data = await response.json();
return data.success;
}

// Add a new registration
export async function POST(request: Request) {
const data = await request.json();
const recaptchaToken = data.recaptchaToken; // Extract the reCAPTCHA token from the request

// Verify the reCAPTCHA token
const isRecaptchaValid = await verifyRecaptcha(recaptchaToken);

if (!isRecaptchaValid) {
return NextResponse.json({
message: "reCAPTCHA verification failed",
error: true,
});
}

// Validate the data
const val = recruitValidate(data);

if (!Array.isArray(data)) {
return NextResponse.json({
message: "Expected an array of JSON objects",
error: true,
});
}

if (val.error) {
return NextResponse.json({ message: "Validation error", error: val.error });
}

try {
// Save to Firebase
const docRef = await addDoc(collection(db, "recruitment2024"), data);
console.log("Document written with ID: ", docRef.id);
} catch (error) {
console.error(error);
return NextResponse.json({ message: "An error occurred", error });
}
// Return a response
return NextResponse.json({ message: "Registration successful", data });
}

// Get all registrations
export async function GET() {
try {
// Get all registrations in recruitment2024 collection
const querySnapshot = await getDocs(collection(db, "recruitment2024"));
// Map the data to get only the data
const data = querySnapshot.docs.map((doc) => doc.data());
return NextResponse.json({ data });
} catch (error) {
console.error(error);
return NextResponse.json({ message: "An error occurred", error });
}
}
26 changes: 26 additions & 0 deletions app/(default)/api/registration/sih/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,35 @@ import { sihValidate } from '@/lib/utils';
import { addDoc, collection, getDocs } from 'firebase/firestore';
import { NextResponse } from 'next/server';


// Verify reCAPTCHA Token
async function verifyRecaptcha(token: string) {
const secretKey = process.env.RECAPTCHA_SECRET_KEY;
const response = await fetch(`https://www.google.com/recaptcha/api/siteverify`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
secret: secretKey || '',
response: token,
}),
});
const data = await response.json();
return data.success;
}


// Add a new registration
export async function POST(request: Request) {
const data = await request.json();
const recaptchaToken = data.recaptchaToken; // Extract the reCAPTCHA token from the request

// Verify the reCAPTCHA token
const isRecaptchaValid = await verifyRecaptcha(recaptchaToken);

if (!isRecaptchaValid) {
return NextResponse.json({ message: 'reCAPTCHA verification failed', error: true });
}

// Validate the data
const val = sihValidate(data);

Expand Down
29 changes: 29 additions & 0 deletions app/(default)/recruitment/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

import RecruitmentForm from "@/components/forms/recruitmentForm";
import DotPattern from "@/components/magicui/dot-pattern";
import "../../css/additional-styles/form.css";
import { cn } from "@/lib/utils";
const RegisterPage = () => {
return (

<div className="w-50 mt-16 mx-auto flex flex-col items-center justify-center">

<div className="form-container my-2">
<RecruitmentForm />
</div>
<DotPattern
width={20}
height={20}
cx={1}
cy={1}
cr={1}
className={cn(
"[mask-image:linear-gradient(to_bottom_right,white,transparent,transparent)] "
)}
/>
</div>

);
};

export default RegisterPage;
2 changes: 1 addition & 1 deletion app/(default)/sihregistration/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// pages/register.tsx

import SIHRegistrationForm from "@/components/forms/sihForm";
import { FormProvider } from "@/components/forms/formContext";
import DotPattern from "@/components/magicui/dot-pattern";
Expand Down
2 changes: 1 addition & 1 deletion components/forms/formContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const FormProvider: React.FC<{ children: React.ReactNode }> = ({
team_info: {
team_name: "",
college_name: "DSCE",
team_size: 5,
team_size: 6,
team_leader: {
name: "",
email: "",
Expand Down
33 changes: 33 additions & 0 deletions components/forms/reCaptcha.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect } from "react";

const Recaptcha = ({ onChange }: { onChange: (token: string | null) => void }) => {
useEffect(() => {
const loadRecaptcha = () => {
if (window.grecaptcha) {
window.grecaptcha.ready(() => {
window.grecaptcha.execute(process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY || '', { action: 'submit' }).then((token: string) => {
onChange(token);
});
});
}
};

const script = document.createElement("script");
script.src = `https://www.google.com/recaptcha/api.js?render=${process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY}`;
script.async = true;
script.defer = true;
script.onload = loadRecaptcha;
document.head.appendChild(script);

return () => {
document.head.removeChild(script);
};
}, [onChange]);

return null;
};

export default Recaptcha;



Loading

0 comments on commit c9233d7

Please sign in to comment.