feat: add local mode switch

This commit is contained in:
ktyl 2024-08-03 12:28:23 +01:00
parent 458ad097d8
commit 60873afe12
1 changed files with 18 additions and 6 deletions

24
main.js
View File

@ -1,6 +1,7 @@
var users = []; var users = [];
var posts = {}; var posts = {};
const localMode = true;
const blockContainer = document.getElementById("block-container"); const blockContainer = document.getElementById("block-container");
const postCountElem = document.getElementById("post-count"); const postCountElem = document.getElementById("post-count");
const postTotalElem = document.getElementById("post-total"); const postTotalElem = document.getElementById("post-total");
@ -217,10 +218,19 @@ function writePost() {
} }
}; };
fetch("https://api.wayfarer.games/singularity/generate-posts.php", request) if (localMode) {
.then(response => response.json()) const post = new Post({
.then(makePostFromJson) id: "1234",
.then(post => blockContainer.insertBefore(post.getElement(), getTopPost())); associatedUser: getCurrentUser().user,
body: "local mode post (local mode post)"
});
blockContainer.insertBefore(post.getElement(), getTopPost());
} else {
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() { function addWritePostBlock() {
@ -267,8 +277,10 @@ function loadDataFromEndpoint(endpoint, callback) {
}); });
} }
loadDataFromEndpoint("https://api.wayfarer.games/singularity/users.json", json => { users = json.users; }); const usersUrl = localMode ? "users.json" : "https://api.wayfarer.games/singularity/users.json";
loadDataFromEndpoint("https://api.wayfarer.games/singularity/posts.json", json => { const postsUrl = localMode ? "posts.json" : "https://api.wayfarer.games/singularity/posts.json";
loadDataFromEndpoint(usersUrl, json => { users = json.users; });
loadDataFromEndpoint(postsUrl, json => {
// first pass to instantiate all the posts // first pass to instantiate all the posts
for (let i = 0; i < json.content.length; i++) { for (let i = 0; i < json.content.length; i++) {
const post = new Post(json.content[i]); const post = new Post(json.content[i]);