Skip to content
Open
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
31 changes: 14 additions & 17 deletions client/src/pages/Sponsors.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import { useEffect, useState } from "react";
import SponsorCard from "../components/SponsorCard";
import sponsorsJsonData from "../placeholders/sponsors.json";

// 1. Define the shape of a single sponsor
interface Sponsor {
name: string;
deal: string;
address: string;
category: "cbd" | "newmarket" | "other";
code?: string;
}

// 2. Define the shape of the whole JSON file
interface SponsorsData {
cbd_sponsors: Sponsor[];
newmarket_sponsors: Sponsor[];
other_sponsors: Sponsor[];
}

const Sponsors = () => {
const [sponsors, setSponsors] = useState<SponsorsData | null>(null);
const [sponsors, setSponsors] = useState<Sponsor[]>([]);

useEffect(() => {
Promise.resolve(sponsorsJsonData as SponsorsData)
fetch("http://localhost:3000/api/sponsors")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use axios for this, also consider adding a sponsorsApi.ts file to handle basic boilerplate (acts as a quick way to see/update the routes if the endpoint urls ever change)

.then((res) => res.json())
.then((data) => setSponsors(data))
.catch((err) => console.error("Error loading sponsors:", err));
}, []);

if (!sponsors) {
if (sponsors.length === 0) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

length = 0 isn't preferable (if there's no sponsors then we're stuck in an infinite loading state!)

should use !sponsors like before (and usestate defaults to null like previously)

return (
<div style={{ textAlign: "center", padding: "2rem" }}>
Loading Sponsors...
Expand All @@ -42,6 +35,10 @@ const Sponsors = () => {
padding: "2rem",
} as const;

const cbd = sponsors.filter((s) => s.category === "cbd");
const newmarket = sponsors.filter((s) => s.category === "newmarket");
const other = sponsors.filter((s) => s.category === "other");

return (
<div style={{ textAlign: "center", backgroundColor: "#faf3d1" }}>
{/* HERO SECTION */}
Expand Down Expand Up @@ -71,12 +68,12 @@ const Sponsors = () => {
<section id="CBD">
<h2 style={{ marginTop: "2rem" }}>CBD Sponsors</h2>
<div style={gridStyle}>
{sponsors.cbd_sponsors.map((s, index) => (
{cbd.map((s, index) => (
<SponsorCard
key={index}
name={s.name}
description={s.deal} // Mapping 'deal' from JSON to 'description' prop
location={s.address} // Mapping 'address' from JSON to 'location' prop
description={s.deal}
location={s.address}
/>
))}
</div>
Expand All @@ -86,7 +83,7 @@ const Sponsors = () => {
<section id="Newmarket">
<h2 style={{ marginTop: "2rem" }}>Newmarket Sponsors</h2>
<div style={gridStyle}>
{sponsors.newmarket_sponsors.map((s, index) => (
{newmarket.map((s, index) => (
<SponsorCard
key={index}
name={s.name}
Expand All @@ -101,7 +98,7 @@ const Sponsors = () => {
<section id="Other">
<h2 style={{ marginTop: "2rem" }}>Other Sponsors</h2>
<div style={gridStyle}>
{sponsors.other_sponsors.map((s, index) => (
{other.map((s, index) => (
<SponsorCard
key={index}
name={s.name}
Expand Down
32 changes: 16 additions & 16 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dotenv": "^17.3.1",
"express": "^5.2.1",
"express-session": "^1.19.0",
"mongoose": "^9.2.4",
"mongoose": "^9.6.1",
"multer": "^2.1.1",
"passport": "^0.7.0",
"passport-google-oauth20": "^2.0.0"
Expand Down
2 changes: 2 additions & 0 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Strategy as GoogleStrategy } from "passport-google-oauth20";
import authRoutes from "./routes/authRoutes";
import imageRoutes from "./routes/imageRoutes";
import executivesRoutes from "./routes/executivesRoutes";
import sponsorRoutes from "./routes/sponsorRoutes";

// app config
dotenv.config({ quiet: true });
Expand Down Expand Up @@ -47,6 +48,7 @@ app.use(express.json());
app.use("/api/auth", authRoutes);
app.use("/api/images", imageRoutes);
app.use("/api/executives", executivesRoutes);
app.use("/api/sponsors", sponsorRoutes);

// Connect to MongoDB and start the server
mongoose
Expand Down
12 changes: 12 additions & 0 deletions server/src/model/sponsors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import mongoose from "mongoose";

const sponsorSchema = new mongoose.Schema(
{
name: { type: String, required: true },
tier: { type: String, enum: ["gold", "silver", "bronze"], required: true },
logoUrl: { type: String, required: true },
},
{ timestamps: true }
);

export const Sponsor = mongoose.model("Sponsor", sponsorSchema);
28 changes: 28 additions & 0 deletions server/src/routes/sponsorRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import express from "express";
import { Sponsor } from "../model/sponsors";

const router = express.Router();

router.post("/", async (req, res) => {
const sponsor = await Sponsor.create(req.body);
res.json(sponsor);
});

router.get("/", async (req, res) => {
const sponsors = await Sponsor.find();
res.json(sponsors);
});

router.put("/:id", async (req, res) => {
const updated = await Sponsor.findByIdAndUpdate(req.params.id, req.body, {
new: true,
});
res.json(updated);
});

router.delete("/:id", async (req, res) => {
await Sponsor.findByIdAndDelete(req.params.id);
res.json({ message: "Deleted" });
});

export default router;
Loading