manage markdowns by path

This commit is contained in:
h z
2024-12-05 18:28:15 +00:00
parent 788fd2f37a
commit da1860a269
12 changed files with 441 additions and 198 deletions

View File

@@ -1,7 +1,6 @@
import React, { useContext, useEffect, useState } from "react";
import { AuthContext } from "../../AuthProvider";
import { useNavigate, useParams } from "react-router-dom";
import { fetchWithCache } from "../../utils/fetchWithCache";
import ReactMarkdown from "react-markdown";
import remarkMath from "remark-math";
import rehypeKatex from "rehype-katex";
@@ -11,6 +10,7 @@ import { okaidia } from "react-syntax-highlighter/dist/esm/styles/prism";
import "katex/dist/katex.min.css";
import "./MarkdownEditor.css";
import config from "../../config";
import {fetch_} from "../../utils/requestUtils";
const MarkdownEditor = () => {
const { roles } = useContext(AuthContext);
@@ -22,7 +22,10 @@ const MarkdownEditor = () => {
useEffect(() => {
if (id) {
fetchWithCache(`/api/markdown/${id}`)
fetch_(`/api/markdown/${id}`, {}, {
use_cache: true,
use_token: false
})
.then((data) => {
setTitle(data.title);
setContent(data.content);
@@ -37,26 +40,23 @@ const MarkdownEditor = () => {
const handleSave = () => {
const url = id ? `${config.BACKEND_HOST}/api/markdown/${id}` : `${config.BACKEND_HOST}/api/markdown`;
const method = id ? "PUT" : "POST";
fetch(url, {
fetch_(url, {
method,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("accessToken")}`,
},
body: JSON.stringify({ title, content, path }),
}, {
use_cache: false,
use_token: true,
}).then((res) => {
if(res.ok)
navigate("/");
else
return res.json().then((data) => {
throw new Error(data.error || "Failed to load markdown");
});
}).catch((err) => {
console.error("failed to load markdown", err);
})
.then((res) => {
if (res.ok) {
navigate("/");
} else {
return res.json().then((data) => {
throw new Error(data.error || "Failed to save markdown");
});
}
})
.catch((err) => {
console.error("failed to load markdown", err);
});
};
const hasPermission = roles.includes("admin") || roles.includes("creator");