Fix formatting, add types

This commit is contained in:
2025-10-16 10:00:31 +02:00
parent 35c4317925
commit 6d9e307318

View File

@@ -1,18 +1,15 @@
"use client";
import { useEffect, useState } from "react";
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
import { Card, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { TopBar } from "@/components/ui/topbar";
import { RefreshCw } from "lucide-react";
import { useRouter } from "next/navigation";
const API_URL = "";
type User = {
id: number;
username: string;
@@ -31,11 +28,10 @@ type Order = {
type FinalizedSummary = { pickup: string[]; kitchen: string[] };
type MenuItem = {
id: number;
category: string;
name: string
name: string;
};
export default function AdminPage() {
@@ -61,8 +57,8 @@ export default function AdminPage() {
useEffect(() => {
fetch(`${API_URL}/api/admin/finalize/time`, { headers: auth() })
.then(r => r.json())
.then(data => setFinalizeTime(data.time));
.then((r) => r.json())
.then((data) => setFinalizeTime(data.time));
}, []);
useEffect(() => {
@@ -73,10 +69,10 @@ export default function AdminPage() {
}
fetch(`${API_URL}/api/me`, {
headers: { Authorization: `Bearer ${token}` }
headers: { Authorization: `Bearer ${token}` },
})
.then(res => res.json())
.then(data => {
.then((res) => res.json())
.then((data) => {
if (!data.role || data.role > 1) {
router.push("/landing");
}
@@ -86,7 +82,7 @@ export default function AdminPage() {
useEffect(() => {
fetch(`${API_URL}/api/admin/users`, { headers: auth() })
.then(r => r.ok ? r.json() : [])
.then((r) => (r.ok ? r.json() : []))
.then(setUsers)
.catch(() => setUsers([]));
}, []);
@@ -108,7 +104,9 @@ export default function AdminPage() {
async function refreshOrders() {
try {
const res = await fetch(`${API_URL}/api/admin/orders`, { headers: auth() });
const res = await fetch(`${API_URL}/api/admin/orders`, {
headers: auth(),
});
if (res.ok) {
const data: Order[] = await res.json();
setOrders(data || []);
@@ -130,7 +128,11 @@ export default function AdminPage() {
await fetch(`${API_URL}/api/admin/menu`, {
method: "POST",
headers: { "Content-Type": "application/json", ...auth() },
body: JSON.stringify({ action: "add", category: newCategory, name: newItem }),
body: JSON.stringify({
action: "add",
category: newCategory,
name: newItem,
}),
});
setNewItem("");
await refreshMenu();
@@ -141,7 +143,11 @@ export default function AdminPage() {
await fetch(`${API_URL}/api/admin/menu`, {
method: "POST",
headers: { "Content-Type": "application/json", ...auth() },
body: JSON.stringify({ action: "update", id: editingItem.id, name: editingItem.name }),
body: JSON.stringify({
action: "update",
id: editingItem.id,
name: editingItem.name,
}),
});
setEditingItem(null);
await refreshMenu();
@@ -158,12 +164,20 @@ export default function AdminPage() {
async function deleteUser(id: number) {
if (!confirm("Biztos törlöd a felhasználót?")) return;
await fetch(`${API_URL}/api/admin/users/${id}`, { method: "DELETE", headers: auth() });
await fetch(`${API_URL}/api/admin/users/${id}`, {
method: "DELETE",
headers: auth(),
});
setUsers((prev) => prev.filter((u) => u.id !== id));
}
async function updateUser(id: number) {
const body: any = {};
const body: {
username?: string;
password?: string;
role?: number;
} = {};
if (newUsername) body.username = newUsername;
if (newPassword) body.password = newPassword;
if (newRole !== null) body.role = newRole;
@@ -225,7 +239,10 @@ export default function AdminPage() {
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ status: "history" }),
});
await fetch(`${API_URL}/api/admin/orders/${id}`, { method: "DELETE", headers: auth() });
await fetch(`${API_URL}/api/admin/orders/${id}`, {
method: "DELETE",
headers: auth(),
});
})
);
const res = await fetch(`${API_URL}/api/admin/orders`);
@@ -242,7 +259,9 @@ export default function AdminPage() {
}
async function loadLastSummary() {
const res = await fetch(`${API_URL}/api/mod/finalize/last`, { headers: auth() });
const res = await fetch(`${API_URL}/api/mod/finalize/last`, {
headers: auth(),
});
const data = await res.json();
if (data.status !== "none") setLastSummary(data);
else setLastSummary(null);
@@ -277,7 +296,10 @@ export default function AdminPage() {
<div className="flex flex-col sm:flex-row gap-2 w-full items-center justify-between">
{/* Keep current username visible */}
<span className="text-white font-medium">
{u.username} <span className="text-sm text-white/60">(role: {u.role})</span>
{u.username}{" "}
<span className="text-sm text-white/60">
(role: {u.role})
</span>
</span>
{/* Inputs + buttons */}
@@ -323,7 +345,10 @@ export default function AdminPage() {
) : (
<>
<span className="text-white font-medium">
{u.username} <span className="text-sm text-white/60">(role: {u.role})</span>
{u.username}{" "}
<span className="text-sm text-white/60">
(role: {u.role})
</span>
</span>
<div className="flex gap-2">
<Button
@@ -343,9 +368,7 @@ export default function AdminPage() {
</div>
</>
)}
</Card>
))
) : (
<p className="text-white/70 text-center">No registered users.</p>
@@ -442,11 +465,17 @@ export default function AdminPage() {
{["soup", "main", "side"].map((cat) => (
<div key={cat} className="flex flex-col">
<h3 className="text-center text-xl font-semibold text-white mb-2">
{cat === "soup" ? "Soups" : cat === "main" ? "Main courses" : "Sides"}
{cat === "soup"
? "Soups"
: cat === "main"
? "Main courses"
: "Sides"}
</h3>
<div className="max-h-[300px] overflow-y-auto space-y-2">
{menuItems.filter((i) => i.category === cat).map((item) => (
{menuItems
.filter((i) => i.category === cat)
.map((item) => (
<Card
key={item.id}
className="glass-panel flex flex-col lg:flex-row md:items-center sm:justify-between p-2 gap-2"
@@ -456,7 +485,10 @@ export default function AdminPage() {
<Input
value={editingItem.name}
onChange={(e) =>
setEditingItem({ ...editingItem, name: e.target.value })
setEditingItem({
...editingItem,
name: e.target.value,
})
}
className="bg-white/70 text-black sm:w-40 border-white"
/>
@@ -479,7 +511,9 @@ export default function AdminPage() {
</div>
) : (
<>
<span className="text-white font-medium">{item.name}</span>
<span className="text-white font-medium">
{item.name}
</span>
<div className="flex gap-2">
<Button
size="sm"
@@ -505,7 +539,9 @@ export default function AdminPage() {
{/* Add new item for this category */}
<div className="flex flex-col sm:flex-row gap-2 mt-3">
<Input
placeholder={`Új ${cat === "soup" ? "Soup" : cat === "main" ? "Main" : "Side"}`}
placeholder={`Új ${
cat === "soup" ? "Soup" : cat === "main" ? "Main" : "Side"
}`}
value={newCategory === cat ? newItem : ""}
onChange={(e) => {
setNewItem(e.target.value);
@@ -551,7 +587,6 @@ export default function AdminPage() {
Finalize Orders Now
</Button>
</div>
</div>
{/* Finalized Order Summary Panel */}
@@ -563,7 +598,9 @@ export default function AdminPage() {
{lastSummary ? (
<>
<div>
<h3 className="text-xl font-semibold text-white mb-2">Átvételi nézet</h3>
<h3 className="text-xl font-semibold text-white mb-2">
Átvételi nézet
</h3>
<ul className="list-disc list-inside text-white space-y-1">
{lastSummary.pickup.map((line, idx) => (
<li key={idx}>{line}</li>
@@ -572,7 +609,9 @@ export default function AdminPage() {
</div>
<div>
<h3 className="text-xl font-semibold text-white mb-2">Konyha nézet</h3>
<h3 className="text-xl font-semibold text-white mb-2">
Konyha nézet
</h3>
<ul className="list-disc list-inside text-white space-y-1">
{lastSummary.kitchen.map((line, idx) => (
<li key={idx}>{line}</li>
@@ -584,7 +623,6 @@ export default function AdminPage() {
<p className="text-white italic">Nincs elérhető összesítés.</p>
)}
</div>
</main>
);
}