From c27713ba27f1757464b3ed984f07b8c382352506 Mon Sep 17 00:00:00 2001 From: headlessdev Date: Fri, 11 Apr 2025 22:55:17 +0200 Subject: [PATCH] Show Applications & Delete Applications --- app/api/applications/delete/route.ts | 23 ++ app/api/applications/get/route.ts | 33 +++ app/dashboard/applications/Applications.tsx | 264 +++++++++++------- lib/generated/prisma/edge.js | 12 +- lib/generated/prisma/index-browser.js | 3 +- lib/generated/prisma/index.d.ts | 101 ++++++- lib/generated/prisma/index.js | 12 +- lib/generated/prisma/package.json | 2 +- .../query_engine-windows.dll.node.tmp15728 | Bin 0 -> 20468736 bytes lib/generated/prisma/wasm.js | 3 +- .../migration.sql | 2 + prisma/schema.prisma | 1 + 12 files changed, 345 insertions(+), 111 deletions(-) create mode 100644 app/api/applications/delete/route.ts create mode 100644 app/api/applications/get/route.ts create mode 100644 lib/generated/prisma/query_engine-windows.dll.node.tmp15728 create mode 100644 prisma/migrations/20250411203401_add_created_at_field/migration.sql diff --git a/app/api/applications/delete/route.ts b/app/api/applications/delete/route.ts new file mode 100644 index 0000000..b3aa4d7 --- /dev/null +++ b/app/api/applications/delete/route.ts @@ -0,0 +1,23 @@ +import { NextResponse, NextRequest } from "next/server"; +import { PrismaClient } from '@/lib/generated/prisma' + +const prisma = new PrismaClient(); + +export async function POST(request: NextRequest) { + try { + const body = await request.json(); + const id = Number(body.id); + + if (!id) { + return NextResponse.json({ error: "Missing ID" }, { status: 400 }); + } + + await prisma.application.delete({ + where: { id: id } + }); + + return NextResponse.json({ success: true }); + } catch (error: any) { + return NextResponse.json({ error: error.message }, { status: 500 }); + } +} \ No newline at end of file diff --git a/app/api/applications/get/route.ts b/app/api/applications/get/route.ts new file mode 100644 index 0000000..f481562 --- /dev/null +++ b/app/api/applications/get/route.ts @@ -0,0 +1,33 @@ +import { NextResponse, NextRequest } from "next/server"; +import { PrismaClient } from '@/lib/generated/prisma' + +interface GetRequest { + page: number; +} + +const prisma = new PrismaClient(); +const ITEMS_PER_PAGE = 5; + +export async function POST(request: NextRequest) { + try { + const body: GetRequest = await request.json(); + const page = Math.max(1, body.page || 1); + + const applications = await prisma.application.findMany({ + skip: (page - 1) * ITEMS_PER_PAGE, + take: ITEMS_PER_PAGE, + orderBy: { name: 'asc' } + }); + + // Gesamtanzahl für Seitenberechnung + const totalCount = await prisma.application.count(); + const maxPage = Math.ceil(totalCount / ITEMS_PER_PAGE); + + return NextResponse.json({ + applications, + maxPage + }); + } catch (error: any) { + return NextResponse.json({ error: error.message }, { status: 500 }); + } +} \ No newline at end of file diff --git a/app/dashboard/applications/Applications.tsx b/app/dashboard/applications/Applications.tsx index 4a00a76..017352d 100644 --- a/app/dashboard/applications/Applications.tsx +++ b/app/dashboard/applications/Applications.tsx @@ -16,7 +16,7 @@ import { SidebarTrigger, } from "@/components/ui/sidebar" import { Button } from "@/components/ui/button" -import { Plus, Link, Home } from "lucide-react" // Importiere Icons +import { Plus, Link, Home, Trash2 } from "lucide-react" // Importiere Icons import { Card, CardContent, @@ -26,29 +26,30 @@ import { CardTitle, } from "@/components/ui/card" import { - Pagination, - PaginationContent, - PaginationEllipsis, - PaginationItem, - PaginationLink, - PaginationNext, - PaginationPrevious, } from "@/components/ui/pagination" + Pagination, + PaginationContent, + PaginationEllipsis, + PaginationItem, + PaginationLink, + PaginationNext, + PaginationPrevious, +} from "@/components/ui/pagination" import { -AlertDialog, -AlertDialogAction, -AlertDialogCancel, -AlertDialogContent, -AlertDialogDescription, -AlertDialogFooter, -AlertDialogHeader, -AlertDialogTitle, -AlertDialogTrigger, + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, } from "@/components/ui/alert-dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" -import { useState } from "react"; +import { useState, useEffect } from "react"; import axios from 'axios'; export default function Dashboard() { @@ -58,9 +59,46 @@ export default function Dashboard() { const [publicURL, setPublicURL] = useState(""); const [localURL, setLocalURL] = useState(""); + const [currentPage, setCurrentPage] = useState(1); + const [maxPage, setMaxPage] = useState(1); + const [applications, setApplications] = useState([]); + const add = async () => { try { const response = await axios.post('/api/applications/add', { name, description, icon, publicURL, localURL }); + // Nach erfolgreichem Hinzufügen kannst du auch die Liste neu laden: + getApplications(); + } catch (error: any) { + console.log(error.response.data); + } + } + + const getApplications = async () => { + try { + const response = await axios.post('/api/applications/get', { page: currentPage }); + setApplications(response.data.applications); + setMaxPage(response.data.maxPage); + } catch (error: any) { + console.log(error.response); + } + } + + useEffect(() => { + getApplications(); + }, [currentPage]); + + const handlePrevious = () => { + setCurrentPage(prev => Math.max(1, prev - 1)); + } + + const handleNext = () => { + setCurrentPage(prev => Math.min(maxPage, prev + 1)); + } + + const deleteApplication = async (id: number) => { + try { + await axios.post('/api/applications/delete', { id }); + getApplications(); } catch (error: any) { console.log(error.response.data); } @@ -95,87 +133,125 @@ export default function Dashboard() {
Your Applications - - - - - - Add an application - -

-

- - setName(e.target.value)}/> -
-
- -