Error still in multilogs slug api typerror todo

This commit is contained in:
Cypro Freelance 2024-07-26 00:45:08 +05:30
parent 8056b67072
commit abd8179815
5 changed files with 51 additions and 60 deletions

View file

@ -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" });
}
};

View file

@ -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 (

View file

@ -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 });
}
};

View file

@ -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 }
);
}
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 }
);
}
};

View file

@ -14,18 +14,6 @@ const Logo = (props: SVGProps<SVGSVGElement>) => (
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"
/>
<defs>
<clipPath id="a" clipPathUnits="userSpaceOnUse">
<path
d="M36.061 266.692H56.36v18.147H36.061z"
style={{
fill: "purple",
fillOpacity: 1,
strokeWidth: 0.264583,
}}
/>
</clipPath>
</defs>
<path
d="M42.32 18.844V7.992h14.126v21.702H42.32Z"
style={{
@ -76,8 +64,7 @@ const Logo = (props: SVGProps<SVGSVGElement>) => (
transform="matrix(.15254 0 0 .15255 -3.77 -.913)"
/>
<path
d="M42.415 280.972c-.581-.136-1.244-.507-1.76-.986-.558-.516-.824-.918-1.114-1.68-.184-.485-.19-.592-.186-3.422.005-3.115.04-3.468.427-4.285.26-.552 1.02-1.398 1.514-1.687.833-.488 1.247-.575 2.753-.575s1.92.087 2.753.575c.494.29 1.253 1.135 1.514 1.687.387.817.422 1.17.427 4.285.004 2.83-.002 2.937-.186 3.423-.29.761-.556 1.163-1.114 1.68-.533.493-1.171.845-1.795.988-.452.105-2.787.102-3.233-.003zM32.28 264.383c-9.32-.055-8.772-.018-10.117-.672-1.642-.798-2.859-2.304-3.32-4.109-.144-.562-.152-1.79-.152-24.088 0-22.298.008-23.525.152-24.087a6.293 6.293 0 0 1 3.32-4.11c1.42-.69-.52-.632 21.209-.632 21.728 0 19.79-.057 21.209.633 1.641.798 2.858 2.304 3.32 4.109.143.562.15 1.79.15 24.087 0 22.298-.007 23.526-.15 24.088-.462 1.805-1.68 3.311-3.32 4.109-.886.43-1.555.582-2.794.632-1.669.067-20.548.093-29.507.04zm24.451-35.887c.555-.283.865-.787.865-1.406 0-.9-.684-1.566-1.61-1.566-.924 0-1.608.666-1.608 1.565 0 .409.213.923.478 1.155.495.433 1.306.542 1.875.252zm3.64 0c.555-.283.865-.787.865-1.406 0-.9-.683-1.566-1.608-1.566s-1.61.666-1.61 1.565c0 .409.214.923.479 1.155.495.433 1.306.542 1.875.252zm3.726 0c.555-.283.865-.787.865-1.406 0-.9-.684-1.566-1.61-1.566-.924 0-1.608.666-1.608 1.565 0 .409.213.923.478 1.155.495.433 1.306.542 1.875.252zm-7.472-7.11c.636-.29 1.054-1.023.937-1.644-.113-.601-.692-1.183-1.288-1.295-1.097-.206-2.13.882-1.82 1.916.28.936 1.302 1.417 2.171 1.022zm3.747-.002c.554-.283.864-.787.864-1.406 0-.9-.683-1.566-1.608-1.566s-1.61.666-1.61 1.565c0 .41.214.923.479 1.155.495.433 1.306.543 1.875.252zm3.62.001c.635-.288 1.053-1.022.936-1.643-.113-.601-.692-1.183-1.288-1.295-1.097-.206-2.13.882-1.82 1.916.28.936 1.302 1.417 2.171 1.022zm-7.367-7.027c.636-.289 1.054-1.022.937-1.644-.113-.6-.692-1.183-1.288-1.295-1.097-.205-2.13.883-1.82 1.917.28.936 1.302 1.417 2.171 1.022zm3.641 0c.636-.289 1.053-1.022.937-1.644-.113-.6-.692-1.183-1.289-1.295-1.096-.205-2.129.883-1.82 1.917.281.936 1.303 1.417 2.172 1.022zm3.725 0c.636-.289 1.054-1.022.937-1.644-.113-.6-.692-1.183-1.288-1.295-1.097-.205-2.13.883-1.82 1.917.28.936 1.302 1.417 2.171 1.022z"
clipPath="url(#a)"
d="M42.415 280.972c-.581-.136-1.244-.507-1.76-.986-.558-.516-.824-.918-1.114-1.68-.184-.485-.19-.592-.186-3.422.005-3.115.04-3.468.427-4.285.26-.552 1.02-1.398 1.514-1.687.833-.488 1.247-.575 2.753-.575s1.92.087 2.753.575c.494.29 1.253 1.135 1.514 1.687.387.817.422 1.17.427 4.285.004 2.83-.002 2.937-.186 3.423-.29.761-.556 1.163-1.114 1.68-.533.493-1.171.845-1.795.988-.452.105-2.787.102-3.233-.003z"
style={{
fill: "#fd7f00",
strokeWidth: 0.0846667,