getting lost files back and making some changes p1

This commit is contained in:
2026-01-14 20:15:07 +01:00
parent 26d140f910
commit bcc5164abb
30609 changed files with 18693 additions and 77811 deletions
+204
View File
@@ -0,0 +1,204 @@
import { db } from "~/server/db";
import { auth } from "~/server/auth";
import { redirect } from "next/navigation";
import { revalidatePath } from "next/cache";
export default async function ItemsPage() {
const session = await auth();
if (!session?.user?.id) {
redirect("/sign-in");
}
const userId = session.user.id;
// fetch all items for shops owned by the user
const items = await db.sellable.findMany({
where: { shop: { userId: userId } },
include: { shop: true },
});
// extract unique shops for dropdown
const uniqueShops = await db.shop.findMany({
where: { userId: userId },
});
// --- Server actions ---
async function addItem(data: FormData) {
"use server";
const name = data.get("item_name") as string;
const price = parseFloat(data.get("price") as string);
const amount = parseInt(data.get("amount") as string);
const shopId = parseInt(data.get("shopId") as string);
if (!name || isNaN(price) || isNaN(amount) || isNaN(shopId)) return;
await db.sellable.create({
data: { item_name: name, price, amount, shopId },
});
revalidatePath("/items");
}
async function updateItem(data: FormData) {
"use server";
const id = data.get("id") as string;
const name = data.get("item_name") as string;
const price = parseFloat(data.get("price") as string);
const amount = parseInt(data.get("amount") as string);
if (!id || !name || isNaN(price) || isNaN(amount)) return;
await db.sellable.update({
where: { id },
data: { item_name: name, price, amount },
});
revalidatePath("/items");
}
async function deleteItem(data: FormData) {
"use server";
const id = data.get("id") as string;
if (!id) return;
await db.sellable.delete({ where: { id } });
revalidatePath("/items");
}
return (
<main className="flex min-h-screen items-center justify-center bg-linear-to-b from-[#2e026d] to-[#7f3b3b] p-4 text-white">
<div className="w-full max-w-4xl space-y-8 rounded-lg bg-white/10 p-6 backdrop-blur-md">
<h1 className="text-4xl font-extrabold">
Manage <span className="text-[hsl(280,100%,70%)]">Items</span>
</h1>
{/* Add new item */}
<section>
<h2 className="mb-2 text-2xl font-semibold">Add New Item</h2>
<form action={addItem} className="flex flex-col gap-4">
<label className="flex flex-col text-sm">
Item Name
<input
type="text"
name="item_name"
placeholder="Item Name"
required
className="rounded border border-white/30 bg-white/10 p-2 text-sm text-white placeholder-white/70"
/>
</label>
<label className="flex flex-col text-sm">
Price
<input
type="number"
step="0.01"
name="price"
placeholder="Price"
required
className="rounded border border-white/30 bg-white/10 p-2 text-sm text-white placeholder-white/70"
/>
</label>
<label className="flex flex-col text-sm">
Amount
<input
type="number"
name="amount"
placeholder="Amount"
required
className="rounded border border-white/30 bg-white/10 p-2 text-sm text-white placeholder-white/70"
/>
</label>
<label className="flex flex-col text-sm">
Shop
<select
name="shopId"
required
className="rounded border border-white/30 bg-white/10 p-2 text-sm text-white"
>
{uniqueShops.map((shop) => (
<option key={shop.id} value={shop.id}>
{shop.label}
</option>
))}
</select>
</label>
<button
type="submit"
className="rounded bg-[hsl(280,100%,70%)] p-2 text-sm font-semibold"
>
Add Item
</button>
</form>
</section>
{/* Existing items in a grid */}
<section>
<h2 className="mb-2 text-2xl font-semibold">Your Items</h2>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
{items.map((item) => (
<div
key={item.id}
className="space-y-2 rounded bg-white/10 p-4 text-sm backdrop-blur-sm"
>
{/* Update form */}
<form action={updateItem} className="flex flex-col gap-2">
<input type="hidden" name="id" value={item.id} />
<label className="flex flex-col">
Name
<input
type="text"
name="item_name"
defaultValue={item.item_name}
className="rounded border border-white/30 bg-white/10 p-1 text-sm text-white"
/>
</label>
<label className="flex flex-col">
Price
<input
type="number"
step="0.01"
name="price"
defaultValue={item.price}
className="rounded border border-white/30 bg-white/10 p-1 text-sm text-white"
/>
</label>
<label className="flex flex-col">
Amount
<input
type="number"
name="amount"
defaultValue={item.amount}
className="rounded border border-white/30 bg-white/10 p-1 text-sm text-white"
/>
</label>
<button
type="submit"
className="rounded bg-green-600 p-1 text-sm font-semibold"
>
Update
</button>
</form>
{/* Delete button */}
<form action={deleteItem}>
<input type="hidden" name="id" value={item.id} />
<button
type="submit"
className="mt-1 w-full rounded bg-red-600 p-1 text-sm font-semibold"
>
Delete
</button>
</form>
<p className="text-xs text-white/70">Shop: {item.shop.label}</p>
</div>
))}
</div>
</section>
</div>
</main>
);
}