Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PUT route to achievements page and fix all the linting errors #81

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/(default)/achievements/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { onAuthStateChanged } from "firebase/auth";
import { auth } from "@/Firebase";
import { db } from "@/Firebase";
import { collection, doc, getDoc, getDocs } from "firebase/firestore";
import Image from "next/image";

interface Achiever {
imageUrl?: string;
Expand All @@ -23,8 +24,10 @@ function AchievementCard({ achiever }: { achiever: Achiever }) {
return (
<div className="bg-[hsla(0,0%,100%,.079)] rounded-xl shadow-lg overflow-hidden w-[330px]">
<div className="overflow-hidden">
<img
src={achiever.imageUrl}
<Image
width={500}
height={500}
src={achiever.imageUrl || ""}
alt={`${achiever.name}'s profile`}
className="w-full h-[300px] object-cover object-center"
/>
Expand Down
101 changes: 97 additions & 4 deletions app/(default)/api/achievements/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { db, storage } from "@/Firebase";
import {
doc,
updateDoc,
getDoc,
collection,
addDoc,
getDocs,
Expand All @@ -9,7 +12,7 @@ import {
DocumentSnapshot,
} from "firebase/firestore";
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
import { NextResponse } from "next/server";
import { NextRequest, NextResponse } from "next/server";

export async function POST(request: Request) {
try {
Expand Down Expand Up @@ -92,10 +95,24 @@ export async function POST(request: Request) {
}
}

export async function GET() {
export async function GET(request: NextRequest) {
try {
// Fetch all documents from the "achievements" collection
const querySnapshot = await getDocs(collection(db, "achievements"));
const { searchParams } = new URL(request.url);
const name = searchParams.get("name");

let querySnapshot;

if (name) {
// Query Firestore by name to find the document
const memberQuery = query(
collection(db, "achievements"),
where("Name", "==", name)
);
querySnapshot = await getDocs(memberQuery);
} else {
// Fetch all documents from the "achievements" collection
querySnapshot = await getDocs(collection(db, "achievements"));
}

// Map through the documents and extract the data
const membersRaw = querySnapshot.docs.map(
Expand All @@ -104,6 +121,7 @@ export async function GET() {
...doc.data(),
})
);

const members = membersRaw.map((member: any) => {
return {
id: member.id,
Expand All @@ -117,6 +135,7 @@ export async function GET() {
imageUrl: member.imageUrl,
};
});

// Return the members data
return NextResponse.json(members);
} catch (error) {
Expand All @@ -138,3 +157,77 @@ export async function GET() {
}
}
}

export async function PUT(request: Request) {
try {
const formData = await request.formData();
const name = formData.get("name") as string;
const email = formData.get("email") as string;
const batch = formData.get("batch") as string;
const portfolio = formData.get("portfolio") as string;
const internship = formData.get("internship") as string;
const companyPosition = formData.get("companyPosition") as string;
const achievements = JSON.parse(
formData.get("achievements") as string
) as string[];
const image = formData.get("image") as File;

const memberQuery = query(
collection(db, "achievements"),
where("Name", "==", name)
);
const querySnapshot = await getDocs(memberQuery);

if (querySnapshot.empty) {
return NextResponse.json(
{ error: `No member found with the name ${name}` },
{ status: 404 }
);
}

const docRef = querySnapshot.docs[0].ref;
let imageUrl = querySnapshot.docs[0].data().imageUrl;

if (image) {
const storageRef = ref(storage, `images/${image.name}`);
await uploadBytes(storageRef, image);
imageUrl = await getDownloadURL(storageRef);
}

await updateDoc(docRef, {
Name: name,
Email: email,
Batch: batch,
Portfolio: portfolio,
Internship: internship,
CompanyPosition: companyPosition,
achievements: achievements,
imageUrl: imageUrl,
});

return NextResponse.json({
name,
email,
batch,
portfolio,
internship,
companyPosition,
achievements,
imageUrl,
});
} catch (error) {
if (error instanceof Error) {
console.error("Error updating member:", error.message);
return NextResponse.json(
{ error: "An error occurred while updating", details: error.message },
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred while updating" },
{ status: 500 }
);
}
}
}
5 changes: 4 additions & 1 deletion app/(default)/events/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "firebase/firestore";
import { onAuthStateChanged } from "firebase/auth";
import "./EventCard.css";
import Image from "next/image";

interface Event {
eventName: string;
Expand Down Expand Up @@ -207,7 +208,9 @@ const EventCard: React.FC = () => {
<div className="event-card-front">
<div className="event-container">
<div className="event-poster-container">
<img
<Image
width={300}
height={300}
src={event.imageUrl}
alt={event.eventName}
className="event-poster"
Expand Down
Loading
Loading