hippofish/packages/backend/src/server/web/cli.js

73 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-02-18 20:33:25 +01:00
"use strict";
2021-03-02 17:03:29 +01:00
window.onload = async () => {
2023-02-18 20:33:25 +01:00
const account = JSON.parse(localStorage.getItem("account"));
2021-03-02 17:03:29 +01:00
const i = account.token;
const api = (endpoint, data = {}) => {
const promise = new Promise((resolve, reject) => {
// Append a credential
if (i) data.i = i;
2023-02-18 20:33:25 +01:00
2021-03-02 17:03:29 +01:00
// Send request
2023-02-18 20:33:25 +01:00
fetch(endpoint.indexOf("://") > -1 ? endpoint : `/api/${endpoint}`, {
method: "POST",
2021-03-02 17:03:29 +01:00
body: JSON.stringify(data),
2023-02-18 20:33:25 +01:00
credentials: "omit",
cache: "no-cache",
})
.then(async (res) => {
const body = res.status === 204 ? null : await res.json();
if (res.status === 200) {
resolve(body);
} else if (res.status === 204) {
resolve();
} else {
reject(body.error);
}
})
.catch(reject);
2021-03-02 17:03:29 +01:00
});
2023-02-18 20:33:25 +01:00
2021-03-02 17:03:29 +01:00
return promise;
};
2023-02-18 20:33:25 +01:00
document.getElementById("submit").addEventListener("click", () => {
api("notes/create", {
text: document.getElementById("text").value,
2021-03-02 17:03:29 +01:00
}).then(() => {
location.reload();
});
});
2023-02-18 20:33:25 +01:00
api("notes/timeline").then((notes) => {
const tl = document.getElementById("tl");
2021-03-02 17:03:29 +01:00
for (const note of notes) {
2023-02-18 20:33:25 +01:00
const el = document.createElement("div");
2023-03-19 09:59:33 +01:00
const header = document.createElement("header");
const name = document.createElement("p");
2023-04-02 06:10:31 +02:00
const avatar = document.createElement("img");
2021-03-02 17:03:29 +01:00
name.textContent = `${note.user.name} @${note.user.username}`;
2023-03-19 10:08:08 +01:00
avatar.src = note.user.avatarUrl;
2023-04-02 06:10:31 +02:00
avatar.style = "height: 40px";
2023-02-18 20:33:25 +01:00
const text = document.createElement("div");
2021-03-02 17:03:29 +01:00
text.textContent = `${note.text}`;
2023-03-19 09:59:33 +01:00
el.appendChild(header);
header.appendChild(avatar);
header.appendChild(name);
2023-03-19 10:33:44 +01:00
if (note.text) {
el.appendChild(text);
}
if (note.files) {
for (const file of note.files) {
const img = document.createElement("img");
img.src = file.properties.thumbnailUrl;
2023-04-02 06:10:31 +02:00
el.appendChild(img);
2023-03-19 10:33:44 +01:00
}
}
2021-03-02 17:03:29 +01:00
tl.appendChild(el);
}
});
};