Skip to content

Commit 97d8cd8

Browse files
committed
chore: hide delete for non-author
1 parent 0a84933 commit 97d8cd8

File tree

4 files changed

+45
-29
lines changed

4 files changed

+45
-29
lines changed

modules/home/home.handler.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@ export default async function homeHandler(req: HttpRequest, ctx: Context) {
2020

2121
const avatar_url = ses?.avatar_url;
2222
const html_url = ses?.html_url;
23+
const author = ses?.username;
2324
// Get posts for the initial page load
2425
const posts = await getPosts();
2526
console.log("Posts data:", JSON.stringify(posts, null, 2));
2627
console.log("Number of posts:", posts.length);
28+
//**
29+
// {
30+
// "id": "01JRF7X2TM6PPRJ52VP98R6ATJ",
31+
// "content": "okeeeee",
32+
// "timestamp": "2025-04-10T06:58:51.860Z",
33+
// "author": "ynwd"
34+
// },
35+
// */
2736

2837
return await ctx.render({
2938
title: "Home",
@@ -32,6 +41,7 @@ export default async function homeHandler(req: HttpRequest, ctx: Context) {
3241
isLogin,
3342
avatar_url,
3443
html_url,
44+
author,
3545
posts,
3646
});
3747
}

modules/home/home.page.tsx

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default function Home({ data }: PageProps<{
1919
isLogin: boolean;
2020
avatar_url: string;
2121
html_url: string;
22+
author: string;
2223
posts: Post[];
2324
}>) {
2425
const [postContent, setPostContent] = useState("");
@@ -30,6 +31,7 @@ export default function Home({ data }: PageProps<{
3031

3132
// Detect mobile devices
3233
useEffect(() => {
34+
console.log("author", data.author);
3335
const checkMobile = () => {
3436
setIsMobile(window.innerWidth < 768);
3537
};
@@ -237,34 +239,36 @@ export default function Home({ data }: PageProps<{
237239
: ""
238240
}`}
239241
>
240-
{/* Delete button */}
241-
<button
242-
type="button"
243-
onClick={() => handleDeletePost(post.id)}
244-
className={`absolute top-4 right-4 p-1.5 rounded-full hover:bg-gray-700/30 transition-colors ${
245-
isDark
246-
? "text-gray-400 hover:text-gray-200"
247-
: "text-gray-500 hover:text-gray-700"
248-
}`}
249-
aria-label="Delete post"
250-
>
251-
<svg
252-
xmlns="http://www.w3.org/2000/svg"
253-
width="16"
254-
height="16"
255-
viewBox="0 0 24 24"
256-
fill="none"
257-
stroke="currentColor"
258-
strokeWidth="2"
259-
strokeLinecap="round"
260-
strokeLinejoin="round"
242+
{/* Delete button - only visible to post author */}
243+
{data.isLogin && data.author === post.author && (
244+
<button
245+
type="button"
246+
onClick={() => handleDeletePost(post.id)}
247+
className={`absolute top-4 right-4 p-1.5 rounded-full hover:bg-gray-700/30 transition-colors ${
248+
isDark
249+
? "text-gray-400 hover:text-gray-200"
250+
: "text-gray-500 hover:text-gray-700"
251+
}`}
252+
aria-label="Delete post"
261253
>
262-
<path d="M3 6h18"></path>
263-
<path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6">
264-
</path>
265-
<path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"></path>
266-
</svg>
267-
</button>
254+
<svg
255+
xmlns="http://www.w3.org/2000/svg"
256+
width="16"
257+
height="16"
258+
viewBox="0 0 24 24"
259+
fill="none"
260+
stroke="currentColor"
261+
strokeWidth="2"
262+
strokeLinecap="round"
263+
strokeLinejoin="round"
264+
>
265+
<path d="M3 6h18"></path>
266+
<path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6">
267+
</path>
268+
<path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"></path>
269+
</svg>
270+
</button>
271+
)}
268272

269273
<div className="flex items-center mb-4">
270274
<div className="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white font-bold">

modules/home/mod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export default function (s: Fastro) {
3434
s.delete("/api/post/:id", deletePostHandler);
3535

3636
// Log registered routes after registration
37-
console.log("Registered pages:", Object.keys(s.getPages()));
38-
console.log("Registered routes:", Object.keys(s.getRoutes()));
37+
// console.log("Registered pages:", Object.keys(s.getPages()));
38+
// console.log("Registered routes:", Object.keys(s.getRoutes()));
3939

4040
return s;
4141
}

modules/home/post.handler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default async function postDetailHandler(
1919
const isLogin = ses?.isLogin || false;
2020
const avatar_url = ses?.avatar_url || "";
2121
const html_url = ses?.html_url || "";
22+
const author = ses?.username || "";
2223

2324
// Get the post details
2425
const post = await getPostById(id);
@@ -43,6 +44,7 @@ export default async function postDetailHandler(
4344
isLogin,
4445
avatar_url,
4546
html_url,
47+
author,
4648
post,
4749
});
4850
}

0 commit comments

Comments
 (0)