From 62b9008814e2ac580c0c72630d274007e012ceee Mon Sep 17 00:00:00 2001 From: Tim Howitz Date: Mon, 2 Jun 2025 08:39:15 +0100 Subject: [PATCH 1/2] Added warehouse log single artefact --- src/app/api/warehouse/log/route.ts | 41 +++++++++++++++++++++++++ src/app/shop/page.tsx | 1 - src/app/warehouse/page.tsx | 49 +++++++++++++++++++++--------- 3 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 src/app/api/warehouse/log/route.ts diff --git a/src/app/api/warehouse/log/route.ts b/src/app/api/warehouse/log/route.ts new file mode 100644 index 0000000..dc35fe2 --- /dev/null +++ b/src/app/api/warehouse/log/route.ts @@ -0,0 +1,41 @@ +import { NextRequest, NextResponse } from "next/server"; +import { apiAuthMiddleware } from "@utils/apiAuthMiddleware"; +import { prisma } from "@utils/prisma"; + +export async function POST(request: NextRequest) { + try { + const body = await request.json(); + const { name, type, description, location, earthquakeCode, warehouseLocation } = body; + + const authResult = await apiAuthMiddleware(); + if ("user" in authResult === false) return authResult; // Handle error response + + const { user } = authResult; + + if (!name || !type || !description || !location || !earthquakeCode || !warehouseLocation) { + return NextResponse.json({ error: "Missing fields" }, { status: 400 }); + } + + const linkedEarthquake = await prisma.earthquake.findUnique({ where: { code: earthquakeCode } }); + + if (!linkedEarthquake) { + return NextResponse.json({ error: "Earthquake code not found" }, { status: 400 }); + } + + await prisma.artefact.create({ + data: { + name, + type, + description, + earthquakeId: linkedEarthquake.id, + warehouseArea: warehouseLocation, + imageName: "NoImageFound.PNG", + creatorId: user.id, + }, + }); + + return NextResponse.json({ message: "Artefact logged successfully" }, { status: 200 }); + } catch (e: any) { + return NextResponse.json({ error: e.message }, { status: 500 }); + } +} diff --git a/src/app/shop/page.tsx b/src/app/shop/page.tsx index faa8adf..684ba10 100644 --- a/src/app/shop/page.tsx +++ b/src/app/shop/page.tsx @@ -291,7 +291,6 @@ export default function Shop() { } // remove all artefacts that were bought (works for both cart and single) setHiddenArtefactIds((ids) => [...ids, ...artefactsToBuy.map((a) => a.id)]); - //!! todo create receiving api route const genOrder = () => "#" + Math.random().toString(24).substring(2, 10).toUpperCase() + new Date().toLocaleDateString("en-GB"); const orderNum = genOrder(); diff --git a/src/app/warehouse/page.tsx b/src/app/warehouse/page.tsx index b0a8421..aaf28ac 100644 --- a/src/app/warehouse/page.tsx +++ b/src/app/warehouse/page.tsx @@ -1,4 +1,5 @@ "use client"; +import axios from "axios"; import useSWR from "swr"; import { Dispatch, SetStateAction, useMemo, useState } from "react"; import { FaTimes } from "react-icons/fa"; @@ -74,10 +75,11 @@ function FilterInput({ // Modal Component for Logging Artefact function LogModal({ onClose }: { onClose: () => void }) { const [name, setName] = useState(""); + const [type, setType] = useState(""); const [description, setDescription] = useState(""); const [location, setLocation] = useState(""); - const [earthquakeId, setEarthquakeId] = useState(""); - const [storageLocation, setStorageLocation] = useState(""); + const [earthquakeCode, setEarthquakeCode] = useState(""); + const [warehouseLocation, setWarehouseLocation] = useState(""); const [isRequired, setIsRequired] = useState(true); const [error, setError] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); @@ -88,23 +90,32 @@ function LogModal({ onClose }: { onClose: () => void }) { } }; - const handleLog = async () => { - if (!name || !description || !location || !earthquakeId || !storageLocation) { + // todo add uploading image + async function handleLog() { + if (!name || !type || !description || !location || !earthquakeCode || !warehouseLocation) { setError("All fields are required."); return; } setIsSubmitting(true); try { - // todo!! add log api route - await new Promise((resolve) => setTimeout(resolve, 500)); // Simulated API call - alert(`Logged ${name} to storage: ${storageLocation}`); + await axios.post("/api/warehouse/log", { + name, + type, + description, + location, + earthquakeCode, + warehouseLocation, + }); + + // todo replace with better alert + alert(`Logged ${name} to storage: ${warehouseLocation}`); onClose(); } catch { setError("Failed to log artefact. Please try again."); } finally { setIsSubmitting(false); } - }; + } return (
void }) { aria-label="Artefact Name" disabled={isSubmitting} /> + setType(e.target.value)} + className="w-full p-2 border border-neutral-300 rounded-md placeholder-neutral-400 focus:ring-2 focus:ring-blue-500" + aria-label="Artefact Type" + disabled={isSubmitting} + />