summaryrefslogtreecommitdiff
path: root/static/main.js
blob: b070ccbad55a02c49d108a741a1388d4edd9199b (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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);

    if (submissions.length === 0) {
        const no_sub_message = document.createElement("span");
        no_sub_message.innerHTML = "no submissions for this problem yet";
        sub_div.appendChild(no_sub_message);
    } else {
        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 = submission.username;
            sub_row.appendChild(sub_user);
        
            const sub_lang = document.createElement("td");
            sub_lang.innerHTML = submission.language;
            sub_row.appendChild(sub_lang);
        
            const sub_size = document.createElement("td");
            sub_size.innerHTML = submission.code.length;
            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_submissions(problem_id) {
    const response = await fetch(`/submission/${problem_id}`)
    const result = await response.json();
    return result;
}

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 submissions = await fetch_submissions(problem.id);
        
        const problem_div = create_problem_element(problem, submissions)
        problems_div.appendChild(problem_div);
    }
}

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