-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPostsPage.js
More file actions
47 lines (42 loc) · 1.18 KB
/
PostsPage.js
File metadata and controls
47 lines (42 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { createPost, deletePost, fetchPostList } from "../utils/api.js";
import PostList from "../components/PostList.js";
import LinkButton from "../components/LinkButton.js";
import { push } from "../utils/router.js";
export default function PostsPage({ $target }) {
const $page = document.createElement("div");
$page.setAttribute("class", "posts-page");
const postList = new PostList({
$target: $page,
initialState: [],
onCreate: async (parent) => {
const createdPost = await createPost(parent);
this.setState();
push(`/posts/${createdPost.id}`);
},
onDelete: async (postId) => {
await deletePost(postId);
this.setState();
history.replaceState(null, null, "/");
const $page = document.querySelector(".post-edit-page");
if ($page) {
$page.remove();
}
},
});
new LinkButton({
$target: $page,
initialState: {
text: "+ 새 페이지",
link: "/posts/new",
},
className: "page-create",
});
this.setState = async () => {
const posts = await fetchPostList();
postList.setState(posts);
this.render();
};
this.render = () => {
$target.prepend($page);
};
}