-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (62 loc) · 2.4 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
import { problems } from "./db/problems.js";
import { UserDb } from "./db/userDb.js";
import { router, routingRules } from "./routers.js";
window.addEventListener("hashchange", router);
window.addEventListener("load", router);
window.addEventListener("hashchange", routingRules);
window.addEventListener("load", routingRules);
window.addEventListener("hashchange", activeNavLink);
window.addEventListener("load", activeNavLink);
document.querySelector("#export").addEventListener("click", () => {
const data = UserDb.get().problems;
const csvContent =
"Title,Difficulty,Patterns,URL,Companies,Tags,Hints,Bugs,Time,Solution Type,Score,My Solution URL,Note\n" +
Object.entries(data)
.map(([pId, pData]) =>
[
...Object.entries(problems.find((p) => p.id === pId))
.filter(([ki, _]) => !["solutions", "id"].includes(ki))
.map(([k, v]) => {
if (["title", "url", "difficulty"].includes(k)) return `"${v}"`;
else if (["companies", "tags"].includes(k))
return `"${v.map((it) => it).join(",")}"`;
else if (k === "patterns")
return `"${v.map((it) => it.name).join(",")}"`;
}),
...Object.entries(pData).map(([_, value]) => `"${value}"`),
].join(",")
)
.join("\n");
const blob = new Blob([csvContent], { type: "text/csv" });
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `problems_${new Date().toISOString().replaceAll(":", "-")}.csv`;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
});
function activeNavLink() {
document.querySelectorAll("nav li a").forEach((link) => {
link.classList.remove("active");
});
const activeLink = document.querySelector(
`nav li a[href="${location.hash || "/"}"]`
);
if (activeLink) activeLink.classList.add("active");
if (UserDb.get().loggedIn) {
document.querySelector("#export").style.display = "inline";
document.querySelector("#logout").style.display = "inline";
document.querySelector("#problem-of-today").style.display = "inline";
}
}
document.querySelector("#logout").addEventListener("click", () => {
if (
!confirm(
`YOUR PROGRESS WILL BE LOST. Are you sure you want to log out from ${UserDb.get().stream.title}?`
)
)
return;
UserDb.clear();
location.hash = "newStream";
});