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); window.removeEventListener("scroll", handleInfiniteScroll);
} }
function writePost() { function getTopPost() {
// 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 => {
// find the first post // find the first post
const children = blockContainer.childNodes; const children = blockContainer.childNodes;
let firstPost = null; let firstPost = null;
@ -203,20 +178,43 @@ function writePost() {
} }
} }
// generate a post to insert before the first post return firstPost;
const postData = { }
function makePostFromJson(json) {
return new Post({
id: json.id, id: json.id,
associatedUser: json.associatedUser, associatedUser: json.associatedUser,
body: json.body 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); fetch("https://api.wayfarer.games/singularity/generate-posts.php", request)
const postElem = post.getElement(); .then(response => response.json())
.then(makePostFromJson)
blockContainer.insertBefore(postElem, firstPost); .then(post => blockContainer.insertBefore(post.getElement(), getTopPost()));
console.log(post);
});
} }
function addWritePostBlock() { function addWritePostBlock() {