summaryrefslogtreecommitdiff
path: root/static/main.js
blob: 73a4c600197da39e7a01bb525eb4fdbe834995b0 (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
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
async function me() {
    try {
        const response = await fetch("/me");
        if (response.ok) {
            const result = await response.json();
            // this is technically a XSS risk (TODO: deal with it)
            // in principle it only affects the person who chose their username, but...
            document.getElementById("logged-in").innerHTML = `Logged in as ${result.username}`;
            document.getElementById("logout-links").hidden = false;
        } else {
            document.getElementById("login-links").hidden = false;
        }
    } catch (error) {
        console.log("hi");
    }
}

async function logout() {
    const response = await fetch("/logout", {method: "post"});
    window.location.reload();
}

function create_problem_element(problem, submissions) {
    const problem_div = document.createElement("div");
    problem_div.className = "problem";
                 
    const title = document.createElement("h2");
    title.innerHTML = problem.title;
    problem_div.appendChild(title);
    
    const description = document.createElement("p");
    description.innerHTML = problem.description;
    problem_div.appendChild(description);
    
    const sub_title = document.createElement("h3");
    sub_title.innerHTML = "Submissions";
    problem_div.appendChild(sub_title);

    const submission_link = document.createElement("a");
    submission_link.href = `submit.html?problem_id=${problem.id}`;
    submission_link.innerHTML = "submit solution"
    problem_div.appendChild(submission_link);
    
    const sub_div = document.createElement("div");
    sub_div.className = "submission-div";
    problem_div.appendChild(sub_div);
    
    const sub_table = document.createElement("table");
    sub_table.className = "submission-table";
    sub_div.appendChild(sub_table);
    
    const header_row = document.createElement("tr");
    sub_table.appendChild(header_row);
    
    const header_user = document.createElement("th");
    header_user.innerHTML = "User";
    header_row.appendChild(header_user);
    const header_lang = document.createElement("th");
    header_lang.innerHTML = "Language";
    header_row.appendChild(header_lang);
    const header_size = document.createElement("th");
    header_size.innerHTML = "Size";
    header_row.appendChild(header_size);
    const header_subm = document.createElement("th");
    header_subm.innerHTML = "Submission";
    header_row.appendChild(header_subm);
    
    for (const submission of submissions) {
        const sub_row = document.createElement("tr");
        sub_table.appendChild(sub_row);
    
        const sub_user = document.createElement("td");
        sub_user.innerHTML = "Username";
        sub_row.appendChild(sub_user);
        
        const sub_lang = document.createElement("td");
        sub_lang.innerHTML = "Language";
        sub_row.appendChild(sub_lang);
        
        const sub_size = document.createElement("td");
        sub_size.innerHTML = "Size";
        sub_row.appendChild(sub_size);

        const sub_subm = document.createElement("td");
        const sub_anch = document.createElement("a");
        sub_anch.href = "submission.html?id=5";
        sub_anch.innerText = "view submission";
        sub_subm.appendChild(sub_anch);
        sub_row.appendChild(sub_subm);
    }
    
    return problem_div
}

async function fetch_problems() {
    const response = await fetch("/problem");
    if (!response.ok) {
        console.log("ummm");
    }

    const result = await response.json();

    const problems_div = document.getElementById("problems");
    while (problems_div.firstChild) {
        problems_div.removeChild(problems_div.lastChild);
    }

    for (const problem of result) {
        const problem_div = create_problem_element(problem, [1, 2, 3])
        problems_div.appendChild(problem_div);
    }
}

async function on_load() {
    await me();
    await fetch_problems();
}