Edit APplications Function & Edit Application API Fixes

This commit is contained in:
headlessdev 2025-04-14 13:59:36 +02:00
parent 130e282cd6
commit 3bdadab7c6
2 changed files with 402 additions and 149 deletions

View File

@ -5,6 +5,7 @@ interface EditRequest {
id: number; id: number;
name: string; name: string;
description: string; description: string;
serverId: number;
icon: string; icon: string;
publicURL: string; publicURL: string;
localURL: string; localURL: string;
@ -13,16 +14,17 @@ interface EditRequest {
export async function PUT(request: NextRequest) { export async function PUT(request: NextRequest) {
try { try {
const body: EditRequest = await request.json(); const body: EditRequest = await request.json();
const { id, name, description, icon, publicURL, localURL } = body; const { id, name, description, serverId, icon, publicURL, localURL } = body;
const existingServer = await prisma.server.findUnique({ where: { id } }); const existingApp = await prisma.application.findUnique({ where: { id } });
if (!existingServer) { if (!existingApp) {
return NextResponse.json({ error: "Server not found" }, { status: 404 }); return NextResponse.json({ error: "Server not found" }, { status: 404 });
} }
const updatedApplication = await prisma.application.update({ const updatedApplication = await prisma.application.update({
where: { id }, where: { id },
data: { data: {
serverId,
name, name,
description, description,
icon, icon,

View File

@ -16,7 +16,15 @@ import {
SidebarTrigger, SidebarTrigger,
} from "@/components/ui/sidebar"; } from "@/components/ui/sidebar";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Plus, Link, Home, Trash2, LayoutGrid, List } from "lucide-react"; import {
Plus,
Link,
Home,
Trash2,
LayoutGrid,
List,
Pencil,
} from "lucide-react";
import { import {
Card, Card,
CardContent, CardContent,
@ -89,6 +97,15 @@ export default function Dashboard() {
const [publicURL, setPublicURL] = useState<string>(""); const [publicURL, setPublicURL] = useState<string>("");
const [localURL, setLocalURL] = useState<string>(""); const [localURL, setLocalURL] = useState<string>("");
const [serverId, setServerId] = useState<number | null>(null); const [serverId, setServerId] = useState<number | null>(null);
const [editName, setEditName] = useState<string>("");
const [editDescription, setEditDescription] = useState<string>("");
const [editIcon, setEditIcon] = useState<string>("");
const [editPublicURL, setEditPublicURL] = useState<string>("");
const [editLocalURL, setEditLocalURL] = useState<string>("");
const [editId, setEditId] = useState<number | null>(null);
const [editServerId, setEditServerId] = useState<number | null>(null);
const [currentPage, setCurrentPage] = useState<number>(1); const [currentPage, setCurrentPage] = useState<number>(1);
const [maxPage, setMaxPage] = useState<number>(1); const [maxPage, setMaxPage] = useState<number>(1);
const [itemsPerPage, setItemsPerPage] = useState<number>(5); const [itemsPerPage, setItemsPerPage] = useState<number>(5);
@ -98,8 +115,8 @@ export default function Dashboard() {
const [loading, setLoading] = useState<boolean>(true); const [loading, setLoading] = useState<boolean>(true);
useEffect(() => { useEffect(() => {
const savedLayout = Cookies.get('layoutPreference-app'); const savedLayout = Cookies.get("layoutPreference-app");
const layout_bool = savedLayout === 'grid'; const layout_bool = savedLayout === "grid";
setIsGridLayout(layout_bool); setIsGridLayout(layout_bool);
setItemsPerPage(layout_bool ? 15 : 5); setItemsPerPage(layout_bool ? 15 : 5);
}, []); }, []);
@ -107,35 +124,35 @@ export default function Dashboard() {
const toggleLayout = () => { const toggleLayout = () => {
const newLayout = !isGridLayout; const newLayout = !isGridLayout;
setIsGridLayout(newLayout); setIsGridLayout(newLayout);
Cookies.set('layoutPreference-app', newLayout ? 'grid' : 'standard', { Cookies.set("layoutPreference-app", newLayout ? "grid" : "standard", {
expires: 365, expires: 365,
path: '/', path: "/",
sameSite: 'strict' sameSite: "strict",
}); });
setItemsPerPage(newLayout ? 15 : 5); setItemsPerPage(newLayout ? 15 : 5);
}; };
const add = async () => { const add = async () => {
try { try {
await axios.post('/api/applications/add', { await axios.post("/api/applications/add", {
name, name,
description, description,
icon, icon,
publicURL, publicURL,
localURL, localURL,
serverId serverId,
}); });
getApplications(); getApplications();
} catch (error: any) { } catch (error: any) {
console.log(error.response?.data); console.log(error.response?.data);
} }
} };
const getApplications = async () => { const getApplications = async () => {
try { try {
setLoading(true); setLoading(true);
const response = await axios.post<ApplicationsResponse>( const response = await axios.post<ApplicationsResponse>(
'/api/applications/get', "/api/applications/get",
{ page: currentPage, ITEMS_PER_PAGE: itemsPerPage } { page: currentPage, ITEMS_PER_PAGE: itemsPerPage }
); );
setApplications(response.data.applications); setApplications(response.data.applications);
@ -145,23 +162,54 @@ export default function Dashboard() {
} catch (error: any) { } catch (error: any) {
console.log(error.response?.data); console.log(error.response?.data);
} }
} };
useEffect(() => { useEffect(() => {
getApplications(); getApplications();
}, [currentPage, itemsPerPage]); }, [currentPage, itemsPerPage]);
const handlePrevious = () => setCurrentPage(prev => Math.max(1, prev - 1)); const handlePrevious = () => setCurrentPage((prev) => Math.max(1, prev - 1));
const handleNext = () => setCurrentPage(prev => Math.min(maxPage, prev + 1)); const handleNext = () =>
setCurrentPage((prev) => Math.min(maxPage, prev + 1));
const deleteApplication = async (id: number) => { const deleteApplication = async (id: number) => {
try { try {
await axios.post('/api/applications/delete', { id }); await axios.post("/api/applications/delete", { id });
getApplications(); getApplications();
} catch (error: any) { } catch (error: any) {
console.log(error.response?.data); console.log(error.response?.data);
} }
};
const openEditDialog = (app: Application) => {
setEditId(app.id);
setEditServerId(app.serverId);
setEditName(app.name);
setEditDescription(app.description || "");
setEditIcon(app.icon || "");
setEditLocalURL(app.localURL || "");
setEditPublicURL(app.publicURL || "");
};
const edit = async () => {
if (!editId) return;
try {
await axios.put("/api/applications/edit", {
id: editId,
serverId: editServerId,
name: editName,
description: editDescription,
icon: editIcon,
publicURL: editPublicURL,
localURL: editLocalURL,
});
getApplications();
setEditId(null);
} catch (error: any) {
console.log(error.response.data);
} }
};
return ( return (
<SidebarProvider> <SidebarProvider>
@ -196,12 +244,20 @@ export default function Dashboard() {
variant="outline" variant="outline"
size="icon" size="icon"
onClick={toggleLayout} onClick={toggleLayout}
title={isGridLayout ? "Switch to list view" : "Switch to grid view"} title={
isGridLayout ? "Switch to list view" : "Switch to grid view"
}
> >
{isGridLayout ? <List className="h-4 w-4" /> : <LayoutGrid className="h-4 w-4" />} {isGridLayout ? (
<List className="h-4 w-4" />
) : (
<LayoutGrid className="h-4 w-4" />
)}
</Button> </Button>
{servers.length === 0 ? ( {servers.length === 0 ? (
<p className="text-muted-foreground">You must first add a server.</p> <p className="text-muted-foreground">
You must first add a server.
</p>
) : ( ) : (
<AlertDialog> <AlertDialog>
<AlertDialogTrigger asChild> <AlertDialogTrigger asChild>
@ -216,17 +272,26 @@ export default function Dashboard() {
<div className="space-y-4 pt-4"> <div className="space-y-4 pt-4">
<div className="grid w-full items-center gap-1.5"> <div className="grid w-full items-center gap-1.5">
<Label>Name</Label> <Label>Name</Label>
<Input placeholder="e.g. Portainer" onChange={(e) => setName(e.target.value)}/> <Input
placeholder="e.g. Portainer"
onChange={(e) => setName(e.target.value)}
/>
</div> </div>
<div className="grid w-full items-center gap-1.5"> <div className="grid w-full items-center gap-1.5">
<Label>Server</Label> <Label>Server</Label>
<Select onValueChange={(v) => setServerId(Number(v))} required> <Select
onValueChange={(v) => setServerId(Number(v))}
required
>
<SelectTrigger className="w-full"> <SelectTrigger className="w-full">
<SelectValue placeholder="Select server" /> <SelectValue placeholder="Select server" />
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent>
{servers.map((server) => ( {servers.map((server) => (
<SelectItem key={server.id} value={String(server.id)}> <SelectItem
key={server.id}
value={String(server.id)}
>
{server.name} {server.name}
</SelectItem> </SelectItem>
))} ))}
@ -234,27 +299,51 @@ export default function Dashboard() {
</Select> </Select>
</div> </div>
<div className="grid w-full items-center gap-1.5"> <div className="grid w-full items-center gap-1.5">
<Label>Description <span className="text-stone-600">(optional)</span></Label> <Label>
<Textarea placeholder="Application description" onChange={(e) => setDescription(e.target.value)}/> Description{" "}
<span className="text-stone-600">(optional)</span>
</Label>
<Textarea
placeholder="Application description"
onChange={(e) => setDescription(e.target.value)}
/>
</div> </div>
<div className="grid w-full items-center gap-1.5"> <div className="grid w-full items-center gap-1.5">
<Label>Icon URL <span className="text-stone-600">(optional)</span></Label> <Label>
<Input placeholder="https://example.com/icon.png" onChange={(e) => setIcon(e.target.value)}/> Icon URL{" "}
<span className="text-stone-600">(optional)</span>
</Label>
<Input
placeholder="https://example.com/icon.png"
onChange={(e) => setIcon(e.target.value)}
/>
</div> </div>
<div className="grid w-full items-center gap-1.5"> <div className="grid w-full items-center gap-1.5">
<Label>Public URL</Label> <Label>Public URL</Label>
<Input placeholder="https://example.com" onChange={(e) => setPublicURL(e.target.value)}/> <Input
placeholder="https://example.com"
onChange={(e) => setPublicURL(e.target.value)}
/>
</div> </div>
<div className="grid w-full items-center gap-1.5"> <div className="grid w-full items-center gap-1.5">
<Label>Local URL <span className="text-stone-600">(optional)</span></Label> <Label>
<Input placeholder="http://localhost:3000" onChange={(e) => setLocalURL(e.target.value)}/> Local URL{" "}
<span className="text-stone-600">(optional)</span>
</Label>
<Input
placeholder="http://localhost:3000"
onChange={(e) => setLocalURL(e.target.value)}
/>
</div> </div>
</div> </div>
</AlertDialogDescription> </AlertDialogDescription>
</AlertDialogHeader> </AlertDialogHeader>
<AlertDialogFooter> <AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel> <AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction onClick={add} disabled={!name || !publicURL || !serverId}> <AlertDialogAction
onClick={add}
disabled={!name || !publicURL || !serverId}
>
Add Add
</AlertDialogAction> </AlertDialogAction>
</AlertDialogFooter> </AlertDialogFooter>
@ -264,38 +353,59 @@ export default function Dashboard() {
</div> </div>
</div> </div>
<br /> <br />
{!loading ? {!loading ? (
<div className={isGridLayout ? <div
"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4" : className={
"space-y-4"}> isGridLayout
? "grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"
: "space-y-4"
}
>
{applications.map((app) => ( {applications.map((app) => (
<Card <Card
key={app.id} key={app.id}
className={isGridLayout ? "h-full flex flex-col justify-between relative" : "w-full mb-4 relative"} className={
isGridLayout
? "h-full flex flex-col justify-between relative"
: "w-full mb-4 relative"
}
> >
<CardHeader> <CardHeader>
<div className="absolute top-2 right-2"> <div className="absolute top-2 right-2">
<div <div
className={`w-4 h-4 rounded-full flex items-center justify-center ${app.online ? "bg-green-700" : "bg-red-700"}`} className={`w-4 h-4 rounded-full flex items-center justify-center ${
app.online ? "bg-green-700" : "bg-red-700"
}`}
title={app.online ? "Online" : "Offline"} title={app.online ? "Online" : "Offline"}
> >
<div className={`w-2 h-2 rounded-full ${app.online ? "bg-green-500" : "bg-red-500"}`} /> <div
className={`w-2 h-2 rounded-full ${
app.online ? "bg-green-500" : "bg-red-500"
}`}
/>
</div> </div>
</div> </div>
<div className="flex items-center justify-between w-full"> <div className="flex items-center justify-between w-full">
<div className="flex items-center"> <div className="flex items-center">
<div className="w-16 h-16 flex-shrink-0 flex items-center justify-center rounded-md"> <div className="w-16 h-16 flex-shrink-0 flex items-center justify-center rounded-md">
{app.icon ? ( {app.icon ? (
<img src={app.icon} alt={app.name} className="w-full h-full object-contain rounded-md" /> <img
src={app.icon}
alt={app.name}
className="w-full h-full object-contain rounded-md"
/>
) : ( ) : (
<span className="text-gray-500 text-xs">Image</span> <span className="text-gray-500 text-xs">Image</span>
)} )}
</div> </div>
<div className="ml-4"> <div className="ml-4">
<CardTitle className="text-2xl font-bold">{app.name}</CardTitle> <CardTitle className="text-2xl font-bold">
{app.name}
</CardTitle>
<CardDescription className="text-md"> <CardDescription className="text-md">
{app.description}<br /> {app.description}
Server: {app.server || 'No server'} <br />
Server: {app.server || "No server"}
</CardDescription> </CardDescription>
</div> </div>
</div> </div>
@ -305,7 +415,9 @@ export default function Dashboard() {
<Button <Button
variant="outline" variant="outline"
className="gap-2 w-full" className="gap-2 w-full"
onClick={() => window.open(app.publicURL, "_blank")} onClick={() =>
window.open(app.publicURL, "_blank")
}
> >
<Link className="h-4 w-4" /> <Link className="h-4 w-4" />
Open Public URL Open Public URL
@ -314,7 +426,9 @@ export default function Dashboard() {
<Button <Button
variant="outline" variant="outline"
className="gap-2 w-full" className="gap-2 w-full"
onClick={() => window.open(app.localURL, "_blank")} onClick={() =>
window.open(app.localURL, "_blank")
}
> >
<Home className="h-4 w-4" /> <Home className="h-4 w-4" />
Open Local URL Open Local URL
@ -329,6 +443,132 @@ export default function Dashboard() {
> >
<Trash2 className="h-4 w-4" /> <Trash2 className="h-4 w-4" />
</Button> </Button>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
size="icon"
className="h-[72px] w-10"
onClick={() => openEditDialog(app)}
>
<Pencil className="h-4 w-4" />
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
Edit Application
</AlertDialogTitle>
<AlertDialogDescription>
<div className="space-y-4 pt-4">
<div className="grid w-full items-center gap-1.5">
<Label>Name</Label>
<Input
placeholder="e.g. Portainer"
value={editName}
onChange={(e) =>
setEditName(e.target.value)
}
/>
</div>
<div className="grid w-full items-center gap-1.5">
<Label>Server</Label>
<Select
value={
editServerId !== null
? String(editServerId)
: undefined
}
onValueChange={(v) =>
setEditServerId(Number(v))
}
required
>
<SelectTrigger className="w-full">
<SelectValue placeholder="Select server" />
</SelectTrigger>
<SelectContent>
{servers.map((server) => (
<SelectItem
key={server.id}
value={String(server.id)}
>
{server.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="grid w-full items-center gap-1.5">
<Label>
Description{" "}
<span className="text-stone-600">
(optional)
</span>
</Label>
<Textarea
placeholder="Application description"
value={editDescription}
onChange={(e) =>
setEditDescription(e.target.value)
}
/>
</div>
<div className="grid w-full items-center gap-1.5">
<Label>
Icon URL{" "}
<span className="text-stone-600">
(optional)
</span>
</Label>
<Input
placeholder="https://example.com/icon.png"
value={editIcon}
onChange={(e) =>
setEditIcon(e.target.value)
}
/>
</div>
<div className="grid w-full items-center gap-1.5">
<Label>Public URL</Label>
<Input
placeholder="https://example.com"
value={editPublicURL}
onChange={(e) =>
setEditPublicURL(e.target.value)
}
/>
</div>
<div className="grid w-full items-center gap-1.5">
<Label>
Local URL{" "}
<span className="text-stone-600">
(optional)
</span>
</Label>
<Input
placeholder="http://localhost:3000"
value={editLocalURL}
onChange={(e) =>
setEditLocalURL(e.target.value)
}
/>
</div>
</div>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={edit}
disabled={
!editName || !editPublicURL || !editServerId
}
>
Save Changes
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div> </div>
</div> </div>
</div> </div>
@ -336,23 +576,34 @@ export default function Dashboard() {
</Card> </Card>
))} ))}
</div> </div>
: ) : (
<div className="flex items-center justify-center"> <div className="flex items-center justify-center">
<div className='inline-block' role='status' aria-label='loading'> <div className="inline-block" role="status" aria-label="loading">
<svg className='w-6 h-6 stroke-white animate-spin ' viewBox='0 0 24 24' fill='none' xmlns='http://www.w3.org/2000/svg'> <svg
<g clip-path='url(#clip0_9023_61563)'> className="w-6 h-6 stroke-white animate-spin "
<path d='M14.6437 2.05426C11.9803 1.2966 9.01686 1.64245 6.50315 3.25548C1.85499 6.23817 0.504864 12.4242 3.48756 17.0724C6.47025 21.7205 12.6563 23.0706 17.3044 20.088C20.4971 18.0393 22.1338 14.4793 21.8792 10.9444' stroke='stroke-current' stroke-width='1.4' stroke-linecap='round' className='my-path'></path> viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<g clip-path="url(#clip0_9023_61563)">
<path
d="M14.6437 2.05426C11.9803 1.2966 9.01686 1.64245 6.50315 3.25548C1.85499 6.23817 0.504864 12.4242 3.48756 17.0724C6.47025 21.7205 12.6563 23.0706 17.3044 20.088C20.4971 18.0393 22.1338 14.4793 21.8792 10.9444"
stroke="stroke-current"
stroke-width="1.4"
stroke-linecap="round"
className="my-path"
></path>
</g> </g>
<defs> <defs>
<clipPath id='clip0_9023_61563'> <clipPath id="clip0_9023_61563">
<rect width='24' height='24' fill='white'></rect> <rect width="24" height="24" fill="white"></rect>
</clipPath> </clipPath>
</defs> </defs>
</svg> </svg>
<span className='sr-only'>Loading...</span> <span className="sr-only">Loading...</span>
</div> </div>
</div> </div>
} )}
<div className="pt-4"> <div className="pt-4">
<Pagination> <Pagination>
<PaginationContent> <PaginationContent>
@ -379,5 +630,5 @@ export default function Dashboard() {
</div> </div>
</SidebarInset> </SidebarInset>
</SidebarProvider> </SidebarProvider>
) );
} }