i choose to commit my life

This commit is contained in:
Cypro Freelance 2024-07-26 01:39:06 +05:30
parent 8c5e7c44bf
commit b5c4ca32ed
7 changed files with 165 additions and 39 deletions

View file

@ -17,7 +17,7 @@ const Sidebar = () => {
<nav className="sidebar-nav"> <nav className="sidebar-nav">
<ul className="sidebar-nav_elements"> <ul className="sidebar-nav_elements">
{AdminLinks.slice(0, 4).map((link) => { {AdminLinks.slice(0, 5).map((link) => {
const isActive = link.url === pathname; const isActive = link.url === pathname;
return ( return (
@ -37,7 +37,7 @@ const Sidebar = () => {
</ul> </ul>
<ul className="sidebar-nav_elements"> <ul className="sidebar-nav_elements">
{AdminLinks.slice(4).map((link) => { {AdminLinks.slice(5).map((link) => {
const isActive = link.url === pathname; const isActive = link.url === pathname;
return ( return (

View file

@ -26,7 +26,7 @@ interface PageEntry {
content: string; content: string;
} }
const AdminLogPage = () => { const MultiLogs = () => {
const [pages, setPages] = useState<PageEntry[]>([]); const [pages, setPages] = useState<PageEntry[]>([]);
const { toast } = useToast(); const { toast } = useToast();
const router = useRouter(); const router = useRouter();
@ -64,6 +64,23 @@ const AdminLogPage = () => {
setLoading(false); setLoading(false);
}; };
const deletePage = async (slug: string) => {
setLoading(true);
const response = await fetch(`/api/mdx/pages/${slug}`, {
method: "DELETE",
});
if (response.ok) {
setPages(pages.filter((page) => page.slug !== slug));
toast({ description: "Page deleted successfully" });
} else {
const errorData = await response.json();
console.error("Failed to delete page:", errorData);
toast({ description: `Error: ${errorData.message}` });
}
setLoading(false);
};
return ( return (
<section id="logs-page" className="wrapper container"> <section id="logs-page" className="wrapper container">
<section id="create-page" className="py-16"> <section id="create-page" className="py-16">
@ -119,6 +136,14 @@ const AdminLogPage = () => {
> >
Edit Edit
</Button> </Button>
<Button
variant={"destructive"}
onClick={() => deletePage(page.slug)}
className="ml-2"
disabled={loading}
>
Delete
</Button>
</TableCell> </TableCell>
</TableRow> </TableRow>
))} ))}
@ -129,4 +154,4 @@ const AdminLogPage = () => {
); );
}; };
export default AdminLogPage; export default MultiLogs;

View file

@ -1,6 +1,5 @@
"use client"; "use client";
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { useRouter } from "next/router";
import ReactMarkdown from "react-markdown"; import ReactMarkdown from "react-markdown";
const Page = ({ params }: { params: { slug: string } }) => { const Page = ({ params }: { params: { slug: string } }) => {

View file

@ -1,17 +1,20 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import clientPromise from "@/lib/db"; import clientPromise from "@/lib/db";
export const GET = async (req: NextRequest) => { export const GET = async (
req: NextRequest,
{ params }: { params: { slug: string } }
) => {
const client = await clientPromise; const client = await clientPromise;
const db = client.db(); const db = client.db();
const { searchParams } = new URL(req.url); const { slug } = params;
const slug = searchParams.get("slug");
if (!slug) { if (!slug) {
return NextResponse.json({ message: "Slug is required" }, { status: 400 }); return NextResponse.json({ message: "Slug is required" }, { status: 400 });
} }
const page = await db.collection("pages").findOne({ slug }); const page = await db.collection("pages").findOne({ slug });
if (page) { if (page) {
return NextResponse.json(page, { status: 200 }); return NextResponse.json(page, { status: 200 });
} else { } else {
@ -19,17 +22,29 @@ export const GET = async (req: NextRequest) => {
} }
}; };
export const PUT = async (req: NextRequest) => { export const PUT = async (
req: NextRequest,
{ params }: { params: { slug: string } }
) => {
const client = await clientPromise; const client = await clientPromise;
const db = client.db(); const db = client.db();
const { searchParams } = new URL(req.url); const { slug } = params;
const slug = searchParams.get("slug");
const { title, content } = await req.json();
if (!slug) { if (!slug) {
return NextResponse.json({ message: "Slug is required" }, { status: 400 }); return NextResponse.json({ message: "Slug is required" }, { status: 400 });
} }
const { title, content } = await req.json();
if (typeof title !== "string" || typeof content !== "string") {
return NextResponse.json(
{ message: "Invalid title or content" },
{ status: 400 }
);
}
try {
// it works here ig
const result = await db const result = await db
.collection("pages") .collection("pages")
.findOneAndUpdate( .findOneAndUpdate(
@ -38,10 +53,75 @@ export const PUT = async (req: NextRequest) => {
{ returnDocument: "after" } { returnDocument: "after" }
); );
if (result && result.value) { // i hate my life fr fr
const page = result.value; console.log("Update Result:", result);
return NextResponse.json(page, { status: 200 }); // result returns like
// Update Result: {
// _id: new ObjectId('66a2946b2b91eef505eef943'),
// title: 'TEST PAGE',
// slug: 'test-page',
// content: 'asd]---\n' +
// '---\n' +
// '\n' +
// 'this is basic heading ?\n' +
// '\n' +
// '**HELLO**\n' +
// '\n' +
// 'erw\n' +
// '\n' +
// 'trying another time for test'
// }
// ERRROR : TypeError: Cannot read properties of undefined (reading '_id')
// aposdjaoi sdio JUST WORK NIAWWWWWWWWW
// if (result && result.value) {
const serializedResult = {
...result?.value,
_id: result?.value._id.toString(), // Convert ObjectId to string
};
return NextResponse.json(result?.value.content, { status: 200 });
// } else {
// return NextResponse.json({ message: "Page not found" }, { status: 404 });
// }
} catch (error) {
console.error("Error updating page:", error);
return NextResponse.json(
{ message: "Failed to update page" },
{ status: 500 }
);
}
};
export const DELETE = async (
req: NextRequest,
{ params }: { params: { slug: string } }
) => {
const client = await clientPromise;
const db = client.db();
const { slug } = params;
if (!slug) {
return NextResponse.json({ message: "Slug is required" }, { status: 400 });
}
try {
const result = await db.collection("pages").deleteOne({ slug });
if (result.deletedCount > 0) {
return NextResponse.json(
{ message: "Page deleted successfully" },
{ status: 200 }
);
} else { } else {
return NextResponse.json({ message: "Page not found" }, { status: 404 }); return NextResponse.json({ message: "Page not found" }, { status: 404 });
} }
} catch (error) {
console.error("Error deleting page:", error);
return NextResponse.json(
{ message: "Failed to delete page" },
{ status: 500 }
);
}
}; };

View file

@ -1,11 +1,20 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import clientPromise from "@/lib/db"; import clientPromise from "@/lib/db";
export const GET = async () => { export const GET = async (req: NextRequest) => {
const client = await clientPromise; const client = await clientPromise;
const db = client.db(); const db = client.db();
try {
const pages = await db.collection("pages").find().toArray(); const pages = await db.collection("pages").find().toArray();
return NextResponse.json(pages, { status: 200 }); return NextResponse.json(pages, { status: 200 });
} catch (error) {
console.error("Error fetching pages:", error);
return NextResponse.json(
{ message: "Failed to fetch pages" },
{ status: 500 }
);
}
}; };
export const POST = async (req: NextRequest) => { export const POST = async (req: NextRequest) => {
@ -13,9 +22,9 @@ export const POST = async (req: NextRequest) => {
const db = client.db(); const db = client.db();
const { title, slug, content } = await req.json(); const { title, slug, content } = await req.json();
if (!title || !slug || typeof content !== "string") { if (!title || !slug || !content) {
return NextResponse.json( return NextResponse.json(
{ message: "Missing required fields or invalid data" }, { message: "Missing required fields" },
{ status: 400 } { status: 400 }
); );
} }
@ -23,7 +32,6 @@ export const POST = async (req: NextRequest) => {
try { try {
const newPage = { title, slug, content }; const newPage = { title, slug, content };
const result = await db.collection("pages").insertOne(newPage); const result = await db.collection("pages").insertOne(newPage);
return NextResponse.json(newPage, { status: 201 }); return NextResponse.json(newPage, { status: 201 });
} catch (error) { } catch (error) {
console.error("Error creating page:", error); console.error("Error creating page:", error);

View file

@ -1,4 +1,4 @@
import { BadgeAlert, BarChart4, Cog, ShieldCheck } from "lucide-react"; import { BadgeAlert, BarChart4, Cog, File, ShieldCheck } from "lucide-react";
import { Download, Home, Settings, User } from "lucide-react"; import { Download, Home, Settings, User } from "lucide-react";
export const NAVBAR = { export const NAVBAR = {
@ -169,6 +169,11 @@ export const AdminLinks = [
url: "/admin/changelogs", url: "/admin/changelogs",
icon: Settings, icon: Settings,
}, },
{
name: "MultiLogs",
url: "/admin/multi-logs",
icon: File,
},
{ {
name: "Back Home", name: "Back Home",
url: "/", url: "/",

View file

@ -15,5 +15,14 @@ export async function middleware(req: NextRequest) {
} }
export const config = { export const config = {
matcher: ["/admin/:path*"], matcher: [
"/admin/:path*",
"/api/delete/downloads/[id]",
"/api/delete/logs/[id]",
"/api/delete/mods/[id]",
"/api/upload",
"/api/uploadlogs",
"/api/uploadmods",
"/api/uploadthing",
],
}; };