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 1d16d36 + 7f445d6 commit 0ffd6db
Show file tree
Hide file tree
Showing 39 changed files with 1,743 additions and 619 deletions.
34 changes: 17 additions & 17 deletions app/(default)/achievements/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface Achiever {

function AchievementCard({ achiever }: { achiever: Achiever }) {
const [isAdmin, setIsAdmin] = useState<boolean>(false);

useEffect(() => {
onAuthStateChanged(auth, async (user) => {
if (user) {
Expand All @@ -30,15 +30,15 @@ function AchievementCard({ achiever }: { achiever: Achiever }) {
//check if uuid present in firestore
//if present then set isAdmin to true
const querySnapshot = await getDocs(collection(db, "admin"));
querySnapshot.forEach((doc) => {
if (doc.data().uid === uid) {
setIsAdmin(true);
}
});
querySnapshot.forEach((doc) => {
if (doc.data().uid === uid) {
setIsAdmin(true);
}
});
}
});
});



return (
Expand Down Expand Up @@ -87,13 +87,13 @@ export default function AchievementsPage() {
//check if uuid present in firestore
//if present then set isAdmin to true
const querySnapshot = await getDocs(collection(db, "admin"));
querySnapshot.forEach((doc) => {
if (doc.data().uid === uid) {
setIsAdmin(true);
}
});
querySnapshot.forEach((doc) => {
if (doc.data().uid === uid) {
setIsAdmin(true);
}
});
}
} );
});
});


Expand Down Expand Up @@ -133,7 +133,7 @@ querySnapshot.forEach((doc) => {

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
try{
try {
const formData = new FormData();
formData.append("image", newAchievement.image || "");
formData.append("email", newAchievement.email || "");
Expand Down Expand Up @@ -166,7 +166,7 @@ querySnapshot.forEach((doc) => {
))}
</div>
{isAdmin ? (
<div className="text-center mb-8">
<div className="text-center mb-8">
<button
onClick={() => setIsModalOpen(true)}
className="bg-white text-black py-2 px-4 rounded shadow-lg"
Expand All @@ -175,8 +175,8 @@ querySnapshot.forEach((doc) => {
</button>
</div>
) : null
}
}

{isModalOpen && (
<div className="fixed inset-0 bg-black bg-opacity-80 flex items-center justify-center z-50 p-4">
<div className="bg-black text-white p-8 rounded-lg w-full max-w-md">
Expand Down
119 changes: 104 additions & 15 deletions app/(default)/api/membersData/route.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,118 @@
import { NextResponse } from 'next/server';
import { db } from '@/Firebase';
import { collection, addDoc } from 'firebase/firestore';
import {
collection, addDoc, getDocs, DocumentData,
DocumentSnapshot,
doc,
updateDoc,
deleteDoc
} from 'firebase/firestore';

export async function GET() {
try {
const querySnapshot = await getDocs(collection(db, "members"));
const membersRaw = querySnapshot.docs.map(
(doc: DocumentSnapshot<DocumentData>) => ({
id: doc.id,
...doc.data(),
})
);

const members = membersRaw.map((member: any) => ({
id: member.id,
name: member.name,
domain: member.domain,
company: member.company || '',
year: member.year,
}));

return NextResponse.json(members);
} catch (error) {
console.error("Error fetching members:", error);
return NextResponse.json(
{
error: "An error occurred while fetching members",
details: error,
},
{ status: 500 }
);
}
}

export async function POST(request: Request) {
try {
const data = await request.json();

if (!Array.isArray(data)) {
return NextResponse.json({ message: 'Expected an array of JSON objects', error: true });
if (!data.name || !data.domain || !data.year) {
return NextResponse.json(
{ message: 'Missing required fields', error: true },
{ status: 400 }
);
}

const savedData = [];
for (const item of data) {
// Save each item to Firebase Firestore
const docRef = await addDoc(collection(db, "members"), item);
console.log("Document written with ID: ", docRef.id);
const docRef = await addDoc(collection(db, "members"), data);
console.log("Document written with ID: ", docRef.id);

return NextResponse.json(
{ message: 'Member added successfully', data },
{ status: 201 }
);
} catch (error) {
console.error('Error adding member:', error);
return NextResponse.json(
{ message: 'Failed to add member', error: error },
{ status: 500 }
);
}
}

export async function PUT(request: Request) {
try {
const data = await request.json();
const { id, ...updatedData } = data;

if (!id || !updatedData.name || !updatedData.domain || !updatedData.year) {
return NextResponse.json(
{ message: 'Missing required fields or member ID', error: true },
{ status: 400 }
);
}

const memberRef = doc(db, "members", id); // Use the document ID
await updateDoc(memberRef, updatedData);

return NextResponse.json(
{ message: 'Member updated successfully', data: updatedData },
{ status: 200 }
);
} catch (error) {
console.error('Error updating member:', error);
return NextResponse.json(
{ message: 'Failed to update member', error },
{ status: 500 }
);
}
}


export async function DELETE(request: Request) {
try {
const { id } = await request.json();

// Collect saved data for response
savedData.push({ id: docRef.id, ...item });
if (!id) {
return NextResponse.json(
{ message: 'Missing member ID', error: true },
{ status: 400 }
);
}

// Return a success response
return NextResponse.json({ message: 'Registration successful', data: savedData });
await deleteDoc(doc(db, 'members', id));
return NextResponse.json({ message: 'Member deleted successfully' }, { status: 200 });
} catch (error) {
console.error('An error occurred:', error);
return NextResponse.json({ message: 'An error occurred', error });
console.error('Error deleting member:', error);
return NextResponse.json(
{ message: 'Failed to delete member', error: error },
{ status: 500 }
);
}
}
}
Loading

0 comments on commit 0ffd6db

Please sign in to comment.