feat: ability to update blog

This commit is contained in:
2025-06-24 20:38:24 -04:00
parent 26bb7de018
commit 7242579c17
6 changed files with 143 additions and 12 deletions
+14 -4
View File
@@ -1,5 +1,5 @@
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { useNavigate, useParams } from "react-router-dom";
import { API_URL } from "./constants";
import type { Blog } from "./types";
import Markdown from "react-markdown";
@@ -11,6 +11,8 @@ import "../styles/markdown.css";
export function BlogViewer() {
const { slug } = useParams<{ slug: string }>();
const [blog, setBlog] = useState<Blog | null>(null);
const navigate = useNavigate();
const me = localStorage.getItem("user_id");
useEffect(() => {
if (!slug) return;
@@ -20,12 +22,20 @@ export function BlogViewer() {
.catch((err) => console.error(err));
}, [slug]);
if (!blog) {
return <p>Loading...</p>;
}
if (!blog) return <p>Loading</p>;
const isAuthor = me === String(blog.author_id);
return (
<div className="max-w-2xl mx-auto py-10 space-y-6">
{isAuthor && (
<button
onClick={() => navigate(`/blog/${slug}/edit`)}
className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700"
>
Edit this post
</button>
)}
<h1 className="text-3xl font-bold">{blog.title.toUpperCase()}</h1>
<p className="text-sm text-gray-500">By User {blog.author_id}</p>
<p className="italic text-gray-600 dark:text-gray-400">