2024-07-20 21:04:38 +02:00
|
|
|
"use client";
|
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
import dynamic from "next/dynamic";
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import { useToast } from "@/components/ui/use-toast";
|
|
|
|
|
|
|
|
const MarkdownEditor = dynamic(() => import("@uiw/react-md-editor"), {
|
2024-09-07 09:12:48 +02:00
|
|
|
ssr: false
|
2024-07-20 21:04:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const EditPage = ({ params }: { params: { slug: string } }) => {
|
2024-09-07 09:12:48 +02:00
|
|
|
const router = useRouter();
|
|
|
|
const { slug } = params;
|
|
|
|
const { toast } = useToast();
|
|
|
|
const [title, setTitle] = useState("");
|
|
|
|
const [content, setContent] = useState("");
|
|
|
|
const [vulnerabilities, setVulnerabilities] = useState("");
|
|
|
|
const [loading, setLoading] = useState(false);
|
2024-07-20 21:04:38 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (slug) {
|
|
|
|
fetch(`/api/mdx/pages/${slug}`)
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => {
|
|
|
|
setTitle(data.title);
|
|
|
|
setContent(data.content);
|
|
|
|
setVulnerabilities(data.vulnerabilities || "");
|
|
|
|
})
|
|
|
|
.catch((error) => console.error("Failed to load page", error));
|
|
|
|
}
|
|
|
|
}, [slug]);
|
2024-07-20 21:04:38 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const savePage = async () => {
|
|
|
|
setLoading(true);
|
|
|
|
const response = await fetch(`/api/mdx/pages/${slug}`, {
|
|
|
|
method: "PUT",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify({ title, content, vulnerabilities })
|
|
|
|
});
|
2024-07-20 21:04:38 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
if (response.ok) {
|
|
|
|
setLoading(false);
|
|
|
|
toast({ description: "Page successfully updated" });
|
|
|
|
router.push(`/admin/multi-logs/`);
|
|
|
|
} else {
|
|
|
|
setLoading(false);
|
|
|
|
toast({ description: "Page Updated" });
|
|
|
|
}
|
|
|
|
};
|
2024-07-20 21:04:38 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const handleEditorChange = (value?: string) => {
|
|
|
|
if (value !== undefined) {
|
|
|
|
setContent(value);
|
|
|
|
}
|
|
|
|
};
|
2024-07-20 21:04:38 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
return (
|
|
|
|
<section id="edit-page" className="wrapper container gap-4">
|
|
|
|
<h1 className="text-3xl font-bold py-6">Edit Page: {slug}</h1>
|
|
|
|
<Input
|
|
|
|
value={title}
|
|
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
|
|
placeholder="Page Title"
|
|
|
|
/>
|
|
|
|
<MarkdownEditor
|
|
|
|
value={content}
|
|
|
|
onChange={handleEditorChange}
|
|
|
|
height={560}
|
|
|
|
/>
|
|
|
|
| 502 Bad Gateway
The server had an error while it was acting as a gateway.
You may need to contact the server administrator at webmaster[at]svrjs[dot]org.
SVR.JS on git.svrjs.org