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

88
main.js
View File

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