Skip to content

Commit 75fcb78

Browse files
committed
Refresh posts
1 parent 0c76375 commit 75fcb78

3 files changed

Lines changed: 11 additions & 5 deletions

File tree

app/(app)/feed/page.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
"use client";
2+
3+
import { useState } from "react";
14
import { PostList } from "@/components/feed/PostList";
25
import { CreatePostDialog } from "@/components/feed/CreatePostDialog";
36

47
export default function FeedPage() {
8+
const [refreshKey, setRefreshKey] = useState(0);
9+
510
return (
611
<div className="space-y-6">
712
<div className="flex items-center justify-between">
813
<h1 className="text-2xl font-semibold">Posts</h1>
9-
<CreatePostDialog />
14+
<CreatePostDialog onSuccess={() => setRefreshKey((k) => k + 1)} />
1015
</div>
11-
<PostList />
16+
<PostList refreshKey={refreshKey} />
1217
</div>
1318
);
1419
}

components/feed/CreatePostDialog.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const schema = z.object({
2525

2626
type FormData = z.infer<typeof schema>;
2727

28-
export function CreatePostDialog() {
28+
export function CreatePostDialog({ onSuccess }: { onSuccess?: () => void }) {
2929
const [open, setOpen] = useState(false);
3030
const [loading, setLoading] = useState(false);
3131

@@ -48,6 +48,7 @@ export function CreatePostDialog() {
4848
toast.success("Post created!");
4949
reset();
5050
setOpen(false);
51+
onSuccess?.();
5152
} catch {
5253
toast.error("Failed to create post.");
5354
} finally {

components/feed/PostList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { useEffect, useState } from "react";
44
import { Post } from "@/types";
55
import { PostCard } from "./PostCard";
66

7-
export function PostList() {
7+
export function PostList({ refreshKey }: { refreshKey?: number }) {
88
const [posts, setPosts] = useState<Post[]>([]);
99

1010
useEffect(() => {
1111
fetch("/api/posts")
1212
.then((r) => (r.ok ? r.json() : []))
1313
.then(setPosts)
1414
.catch(console.error);
15-
}, []);
15+
}, [refreshKey]);
1616

1717
if (posts.length === 0) {
1818
return <p className="text-center text-muted-foreground py-12">No posts yet. Be the first!</p>;

0 commit comments

Comments
 (0)