From abd8179815893bc73c5d09a06d51764c82c8bdfe Mon Sep 17 00:00:00 2001 From: Cypro Freelance <110410268+Proxyy587@users.noreply.github.com> Date: Fri, 26 Jul 2024 00:45:08 +0530 Subject: [PATCH] Error still in multilogs slug api typerror todo --- app/(auth)/admin/multi-logs/[slug]/page.tsx | 4 +- app/(auth)/admin/multi-logs/page.tsx | 9 ++-- app/api/mdx/pages/[slug]/route.ts | 35 ++++++++++++--- app/api/mdx/pages/route.ts | 48 ++++++--------------- components/shared/Logo.tsx | 15 +------ 5 files changed, 51 insertions(+), 60 deletions(-) diff --git a/app/(auth)/admin/multi-logs/[slug]/page.tsx b/app/(auth)/admin/multi-logs/[slug]/page.tsx index cfba81c..816cac5 100644 --- a/app/(auth)/admin/multi-logs/[slug]/page.tsx +++ b/app/(auth)/admin/multi-logs/[slug]/page.tsx @@ -32,7 +32,7 @@ const EditPage = ({ params }: { params: { slug: string } }) => { const savePage = async () => { setLoading(true); - const response = await fetch(`/api/mdx/pages/${slug}?slug=${slug}`, { + const response = await fetch(`/api/mdx/pages/${slug}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title, content }), @@ -44,7 +44,7 @@ const EditPage = ({ params }: { params: { slug: string } }) => { router.push("/admin/multi-logs"); } else { setLoading(false); - toast({ description: "Page update failed", variant: "destructive" }); + toast({ description: "Updated but cant return data" }); } }; diff --git a/app/(auth)/admin/multi-logs/page.tsx b/app/(auth)/admin/multi-logs/page.tsx index c168e1d..c2a53b5 100644 --- a/app/(auth)/admin/multi-logs/page.tsx +++ b/app/(auth)/admin/multi-logs/page.tsx @@ -1,7 +1,6 @@ "use client"; import React, { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; - import { Table, TableBody, @@ -56,13 +55,13 @@ const AdminLogPage = () => { setPages([...pages, newPage]); setPageTitle(""); setOpen(false); - setLoading(false); toast({ description: "Page created successfully" }); } else { - console.error("Failed to create page"); - toast({ description: "Some Error Occured" }); - setLoading(false); + const errorData = await response.json(); + console.error("Failed to create page:", errorData); + toast({ description: `Error: ${errorData.message}` }); } + setLoading(false); }; return ( diff --git a/app/api/mdx/pages/[slug]/route.ts b/app/api/mdx/pages/[slug]/route.ts index 95e31a3..abd5a12 100644 --- a/app/api/mdx/pages/[slug]/route.ts +++ b/app/api/mdx/pages/[slug]/route.ts @@ -1,13 +1,11 @@ import { NextRequest, NextResponse } from "next/server"; import clientPromise from "@/lib/db"; -export const GET = async ( - req: NextRequest, - { params }: { params: { slug: string } } -) => { +export const GET = async (req: NextRequest) => { const client = await clientPromise; const db = client.db(); - const { slug } = params; + const { searchParams } = new URL(req.url); + const slug = searchParams.get("slug"); if (!slug) { return NextResponse.json({ message: "Slug is required" }, { status: 400 }); @@ -20,3 +18,30 @@ export const GET = async ( return NextResponse.json({ message: "Page not found" }, { status: 404 }); } }; + +export const PUT = async (req: NextRequest) => { + const client = await clientPromise; + const db = client.db(); + const { searchParams } = new URL(req.url); + const slug = searchParams.get("slug"); + const { title, content } = await req.json(); + + if (!slug) { + return NextResponse.json({ message: "Slug is required" }, { status: 400 }); + } + + const result = await db + .collection("pages") + .findOneAndUpdate( + { slug }, + { $set: { title, content } }, + { returnDocument: "after" } + ); + + if (result && result.value) { + const page = result.value; + return NextResponse.json(page, { status: 200 }); + } else { + return NextResponse.json({ message: "Page not found" }, { status: 404 }); + } +}; diff --git a/app/api/mdx/pages/route.ts b/app/api/mdx/pages/route.ts index a0e245d..2143526 100644 --- a/app/api/mdx/pages/route.ts +++ b/app/api/mdx/pages/route.ts @@ -8,48 +8,28 @@ export const GET = async () => { return NextResponse.json(pages, { status: 200 }); }; -export const PUT = async ( - req: NextRequest, - { params }: { params: { slug: string } } -) => { - const client = await clientPromise; - const db = client.db(); - const { slug } = params; - const { title, content } = await req.json(); - - if (!slug) { - return NextResponse.json({ message: "Slug is required" }, { status: 400 }); - } - - const result = await db - .collection("pages") - .findOneAndUpdate( - { slug }, - { $set: { title, content } }, - { returnDocument: "after" } - ); - - if (result && result.value) { - const page = result.value; - return NextResponse.json(page, { status: 200 }); - } else { - return NextResponse.json({ message: "Page not found" }, { status: 404 }); - } -}; - export const POST = async (req: NextRequest) => { const client = await clientPromise; const db = client.db(); const { title, slug, content } = await req.json(); - if (!title || !slug || !content) { + if (!title || !slug || typeof content !== "string") { return NextResponse.json( - { message: "Missing required fields" }, + { message: "Missing required fields or invalid data" }, { status: 400 } ); } - const newPage = { title, slug, content }; - const result = await db.collection("pages").insertOne(newPage); - return NextResponse.json(newPage, { status: 201 }); + try { + const newPage = { title, slug, content }; + const result = await db.collection("pages").insertOne(newPage); + + return NextResponse.json(newPage, { status: 201 }); + } catch (error) { + console.error("Error creating page:", error); + return NextResponse.json( + { message: "Failed to create page" }, + { status: 500 } + ); + } }; diff --git a/components/shared/Logo.tsx b/components/shared/Logo.tsx index 11c86ee..c7303a9 100644 --- a/components/shared/Logo.tsx +++ b/components/shared/Logo.tsx @@ -14,18 +14,6 @@ const Logo = (props: SVGProps) => ( fill="currentColor" d="M53.22 40.315c-2.31 0-4.38-.375-6.21-1.125s-3.3-1.86-4.41-3.33c-1.08-1.47-1.65-3.24-1.71-5.31h8.19c.12 1.17.525 2.07 1.215 2.7.69.6 1.59.9 2.7.9 1.14 0 2.04-.255 2.7-.765.66-.54.99-1.275.99-2.205 0-.78-.27-1.425-.81-1.935-.51-.51-1.155-.93-1.935-1.26-.75-.33-1.83-.705-3.24-1.125-2.04-.63-3.705-1.26-4.995-1.89-1.29-.63-2.4-1.56-3.33-2.79-.93-1.23-1.395-2.835-1.395-4.815 0-2.94 1.065-5.235 3.195-6.885 2.13-1.68 4.905-2.52 8.325-2.52 3.48 0 6.285.84 8.415 2.52 2.13 1.65 3.27 3.96 3.42 6.93H56.01c-.06-1.02-.435-1.815-1.125-2.385-.69-.6-1.575-.9-2.655-.9-.93 0-1.68.255-2.25.765-.57.48-.855 1.185-.855 2.115 0 1.02.48 1.815 1.44 2.385.96.57 2.46 1.185 4.5 1.845 2.04.69 3.69 1.35 4.95 1.98 1.29.63 2.4 1.545 3.33 2.745.93 1.2 1.395 2.745 1.395 4.635 0 1.8-.465 3.435-1.395 4.905-.9 1.47-2.22 2.64-3.96 3.51-1.74.87-3.795 1.305-6.165 1.305ZM98.68 8.41 87.475 40h-9.63L66.642 8.41h8.19l7.83 23.85 7.874-23.85h8.145ZM117.557 40l-6.57-11.925h-1.845V40h-7.695V8.41h12.915c2.49 0 4.605.435 6.345 1.305 1.77.87 3.09 2.07 3.96 3.6.87 1.5 1.305 3.18 1.305 5.04 0 2.1-.6 3.975-1.8 5.625-1.17 1.65-2.91 2.82-5.22 3.51l7.29 12.51h-8.685Zm-8.415-17.37h4.77c1.41 0 2.46-.345 3.15-1.035.72-.69 1.08-1.665 1.08-2.925 0-1.2-.36-2.145-1.08-2.835-.69-.69-1.74-1.035-3.15-1.035h-4.77v7.83Zm24.81 17.73c-1.35 0-2.46-.39-3.33-1.17-.84-.81-1.26-1.8-1.26-2.97 0-1.2.42-2.205 1.26-3.015.87-.81 1.98-1.215 3.33-1.215 1.32 0 2.4.405 3.24 1.215.87.81 1.305 1.815 1.305 3.015 0 1.17-.435 2.16-1.305 2.97-.84.78-1.92 1.17-3.24 1.17Zm28.45-31.95v21.51c0 3.33-.945 5.895-2.835 7.695-1.86 1.8-4.38 2.7-7.56 2.7-3.33 0-6-.945-8.01-2.835-2.01-1.89-3.015-4.575-3.015-8.055h7.65c0 1.32.27 2.325.81 3.015.54.66 1.32.99 2.34.99.93 0 1.65-.3 2.16-.9.51-.6.765-1.47.765-2.61V8.41h7.695Zm17.196 31.905c-2.31 0-4.38-.375-6.21-1.125s-3.3-1.86-4.41-3.33c-1.08-1.47-1.65-3.24-1.71-5.31h8.19c.12 1.17.525 2.07 1.215 2.7.69.6 1.59.9 2.7.9 1.14 0 2.04-.255 2.7-.765.66-.54.99-1.275.99-2.205 0-.78-.27-1.425-.81-1.935-.51-.51-1.155-.93-1.935-1.26-.75-.33-1.83-.705-3.24-1.125-2.04-.63-3.705-1.26-4.995-1.89-1.29-.63-2.4-1.56-3.33-2.79-.93-1.23-1.395-2.835-1.395-4.815 0-2.94 1.065-5.235 3.195-6.885 2.13-1.68 4.905-2.52 8.325-2.52 3.48 0 6.285.84 8.415 2.52 2.13 1.65 3.27 3.96 3.42 6.93h-8.325c-.06-1.02-.435-1.815-1.125-2.385-.69-.6-1.575-.9-2.655-.9-.93 0-1.68.255-2.25.765-.57.48-.855 1.185-.855 2.115 0 1.02.48 1.815 1.44 2.385.96.57 2.46 1.185 4.5 1.845 2.04.69 3.69 1.35 4.95 1.98 1.29.63 2.4 1.545 3.33 2.745.93 1.2 1.395 2.745 1.395 4.635 0 1.8-.465 3.435-1.395 4.905-.9 1.47-2.22 2.64-3.96 3.51-1.74.87-3.795 1.305-6.165 1.305Z" /> - - - - - ) => ( transform="matrix(.15254 0 0 .15255 -3.77 -.913)" />