-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
123 lines (112 loc) · 3.6 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Initializes the application by including HTML files
* and highlighting the current page.
*/
async function init() {
await includeHTML();
getUserAccount();
}
const USER = [];
let currentUser;
/**
* Retrieves the current user based on the email provided in the URL parameters.
*/
async function getCurrentUser() {
const urlParams = new URLSearchParams(window.location.search);
const userMail = urlParams.get("mail");
const response = await getItem("users");
const responseAsJson = JSON.parse(response);
const users = responseAsJson.users;
const usersFiltered = users.filter((user) => user.userMail == userMail);
currentUser = usersFiltered;
}
const STORAGE_TOKEN = "V3GBL599LI0VXDK4HJXHD632WIB3WOBMWS6ENT06";
const STORAGE_URL = "https://remote-storage.developerakademie.org/item";
/**
* Stores the given key-value pair in the remote storage.
*
* @param {string} key - The key under which to store the value.
* @param {any} value - The value to store.
* @returns {Promise<any>} A Promise that resolves to the response from the server.
*/
async function setItem(key, value) {
const payload = { key, value, token: STORAGE_TOKEN };
return fetch(STORAGE_URL, {
method: "POST",
body: JSON.stringify(payload),
}).then((res) => res.json());
}
/**
* Retrieves the value associated with the given key from the remote storage.
*
* @param {string} key - The key of the value to retrieve.
* @returns {Promise<any>} A Promise that resolves to the retrieved value.
* @throws {string} If the requested key is not found in the storage.
*/
async function getItem(key) {
const url = `${STORAGE_URL}?key=${key}&token=${STORAGE_TOKEN}`;
return fetch(url)
.then((res) => res.json())
.then((res) => {
if (res.data) {
return res.data.value;
}
throw `Could not find data with key "${key}".`;
});
}
/**
* Function to include HTML Templates
*/
async function includeHTML() {
let includeElements = document.querySelectorAll("[w3-include-html]");
for (let i = 0; i < includeElements.length; i++) {
const element = includeElements[i];
file = element.getAttribute("w3-include-html");
let resp = await fetch(file);
if (resp.ok) {
element.innerHTML = await resp.text();
} else {
element.innerHTML = "Page not found";
}
}
highlightCurrentPage();
}
/**
* Highlight the current page in the sidebar by changing its background color and border radius.
*/
function highlightCurrentPage() {
const currentPage = window.location.pathname.split("/").pop().toLowerCase();
const pages = [
{ name: "summary.html", index: 0 },
{ name: "add_task.html", index: 1 },
{ name: "board.html", index: 2 },
{ name: "contacts.html", index: 3 },
{ name: "privacy_policy.html", index: 4 },
{ name: "legal_notice.html", index: 5 },
];
pages.forEach((page) => {
if (currentPage === page.name) {
setActiveStyle(page.index);
}
});
}
/**
* Set the active style for a sidebar element.
* @param {number} index - The index of the sidebar element in the array.
*/
const setActiveStyle = (index) => {
const sidebarElement = document.getElementById(`sidebar_${index}`);
if (sidebarElement) {
sidebarElement.style.backgroundColor = "#091931";
sidebarElement.style.borderRadius = "10px";
}
};
/**
* Change the current page to the specified URL.
* @param {string} url - The URL of the page to navigate to, excluding the file extension.
*/
function changeCurrentPage(url) {
const urlParams = new URLSearchParams(window.location.search);
let userMail = urlParams.get("mail");
window.location.href = `./${url}.html?mail=${userMail}`;
}