chore: clean up post writing

This commit is contained in:
ktyl 2024-07-20 13:40:54 +01:00
parent 9d2597f7a4
commit 2755f7ebd3
1 changed files with 43 additions and 45 deletions

68
main.js
View File

@ -165,32 +165,7 @@ function removeInfiniteScroll() {
window.removeEventListener("scroll", handleInfiniteScroll);
}
function writePost() {
// TODO: inject a new post element at the top of the feed
console.log("write something interesting");
setTimeout(() => {
}, 500);
const userName = "theChief";
fetch("https://api.wayfarer.games/singularity/generate-posts.php", {
method: "POST",
mode: "cors",
body: JSON.stringify({
"user": userName,
"interests": [
"gen-ai",
"blockchain",
"nfts"
],
"posting_style": "just the most truly inane takes"
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => {
function getTopPost() {
// find the first post
const children = blockContainer.childNodes;
let firstPost = null;
@ -203,20 +178,43 @@ function writePost() {
}
}
// generate a post to insert before the first post
const postData = {
return firstPost;
}
function makePostFromJson(json) {
return new Post({
id: json.id,
associatedUser: json.associatedUser,
body: json.body
});
}
function getCurrentUser() {
return {
"user": "theChief",
"interests": [
"gen-ai",
"blockchain",
"nfts"
],
"posting_style": "just the most truly inane takes"
};
}
function writePost() {
const request = {
method: "POST",
mode: "cors",
body: JSON.stringify(getCurrentUser()),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
};
const post = new Post(postData);
const postElem = post.getElement();
blockContainer.insertBefore(postElem, firstPost);
console.log(post);
});
fetch("https://api.wayfarer.games/singularity/generate-posts.php", request)
.then(response => response.json())
.then(makePostFromJson)
.then(post => blockContainer.insertBefore(post.getElement(), getTopPost()));
}
function addWritePostBlock() {