-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsidebarBody.js
More file actions
87 lines (76 loc) · 2.35 KB
/
sidebarBody.js
File metadata and controls
87 lines (76 loc) · 2.35 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { request } from "../../utils/api.js";
import { push } from "../../utils/router.js";
export default function SidebarBody({ $target }) {
const $sidebarBody = document.createElement("div");
const $renderList = document.createElement("div");
$sidebarBody.className = "sidebar-body";
$target.appendChild($sidebarBody);
this.setState = async () => {
this.state = await request("/documents", {
method: "GET",
});
$renderList.innerHTML = "";
this.render();
};
const addDocumnet = async (dataId) => {
const newDocument = await request("/documents", {
method: "POST",
body: JSON.stringify({
title: "제목",
parent: dataId,
}),
});
$renderList.innerHTML = "";
this.setState();
push(`/documents/${newDocument.id}`);
};
const deleteDocument = async (dataId) => {
await request(`/documents/${dataId}`, {
method: "DELETE",
});
$renderList.innerHTML = "";
push("/");
};
const renderDocuments = (documents, $renderList) => {
documents.map((e) => {
const $ul = document.createElement("ul");
const $li = document.createElement("li");
$li.className = "document-li";
$li.setAttribute("data-id", e.id);
$li.textContent = e.title;
const $addBtn = document.createElement("button");
$addBtn.className = "add-btn";
$addBtn.innerHTML = "+";
const $deleteBtn = document.createElement("button");
$deleteBtn.className = "delete-btn";
$deleteBtn.innerHTML = "x";
$renderList.appendChild($ul);
$ul.appendChild($li);
$li.appendChild($addBtn);
$li.appendChild($deleteBtn);
if (e.documents) {
renderDocuments(e.documents, $ul);
}
});
return $renderList.innerHTML;
};
this.render = () => {
if (this.state) {
$sidebarBody.innerHTML = renderDocuments(this.state, $renderList);
} else {
$sidebarBody.innerHTML = "새 페이지를 눌러 문서를 작성해 주세요!";
}
};
this.render();
$sidebarBody.addEventListener("click", (e) => {
const target = e.target;
const dataId = target.closest("li").dataset.id;
if (target.className === "add-btn") {
addDocumnet(dataId);
} else if (target.className === "delete-btn") {
deleteDocument(dataId);
} else if (dataId) {
push(`/documents/${dataId}`);
}
});
}