feat: use reply endpoint

This commit is contained in:
ktyl 2024-08-04 19:33:00 +01:00
parent 56c315c301
commit c6bd7d80d6
1 changed files with 33 additions and 18 deletions

51
main.js
View File

@ -258,49 +258,64 @@ function getCurrentUser() {
}; };
} }
function writePost(postCallback) { function getPostRequest(body) {
const request = { return {
method: "POST", method: "POST",
mode: "cors", mode: "cors",
body: JSON.stringify(getCurrentUser()), body: JSON.stringify(body),
headers: { headers: {
"Content-type": "application/json; charset=UTF-8" "Content-type": "application/json; charset=UTF-8"
} }
}; };
}
function writePost() {
if (localMode) { if (localMode) {
const post = new Post({ const post = new Post({
id: "1234", id: "1234",
associatedUser: getCurrentUser().user, associatedUser: getCurrentUser().user,
body: "local mode post (local mode post)" body: "local mode post (local mode post)"
}); });
postCallback(post); blockContainer.insertBefore(post.getElement(), getTopPost());
} else { return;
fetch("https://api.wayfarer.games/singularity/generate-posts.php", request) }
.then(response => response.json())
.then(makePostFromJson)
.then(postCallback);
}
}
function writeNewPost() { fetch("https://api.wayfarer.games/singularity/generate-posts.php", getPostRequest(getCurrentUser()))
writePost(post => blockContainer.insertBefore(post.getElement(), getTopPost())); .then(response => response.json())
.then(makePostFromJson)
.then(post => blockContainer.insertBefore(post.getElement(), getTopPost()));
} }
function writeReply(post) { function writeReply(post) {
// find the correct element // find the correct element
const elem = document.getElementById(post.id); const elem = document.getElementById(post.id);
const user = getCurrentUser();
writePost(reply => { if (localMode) {
post.addReply(reply); console.error("TODO: implement local replies");
elem.append(reply.getElement()); return;
}); }
const replyBody = {
postId: post.id,
interests: user.interests,
user: user.user,
posting_style: user.posting_style
};
const request = getPostRequest(replyBody);
fetch("https://api.wayfarer.games/singularity/generate-reply.php", getPostRequest(replyBody))
.then(response => response.json())
.then(makePostFromJson)
.then(reply => {
post.addReply(reply);
elem.append(reply.getElement());
});
} }
function addWritePostBlock() { function addWritePostBlock() {
const blockElem = document.createElement("div"); const blockElem = document.createElement("div");
blockElem.addEventListener("click", writeNewPost); blockElem.addEventListener("click", writePost);
blockElem.className = "block write-post"; blockElem.className = "block write-post";
const spanElem = document.createElement("h2"); const spanElem = document.createElement("h2");