Added warehouse log single artefact
This commit is contained in:
parent
f3ad2c8ed8
commit
62b9008814
41
src/app/api/warehouse/log/route.ts
Normal file
41
src/app/api/warehouse/log/route.ts
Normal file
@ -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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -291,7 +291,6 @@ export default function Shop() {
|
|||||||
}
|
}
|
||||||
// remove all artefacts that were bought (works for both cart and single)
|
// remove all artefacts that were bought (works for both cart and single)
|
||||||
setHiddenArtefactIds((ids) => [...ids, ...artefactsToBuy.map((a) => a.id)]);
|
setHiddenArtefactIds((ids) => [...ids, ...artefactsToBuy.map((a) => a.id)]);
|
||||||
//!! todo create receiving api route
|
|
||||||
const genOrder = () =>
|
const genOrder = () =>
|
||||||
"#" + Math.random().toString(24).substring(2, 10).toUpperCase() + new Date().toLocaleDateString("en-GB");
|
"#" + Math.random().toString(24).substring(2, 10).toUpperCase() + new Date().toLocaleDateString("en-GB");
|
||||||
const orderNum = genOrder();
|
const orderNum = genOrder();
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import axios from "axios";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
import { Dispatch, SetStateAction, useMemo, useState } from "react";
|
import { Dispatch, SetStateAction, useMemo, useState } from "react";
|
||||||
import { FaTimes } from "react-icons/fa";
|
import { FaTimes } from "react-icons/fa";
|
||||||
@ -74,10 +75,11 @@ function FilterInput({
|
|||||||
// Modal Component for Logging Artefact
|
// Modal Component for Logging Artefact
|
||||||
function LogModal({ onClose }: { onClose: () => void }) {
|
function LogModal({ onClose }: { onClose: () => void }) {
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
|
const [type, setType] = useState("");
|
||||||
const [description, setDescription] = useState("");
|
const [description, setDescription] = useState("");
|
||||||
const [location, setLocation] = useState("");
|
const [location, setLocation] = useState("");
|
||||||
const [earthquakeId, setEarthquakeId] = useState("");
|
const [earthquakeCode, setEarthquakeCode] = useState("");
|
||||||
const [storageLocation, setStorageLocation] = useState("");
|
const [warehouseLocation, setWarehouseLocation] = useState("");
|
||||||
const [isRequired, setIsRequired] = useState(true);
|
const [isRequired, setIsRequired] = useState(true);
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
@ -88,23 +90,32 @@ function LogModal({ onClose }: { onClose: () => void }) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLog = async () => {
|
// todo add uploading image
|
||||||
if (!name || !description || !location || !earthquakeId || !storageLocation) {
|
async function handleLog() {
|
||||||
|
if (!name || !type || !description || !location || !earthquakeCode || !warehouseLocation) {
|
||||||
setError("All fields are required.");
|
setError("All fields are required.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setIsSubmitting(true);
|
setIsSubmitting(true);
|
||||||
try {
|
try {
|
||||||
// todo!! add log api route
|
await axios.post("/api/warehouse/log", {
|
||||||
await new Promise((resolve) => setTimeout(resolve, 500)); // Simulated API call
|
name,
|
||||||
alert(`Logged ${name} to storage: ${storageLocation}`);
|
type,
|
||||||
|
description,
|
||||||
|
location,
|
||||||
|
earthquakeCode,
|
||||||
|
warehouseLocation,
|
||||||
|
});
|
||||||
|
|
||||||
|
// todo replace with better alert
|
||||||
|
alert(`Logged ${name} to storage: ${warehouseLocation}`);
|
||||||
onClose();
|
onClose();
|
||||||
} catch {
|
} catch {
|
||||||
setError("Failed to log artefact. Please try again.");
|
setError("Failed to log artefact. Please try again.");
|
||||||
} finally {
|
} finally {
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@ -124,6 +135,15 @@ function LogModal({ onClose }: { onClose: () => void }) {
|
|||||||
aria-label="Artefact Name"
|
aria-label="Artefact Name"
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
/>
|
/>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Type (e.g., Lava, Tephra, Ash"
|
||||||
|
value={type}
|
||||||
|
onChange={(e) => 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}
|
||||||
|
/>
|
||||||
<textarea
|
<textarea
|
||||||
placeholder="Description"
|
placeholder="Description"
|
||||||
value={description}
|
value={description}
|
||||||
@ -143,18 +163,19 @@ function LogModal({ onClose }: { onClose: () => void }) {
|
|||||||
/>
|
/>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="Earthquake ID"
|
placeholder="Earthquake Code"
|
||||||
value={earthquakeId}
|
value={earthquakeCode}
|
||||||
onChange={(e) => setEarthquakeId(e.target.value)}
|
// todo check code is correct format
|
||||||
|
onChange={(e) => setEarthquakeCode(e.target.value)}
|
||||||
className="w-full p-2 border border-neutral-300 rounded-md placeholder-neutral-400 focus:ring-2 focus:ring-blue-500"
|
className="w-full p-2 border border-neutral-300 rounded-md placeholder-neutral-400 focus:ring-2 focus:ring-blue-500"
|
||||||
aria-label="Earthquake ID"
|
aria-label="Earthquake ID"
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
/>
|
/>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="Storage Location (e.g., A-12)"
|
placeholder="Warehouse Location (e.g., A-12)"
|
||||||
value={storageLocation}
|
value={warehouseLocation}
|
||||||
onChange={(e) => setStorageLocation(e.target.value)}
|
onChange={(e) => setWarehouseLocation(e.target.value)}
|
||||||
className="w-full p-2 border border-neutral-300 rounded-md placeholder-neutral-400 focus:ring-2 focus:ring-blue-500"
|
className="w-full p-2 border border-neutral-300 rounded-md placeholder-neutral-400 focus:ring-2 focus:ring-blue-500"
|
||||||
aria-label="Storage Location"
|
aria-label="Storage Location"
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user