-
Notifications
You must be signed in to change notification settings - Fork 0
prompt generation of backend.... post and get giving error 404 but it… #39
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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") | ||
| .then((res) => res.json()) | ||
| .then((data) => setSponsors(data)) | ||
| .catch((err) => console.error("Error loading sponsors:", err)); | ||
| }, []); | ||
|
|
||
| if (!sponsors) { | ||
| if (sponsors.length === 0) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... | ||
|
|
@@ -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 */} | ||
|
|
@@ -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> | ||
|
|
@@ -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} | ||
|
|
@@ -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} | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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); |
| 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; |
There was a problem hiding this comment.
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)