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