aboutsummaryrefslogtreecommitdiff
path: root/src/js/app.js
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-04-17 22:45:12 +0200
committerHampusM <hampus@hampusmat.com>2021-04-17 22:45:12 +0200
commit15992970bc9d3eec402f6d85e40236e3095f0fc0 (patch)
treedd4bc2672d468b7ec74b41d089dae3b4ea4ed9f2 /src/js/app.js
parent7ec5335b7893bd35fa2fa345e5c88ee5dbd5a656 (diff)
Finished repos api endpoint, created complete repos page & removed dist dir
Diffstat (limited to 'src/js/app.js')
-rw-r--r--src/js/app.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/js/app.js b/src/js/app.js
new file mode 100644
index 0000000..d39d848
--- /dev/null
+++ b/src/js/app.js
@@ -0,0 +1,66 @@
+function request(method, source, data = null)
+{
+ return new Promise(function (resolve, reject){
+ let xhr = new XMLHttpRequest();
+ xhr.open(method, source, true);
+ xhr.setRequestHeader("Content-Type", "application/json");
+ xhr.send(data);
+
+ xhr.onload = function()
+ {
+ if(this.status >= 200 && this.status < 300){
+ resolve(xhr.response);
+ }
+ resolve({ status: this.status, statusText: xhr.statusText });
+ };
+ xhr.onerror = () =>
+ {
+ resolve({ status: this.status, statusText: xhr.statusText });
+ }
+ });
+}
+
+
+document.addEventListener("DOMContentLoaded", async function ()
+{
+ const list = document.getElementById("repos");
+ const repos = JSON.parse(await request("GET", "http://localhost:1337/api/v1/repos"))["data"];
+
+ const params = new URLSearchParams(window.location.search);
+ const search = params.get("q");
+
+ for(const [key, value] of Object.entries(repos)) {
+ const li = document.createElement("li");
+ const repo_div = document.createElement("div");
+
+ const repo_title = document.createElement("p");
+ const link = document.createElement("a");
+ link.setAttribute("href", key);
+ link.appendChild(document.createTextNode(key));
+ repo_title.appendChild(link);
+ repo_title.classList.add("repo-title");
+
+ const repo_last_updated = document.createElement("span");
+ repo_last_updated.appendChild(document.createTextNode(`Last updated about ${value["last_updated"]} ago`));
+ repo_last_updated.classList.add("repo-last-updated");
+
+ const repo_desc = document.createElement("span");
+ repo_desc.appendChild(document.createTextNode(value["description"]));
+ repo_desc.classList.add("repo-desc");
+
+ repo_div.appendChild(repo_title)
+ repo_div.appendChild(repo_last_updated)
+ repo_div.appendChild(repo_desc)
+
+ li.appendChild(repo_div);
+
+ if(search !== null) {
+ if(key.indexOf(search) != -1) {
+ list.appendChild(li);
+ }
+ }
+ else {
+ list.appendChild(li);
+ }
+ }
+}); \ No newline at end of file