aboutsummaryrefslogtreecommitdiff
path: root/src/js/app.js
blob: d39d848849b0a9e9f709a781c8bc094da7f902d9 (plain)
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
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);
		}
	}
});