diff --git a/app/(auth)/admin/changelogs/page.tsx b/app/(auth)/admin/changelogs/page.tsx index a93cbf9..3275e59 100644 --- a/app/(auth)/admin/changelogs/page.tsx +++ b/app/(auth)/admin/changelogs/page.tsx @@ -1,6 +1,6 @@ "use client"; -import React from "react"; +import React, { useEffect, useState } from "react"; import { useForm, SubmitHandler, useFieldArray } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Button } from "@/components/ui/button"; @@ -12,13 +12,35 @@ import { FormLabel, FormMessage, } from "@/components/ui/form"; +import { + Table, + TableBody, + TableCaption, + TableCell, + TableHead, + TableHeader, + TableRow, +} from "@/components/ui/table"; import { Input } from "@/components/ui/input"; import { logsSchema } from "@/lib/validations/validation"; import { z } from "zod"; +import { useToast } from "@/components/ui/use-toast"; + +interface LogEntry { + _id: string; + version: string; + date: string; + bullets: { point: string }[]; +} type LogsFormValues = z.infer; const AdminLogPage = () => { + const [logs, setLogs] = useState([]); + const [error, setError] = useState(""); + const { toast } = useToast(); + const [loading, setLoading] = useState(false); + const form = useForm({ resolver: zodResolver(logsSchema), defaultValues: { @@ -33,7 +55,33 @@ const AdminLogPage = () => { name: "bullets", }); + const fetchLogs = async () => { + try { + const response = await fetch("/api/logs", { + method: "GET", + }); + if (response.ok) { + const data: LogEntry[] = await response.json(); + setLogs(data); + } else { + throw new Error(`HTTP error! status: ${response.status}`); + } + } catch (error: any) { + setError(error.message || "Failed to fetch logs"); + } + }; + + useEffect(() => { + fetchLogs(); + const interval = setInterval(() => { + fetchLogs(); + }, 10000); + + return () => clearInterval(interval); + }, []); + const onSubmit: SubmitHandler = async (data) => { + setLoading(true); const response = await fetch("/api/uploadlogs", { method: "POST", headers: { @@ -44,11 +92,30 @@ const AdminLogPage = () => { if (response.ok) { form.reset(); + fetchLogs(); + setLoading(false); + toast({ description: "Logs successfully added" }); console.log("Upload successful"); - alert("Uploaded"); } else { console.error("Upload failed"); - alert("Upload Failed"); + setLoading(false); + toast({ description: "Upload Failed", variant: "destructive" }); + } + }; + + const deleteLog = async (id: string) => { + try { + const response = await fetch(`/api/delete/${id}`, { + method: "DELETE", + }); + if (response.ok) { + fetchLogs(); + console.log("Delete successful"); + } else { + throw new Error(`HTTP error! status: ${response.status}`); + } + } catch (error: any) { + setError(error.message || "Failed to delete log"); } }; @@ -83,6 +150,7 @@ const AdminLogPage = () => { )} /> + {fields.map((field, index) => ( { + + {/* Section to list and delete logs */} +
+

Existing Logs

+ {error &&

{error}

} + + + + Version + Date + Actions + + + + {logs.map((log) => ( + + + {log.version} + + {log.date} + + + + + ))} + +
+
); }; diff --git a/app/(auth)/admin/downloads/page.tsx b/app/(auth)/admin/downloads/page.tsx index 0c82ecf..9f9b2d2 100644 --- a/app/(auth)/admin/downloads/page.tsx +++ b/app/(auth)/admin/downloads/page.tsx @@ -16,8 +16,11 @@ import { import { Input } from "@/components/ui/input"; import { UploadButton, UploadDropzone } from "@/lib/uploadthing"; import { downloadSchema } from "@/lib/validations/validation"; +import { useToast } from "@/components/ui/use-toast"; const DownloadsPage = () => { + const { toast } = useToast(); + const form = useForm>({ resolver: zodResolver(downloadSchema), defaultValues: { @@ -41,11 +44,11 @@ const DownloadsPage = () => { if (response.ok) { form.reset(); + toast({ description: "Download Sucessfully Updated" }); console.log("Upload successful"); - alert("Uploaded"); } else { console.error("Upload failed"); - alert("Upload Failed"); + toast({ description: "Uploading Failed", variant: "destructive" }); } }; diff --git a/app/(auth)/admin/mods/page.tsx b/app/(auth)/admin/mods/page.tsx index c48f53f..91c9133 100644 --- a/app/(auth)/admin/mods/page.tsx +++ b/app/(auth)/admin/mods/page.tsx @@ -16,8 +16,10 @@ import { import { Input } from "@/components/ui/input"; import { UploadButton, UploadDropzone } from "@/lib/uploadthing"; import { modsSchema } from "@/lib/validations/validation"; +import { useToast } from "@/components/ui/use-toast"; const SvrjsModsAdminPage = () => { + const { toast } = useToast(); const form = useForm>({ resolver: zodResolver(modsSchema), defaultValues: { @@ -39,11 +41,16 @@ const SvrjsModsAdminPage = () => { if (response.ok) { form.reset(); + toast({ + description: "Successfully Uploaded Mods", + }); console.log("Upload successful"); - alert("Uploaded"); } else { console.error("Upload failed"); - alert("Upload Failed"); + toast({ + description: "Upload failed", + variant: "destructive", + }); } }; diff --git a/app/api/delete/[id]/route.ts b/app/api/delete/[id]/route.ts new file mode 100644 index 0000000..f53ff7d --- /dev/null +++ b/app/api/delete/[id]/route.ts @@ -0,0 +1,30 @@ +// app/api/delete/[id]/route.ts +import clientPromise from "@/lib/db"; +import { ObjectId } from "mongodb"; +import { NextResponse } from "next/server"; + +export async function DELETE( + request: Request, + { params }: { params: { id: string } } +) { + const { id } = params; + + try { + const client = await clientPromise; + const db = client.db("downloadsDatabase"); + const collection = db.collection("logs"); + + const result = await collection.deleteOne({ _id: new ObjectId(id) }); + + if (result.deletedCount === 1) { + return NextResponse.json({ message: "Log deleted successfully" }); + } else { + return NextResponse.json({ message: "Log not found" }, { status: 404 }); + } + } catch (error) { + return NextResponse.json( + { message: "Failed to delete log", error: error }, + { status: 500 } + ); + } +} diff --git a/app/favicon.ico b/app/favicon.ico index b511e44..c5d08a7 100644 Binary files a/app/favicon.ico and b/app/favicon.ico differ diff --git a/app/layout.tsx b/app/layout.tsx index d723b8e..b2a35a9 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -30,7 +30,7 @@ export default function RootLayout({ disableTransitionOnChange > - {children} +
{children}
diff --git a/bun.lockb b/bun.lockb index 01fd721..ebcad42 100644 Binary files a/bun.lockb and b/bun.lockb differ