diff --git a/app/api/shopping-list/[id]/route.ts b/app/api/shopping-list/[id]/route.ts new file mode 100644 index 0000000..5e7edda --- /dev/null +++ b/app/api/shopping-list/[id]/route.ts @@ -0,0 +1,34 @@ +import { NextRequest, NextResponse } from "next/server"; +import { getSession } from "@/lib/auth"; +import prisma from "@/lib/prisma"; + +type Ctx = { + params: Promise<{ id: string }>; +}; + +export async function DELETE(_req: NextRequest, context: Ctx) { + const session = await getSession(); + if (!session?.user?.id) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const { id } = await context.params; + const listId = Number(id); + + if (!Number.isFinite(listId)) { + return NextResponse.json({ error: "Invalid id" }, { status: 400 }); + } + + const list = await prisma.shoppingList.findUnique({ + where: { id: listId }, + select: { id: true, userId: true }, + }); + + if (!list || list.userId !== session.user.id) { + return NextResponse.json({ error: "Not found" }, { status: 404 }); + } + + await prisma.shoppingList.delete({ where: { id: listId } }); + + return NextResponse.json({ ok: true }, { status: 200 }); +} diff --git a/app/components/DeleteShoppingListButton.tsx b/app/components/DeleteShoppingListButton.tsx new file mode 100644 index 0000000..b03bb16 --- /dev/null +++ b/app/components/DeleteShoppingListButton.tsx @@ -0,0 +1,35 @@ +"use client"; + +import { useRouter } from "next/navigation"; + +export default function DeleteShoppingListButton({ id }: { id: number }) { + const router = useRouter(); + + async function onDelete() { + const ok = confirm("Na pewno usunąć tę listę zakupów?"); + if (!ok) return; + + const res = await fetch(`/api/shopping-list/${id}`, { + method: "DELETE", + }); + + if (!res.ok) { + const txt = await res.text(); + console.error("DELETE /api/shopping-list/[id] nieudany", txt); + alert("Nie udało się usunąć listy."); + return; + } + + router.refresh(); + } + + return ( + + ); +} diff --git a/app/shopping-list/[id]/page.tsx b/app/shopping-list/[id]/page.tsx index 6a216a4..db86c12 100644 --- a/app/shopping-list/[id]/page.tsx +++ b/app/shopping-list/[id]/page.tsx @@ -1,43 +1,73 @@ -import prisma from "@/lib/prisma" -import { getSession } from "@/lib/auth" +import prisma from "@/lib/prisma"; +import { getSession } from "@/lib/auth"; +import Link from "next/link"; type Props = { - params : Promise<{ id: string }> + params: Promise<{ id: string }>; +}; + +type ShoppingItem = { name: string; amount: number }; + +function formatDate(d: Date) { + return d.toISOString().slice(0, 10); } export default async function ShoppingListDetailPage({ params }: Props) { - const session = await getSession() - if (!session?.user?.id) return

Musisz być zalogowany

+ const session = await getSession(); + if (!session?.user?.id) return

Musisz być zalogowany

; - const corParams = await params - const id = parseInt(corParams.id, 10) - console.log("ID listy zakupów:", id) + const { id } = await params; + const listId = parseInt(id, 10); const list = await prisma.shoppingList.findUnique({ - where: { id }, - }) + where: { id: listId }, + }); if (!list || list.userId !== session.user.id) { - return

Lista zakupów nie znaleziona

+ return

Lista zakupów nie znaleziona

; } - const shoppingListItems = (list.items as { name: string; amount: number }[]) + const items = list.items as ShoppingItem[]; return ( -
+
+
-

- Lista zakupów: od {list.startDate.toISOString().slice(0,10)} do {list.endDate.toISOString().slice(0,10)} -

- -
    - {shoppingListItems.map((item: any) => ( -
  • - {item.name}: {item.amount}g -
  • - ))} -
+

+ Lista zakupów +

+

+ Zakres: {formatDate(list.startDate)} → {formatDate(list.endDate)} +

+ + + ← Wróć + +
+ +
+ {items.length === 0 ? ( +

Brak produktów na liście.

+ ) : ( +
    + {items.map((item) => ( +
  • + {item.name} + + {item.amount} g + +
  • + ))} +
+ )} +
- ) + ); } diff --git a/app/shopping-list/page.tsx b/app/shopping-list/page.tsx index 67d26ea..4ceb529 100644 --- a/app/shopping-list/page.tsx +++ b/app/shopping-list/page.tsx @@ -1,38 +1,84 @@ import prisma from "@/lib/prisma"; import { getSession } from "@/lib/auth"; +import Link from "next/link"; +import DeleteShoppingListButton from "@/app/components/DeleteShoppingListButton"; + +function formatDate(d: Date) { + return d.toISOString().slice(0, 10); +} export default async function ShoppingListPage() { const session = await getSession(); - if (!session?.user?.id) return

Musisz być zalogowany

; + if (!session?.user?.id) return

Musisz być zalogowany

; const lists = await prisma.shoppingList.findMany({ where: { userId: session.user.id }, orderBy: { createdAt: "desc" }, }); - return ( -
-

- Listy zakupowe -

+
+
+
+

+ Listy zakupowe +

+

+ Wybierz listę, aby zobaczyć szczegóły. +

+
+ + + + Wygeneruj nową listę + +
{lists.length === 0 ? ( -

Brak list zakupów. Wygeneruj nową listę zakupów.

+
+ Brak list zakupów. Kliknij “Wygeneruj nową listę”. +
) : ( + - - )} + )}
); }