-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagesAPICalls.ts
87 lines (72 loc) · 2.33 KB
/
pagesAPICalls.ts
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 { sendAPICall } from "../APICalls";
const endpointBase = "pages";
export async function findPage(pageId: string | undefined) {
const body = null;
const endpoint = `${endpointBase}/${pageId}`;
return await sendAPICall("GET", endpoint, body);
}
export async function updateRights(pageId: string | null, readRights: boolean, updateRights: boolean) {
const body = {
pageId,
readRights,
updateRights,
};
const endpoint = `${endpointBase}/updateRights`;
return await sendAPICall("PATCH", endpoint, body);
}
export async function findOrCreate(pageId: string | undefined) {
const body = {
pageId,
userId: localStorage.getItem("user_id"),
};
const endpoint = `${endpointBase}/findOrCreate`;
return await sendAPICall("POST", endpoint, body);
}
export async function getPages(){
const body = null;
const endpoint = `${endpointBase}/findAll/${localStorage.getItem("user_id")}`;
return await sendAPICall("GET", endpoint, body);
}
export async function getPageTitle(pageId: string){
const body = null;
const endpoint = `${endpointBase}/getTitle/${pageId}`;
return await sendAPICall("GET", endpoint, body);
}
export async function createPage(title: string, parentId: string) {
const body = {
title,
parentId,
userId: localStorage.getItem("user_id"),
};
const endpoint = `${endpointBase}/`;
return await sendAPICall("POST", endpoint, body);
}
export async function updatePage(blocks: [], pageId: string | null, slashMenuBlockId: string | null) {
const body = {
pageId,
update: {
title: findElementInBlockList(blocks, ["h1"]),
thumbnailSrc: findElementInBlockList(blocks, ["img", "gif"]),
blocks,
slashMenuBlockId,
},
};
const endpoint = `${endpointBase}/`;
return await sendAPICall("PATCH", endpoint, body);
}
export async function getRecentPages() {
const body = null;
const userId = localStorage.getItem("user_id");
const endpoint = `${endpointBase}/recentDocuments/${userId}`;
return await sendAPICall("GET", endpoint, body);
}
function findElementInBlockList(blockList: any[], type: string[]) {
for (const block of blockList) {
if (type.includes(block.type)) {
let tempElement = document.createElement("div");
tempElement.innerHTML = block.html as string;
return tempElement.firstChild?.textContent;
}
}
return null;
}