Files
chookchat/client/script.js

142 lines
3.9 KiB
JavaScript
Raw Normal View History

2026-07-20 20:11:06 +10:00
async function like(id) {
2026-07-21 12:06:46 +10:00
const formData = new FormData();
formData.append('post', id);
const result = await fetch('/like', {
method: 'POST',
body: formData
});
if (result.status === 401) {
2026-07-21 16:58:44 +10:00
window.location.href = "/login";
2026-07-21 12:06:46 +10:00
return;
}
if (result.status < 200 || result.status >= 400) {
alert("your post didnt get the like because the server thought your opinion was invalid");
return;
}
}
2026-07-21 16:05:08 +10:00
async function post() {
const postbox = document.getElementById('newpost');
const formData = new FormData();
formData.append('post', postbox.value);
const result = await fetch('/post', {
method: 'POST',
body: formData
});
if (result.status < 200 || result.status >= 400) {
alert("you have been silenced by the server and your post did not go through");
return;
}
window.location.href = "/";
}
2026-07-21 12:06:46 +10:00
async function register() {
2026-07-20 19:02:04 +10:00
const usernameBox = document.getElementById('username');
const passwordBox = document.getElementById('password');
const formData = new FormData();
formData.append('username', usernameBox.value);
formData.append('password', passwordBox.value);
2026-07-21 12:06:46 +10:00
const result = await fetch('/register', {
2026-07-20 20:11:06 +10:00
method: 'POST',
body: formData
2026-07-20 19:02:04 +10:00
});
2026-07-20 20:11:06 +10:00
if (result.status < 200 || result.status >= 400) {
2026-07-21 12:06:46 +10:00
alert("fard");
return;
2026-07-20 20:11:06 +10:00
}
2026-07-21 12:06:46 +10:00
window.location.href = "/";
}
async function login() {
const usernameBox = document.getElementById('username');
const passwordBox = document.getElementById('password');
const formData = new FormData();
formData.append('username', usernameBox.value);
formData.append('password', passwordBox.value);
const result = await fetch('/login', {
method: 'POST',
body: formData
});
if (result.status < 200 || result.status >= 400) {
alert("for some reason the server hates you and didn't let you log into your account");
return;
}
window.location.href = "/";
2026-07-20 19:02:04 +10:00
}
2026-07-21 13:09:52 +10:00
async function checkLoginStatus() {
const result = await fetch('/me');
if (result.headers.get('X-Logged-In') !== 'true') {
return "";
}
const username = result.headers.get('X-Username');
return username;
}
document.addEventListener('DOMContentLoaded', async function() {
2026-07-21 16:05:08 +10:00
if (window.fetch) {
2026-07-21 13:09:52 +10:00
// We know the fetch API exists, so hide the stuff that doesn't rely on it
const form = document.getElementById("postbox-form");
2026-07-21 16:05:08 +10:00
if (!form) {
return;
}
2026-07-21 13:09:52 +10:00
form.style.display = 'none';
const postbox = document.getElementById("postbox");
2026-07-21 16:05:08 +10:00
if (!postbox) {
return;
}
2026-07-21 13:09:52 +10:00
const userStatus = await checkLoginStatus();
// if we're logged in, show the post box,
// otherwise show a welcome message, encouraging users to sign up/log in
if (userStatus === "") {
const newElement = document.createElement("p");
newElement.textContent = "Welcome to Chookchat!\nTo get posting, click 'login' to log in or create an account.\nEnjoy your stay!";
postbox.appendChild(newElement);
} else {
const helperText = document.createElement("p");
helperText.textContent = "You're posting as " + userStatus;
postbox.appendChild(helperText);
2026-07-21 16:05:08 +10:00
2026-07-21 13:09:52 +10:00
const textArea = document.createElement("textarea");
2026-07-21 16:05:08 +10:00
textArea.id = "newpost";
2026-07-21 13:09:52 +10:00
postbox.appendChild(textArea);
2026-07-21 16:05:08 +10:00
const postButton = document.createElement("button");
postButton.textContent = "Post";
postButton.id = "postbutton"
postButton.onclick = post;
postbox.appendChild(postButton);
const loginLink = document.getElementById("loginLink");
loginLink.textContent = "hello, " + userStatus + "!";
loginLink.href = "/profile/" + userStatus;
2026-07-21 13:09:52 +10:00
}
}
});