Start of works on frontend
This commit is contained in:
9
static/js/loginpage.js
Normal file
9
static/js/loginpage.js
Normal file
@ -0,0 +1,9 @@
|
||||
async function handleLogin(event) {
|
||||
event.preventDefault();
|
||||
const username = document.getElementById("username").value;
|
||||
const password = document.getElementById("password").value;
|
||||
const result = await login(username, password);
|
||||
if (result) {
|
||||
window.location.href = "/";
|
||||
}
|
||||
}
|
30
static/js/mainpage.js
Normal file
30
static/js/mainpage.js
Normal file
@ -0,0 +1,30 @@
|
||||
window.addEventListener("load", async () => {
|
||||
console.log("loading...");
|
||||
const rootFolder = await getFolder();
|
||||
const bm = document.getElementById("bookmarks");
|
||||
if (!rootFolder.success) {
|
||||
bm.textContent = "Нет закладок";
|
||||
return;
|
||||
}
|
||||
const template = document.querySelector("#bookmark");
|
||||
const tagtempl = document.querySelector("#booktag");
|
||||
if (!template) {
|
||||
return;
|
||||
}
|
||||
for (let b of rootFolder.folder.ChildBookmarks) {
|
||||
const clone = template.content.cloneNode(true);
|
||||
clone.querySelector("#bookmark_name").textContent = b.Name;
|
||||
const link = clone.querySelector("#bookmark_link");
|
||||
link.setAttribute("href", b.Url);
|
||||
link.textContent = b.Url;
|
||||
for (let tag of b.Tags) {
|
||||
const t = tagtempl.content.cloneNode(true);
|
||||
const el = t.querySelector("span");
|
||||
el.addEventListener("onclick", () => alert(tag));
|
||||
el.textContent = "#" + tag;
|
||||
clone.querySelector("#bookmark_tags").appendChild(t);
|
||||
}
|
||||
|
||||
bm.appendChild(clone);
|
||||
}
|
||||
});
|
57
static/js/nashboard.js
Normal file
57
static/js/nashboard.js
Normal file
@ -0,0 +1,57 @@
|
||||
const baseUrl = "/api/";
|
||||
|
||||
function getSession() {
|
||||
return localStorage.getItem("SessionID");
|
||||
}
|
||||
|
||||
function request(url, method = "GET", data) {
|
||||
let headers = [{ "Content-Type": "application/json" }];
|
||||
const token = getSession();
|
||||
if (token) headers.push({ SessionID: token });
|
||||
let body = null;
|
||||
if (method != "GET") {
|
||||
body = JSON.stringify(data);
|
||||
}
|
||||
const r = new Request(baseUrl + url, {
|
||||
method,
|
||||
body,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
SessionID: token,
|
||||
},
|
||||
});
|
||||
return r;
|
||||
}
|
||||
|
||||
function get(request) {}
|
||||
|
||||
async function login(username, password) {
|
||||
const r = request("auth/login", "POST", { username, password });
|
||||
try {
|
||||
const result = await fetch(r);
|
||||
if (result.status == 200) {
|
||||
const token = await result.json();
|
||||
localStorage.setItem("SessionID", token);
|
||||
return true;
|
||||
}
|
||||
console.error({ status: result.status, body: await result.json() });
|
||||
return false;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function getFolder(id) {
|
||||
let url = "f";
|
||||
if (id) url = `f/{id}`;
|
||||
const r = request(url);
|
||||
try {
|
||||
const result = await fetch(r);
|
||||
const folder = await result.json();
|
||||
return { success: true, folder };
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return { success: false, err };
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user