diff --git a/package-lock.json b/package-lock.json index 7f1b7cb..9ade85b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,7 +28,8 @@ "react-dom": "^19.0.0", "react-icons": "^5.5.0", "react-leaflet": "^5.0.0", - "react-node": "^1.0.2" + "react-node": "^1.0.2", + "swr": "^2.3.3" }, "devDependencies": { "@eslint/eslintrc": "^3", @@ -3015,6 +3016,15 @@ "node": ">= 0.8" } }, + "node_modules/dequal": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz", + "integrity": "sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, "node_modules/detect-libc": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.3.tgz", @@ -7704,6 +7714,19 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/swr": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/swr/-/swr-2.3.3.tgz", + "integrity": "sha512-dshNvs3ExOqtZ6kJBaAsabhPdHyeY4P2cKwRCniDVifBMoG/SVI7tfLWqPXriVspf2Rg4tPzXJTnwaihIeFw2A==", + "license": "MIT", + "dependencies": { + "dequal": "^2.0.3", + "use-sync-external-store": "^1.4.0" + }, + "peerDependencies": { + "react": "^16.11.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" + } + }, "node_modules/tailwindcss": { "version": "3.4.17", "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.17.tgz", diff --git a/package.json b/package.json index 8b9dab8..f14f100 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "react-dom": "^19.0.0", "react-icons": "^5.5.0", "react-leaflet": "^5.0.0", - "react-node": "^1.0.2" + "react-node": "^1.0.2", + "swr": "^2.3.3" }, "devDependencies": { "@eslint/eslintrc": "^3", diff --git a/src/app/api/earthquakes/route.ts b/src/app/api/earthquakes/route.ts new file mode 100644 index 0000000..17c6a27 --- /dev/null +++ b/src/app/api/earthquakes/route.ts @@ -0,0 +1,65 @@ +import { NextResponse } from 'next/server'; + +import { PrismaClient } from '@prisma/client'; + +const usingPrisma = false; +let prisma: PrismaClient; +if (usingPrisma) prisma = new PrismaClient(); + +export async function GET(request: Request) { + try { + const events = [ + { + id: "1234", + title: "Earthquake in Germany", + text1: "Magnitude 8.5", + text2: "30 minutes ago", + magnitude: 8.5, + longitude: 10.4515, // Near Berlin, Germany + latitude: 52.52, + }, + { + id: "2134", + title: "Earthquake in California", + text1: "Magnitude 5.3", + text2: "2 hours ago", + magnitude: 5.3, + longitude: -122.4194, // Near San Francisco, California, USA + latitude: 37.7749, + }, + { + id: "2314", + title: "Tremor in Japan", + text1: "Magnitude 4.7", + text2: "5 hours ago", + magnitude: 4.7, + longitude: 139.6917, // Near Tokyo, Japan + latitude: 35.6762, + }, + { + id: "2341", + title: "Tremor in Spain", + text1: "Magnitude 2.1", + text2: "10 hours ago", + magnitude: 2.1, + longitude: -3.7038, // Near Madrid, Spain + latitude: 40.4168, + }, + ]; + + let earthquakes; + if (usingPrisma) earthquakes = await prisma.earthquakes.findMany(); + + if (earthquakes) { + return NextResponse.json({ message: "Got earthquakes successfully", earthquakes }, { status: 200 }); + } else { + return NextResponse.json({ message: "Got earthquakes successfully", earthquakes: events }, { status: 200 }); + // return NextResponse.json({ message: "Failed to get earthquakes" }, { status: 401 }); + } + } catch (error) { + console.error("Error in earthquakes endpoint:", error); + return NextResponse.json({ message: "Internal Server Error" }, { status: 500 }); + } finally { + if (usingPrisma) await prisma.$disconnect(); + } +} diff --git a/src/app/api/login/route.ts b/src/app/api/login/route.ts index b1664f2..9ff9622 100644 --- a/src/app/api/login/route.ts +++ b/src/app/api/login/route.ts @@ -5,8 +5,9 @@ import { PrismaClient } from '@prisma/client'; import { findUserByEmail, readUserCsv, User } from '../functions/csvReadWrite'; -const prisma = new PrismaClient(); const usingPrisma = false; +let prisma: PrismaClient; +if (usingPrisma) prisma = new PrismaClient(); export async function POST(request: Request) { try { diff --git a/src/app/api/observatories/route.ts b/src/app/api/observatories/route.ts new file mode 100644 index 0000000..3f17067 --- /dev/null +++ b/src/app/api/observatories/route.ts @@ -0,0 +1,54 @@ +import { NextResponse } from 'next/server'; + +import { PrismaClient } from '@prisma/client'; + +const usingPrisma = false; +let prisma: PrismaClient; +if (usingPrisma) prisma = new PrismaClient(); + +export async function GET(request: Request) { + try { + const events = [ + { + id: "1234", + title: "Earthquake - Berlin Observatory", + text1: "Logged by ", + text2: "30 minutes ago", + longitude: 10.4515, // Near Berlin, Germany + latitude: 52.52, + }, + { + id: "2134", + title: "New Observatory - Phuket, Thailand", + text1: "Dr. Neil Armstrong", + text2: "2 weeks ago", + longitude: -122.4194, + latitude: 37.7749, + }, + { + id: "2314", + title: "Observatory Scientist Change", + text1: "Dr. Samantha Green new lead scientist", + text2: "1 month ago", + longitude: 139.6917, + latitude: 35.6762, + }, + ]; + + // todo get earthquakes associated with observatories + let observatories; + if (usingPrisma) observatories = await prisma.observatories.findMany(); + + if (observatories) { + return NextResponse.json({ message: "Got observatories successfully", observatories }, { status: 200 }); + } else { + return NextResponse.json({ message: "Got observatories successfully", observatories: events }, { status: 200 }); + // return NextResponse.json({ message: "Failed to get observatories" }, { status: 401 }); + } + } catch (error) { + console.error("Error in observatories endpoint:", error); + return NextResponse.json({ message: "Internal Server Error" }, { status: 500 }); + } finally { + if (usingPrisma) await prisma.$disconnect(); + } +} diff --git a/src/app/api/signup/route.ts b/src/app/api/signup/route.ts index 876bd1b..8e27c71 100644 --- a/src/app/api/signup/route.ts +++ b/src/app/api/signup/route.ts @@ -7,8 +7,9 @@ import { findUserByEmail, passwordStrengthCheck, readUserCsv, User, writeUserCsv } from '../functions/csvReadWrite'; -const prisma = new PrismaClient(); const usingPrisma = false; +let prisma: PrismaClient; +if (usingPrisma) prisma = new PrismaClient(); export async function POST(request: Request) { try { diff --git a/src/app/earthquakes/page.tsx b/src/app/earthquakes/page.tsx index 6cc05b2..ca0200a 100644 --- a/src/app/earthquakes/page.tsx +++ b/src/app/earthquakes/page.tsx @@ -1,61 +1,23 @@ "use client"; import { useMemo, useState } from 'react'; +import useSWR from 'swr'; import Map from '@components/Map'; import Sidebar from '@components/Sidebar'; +import { fetcher } from '@utils/fetcher'; export default function Earthquakes() { const [selectedEventId, setSelectedEventId] = useState(""); const [hoveredEventId, setHoveredEventId] = useState(""); - - const events = useMemo( - () => [ - { - id: "1234", - title: "Earthquake in Germany", - text1: "Magnitude 8.5", - text2: "30 minutes ago", - magnitude: 8.5, - longitude: 10.4515, // Near Berlin, Germany - latitude: 52.52, - }, - { - id: "2134", - title: "Earthquake in California", - text1: "Magnitude 5.3", - text2: "2 hours ago", - magnitude: 5.3, - longitude: -122.4194, // Near San Francisco, California, USA - latitude: 37.7749, - }, - { - id: "2314", - title: "Tremor in Japan", - text1: "Magnitude 4.7", - text2: "5 hours ago", - magnitude: 4.7, - longitude: 139.6917, // Near Tokyo, Japan - latitude: 35.6762, - }, - { - id: "2341", - title: "Tremor in Spain", - text1: "Magnitude 2.1", - text2: "10 hours ago", - magnitude: 2.1, - longitude: -3.7038, // Near Madrid, Spain - latitude: 40.4168, - }, - ], - [] - ); + // todo properly integrate loading + const { data, error, isLoading } = useSWR("/api/earthquakes", fetcher); return (
[ - { - id: "1234", - title: "Earthquake - Berlin Observatory", - text1: "Logged by ", - text2: "30 minutes ago", - longitude: 10.4515, // Near Berlin, Germany - latitude: 52.52, - }, - { - id: "2134", - title: "New Observatory - Phuket, Thailand", - text1: "Dr. Neil Armstrong", - text2: "2 weeks ago", - longitude: -122.4194, - latitude: 37.7749, - }, - { - id: "2314", - title: "Observatory Scientist Change", - text1: "Dr. Samantha Green new lead scientist", - text2: "1 month ago", - longitude: 139.6917, - latitude: 35.6762, - }, - ], - [] - ); + // todo properly integrate loading + const { data, error, isLoading } = useSWR("/api/earthquakes", fetcher); return (
axios.get(url).then((res) => res.data);