summaryrefslogtreecommitdiff
path: root/static/submit.js
blob: 936518b3cd3eb31bcbda2eaaeeb391404bb4effd (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

function problem_id() {
    const url_params = new URLSearchParams(window.location.search);
    return parseInt(url_params.get("problem_id"));
}

async function submit() {
    const language = document.getElementById("submission-language").value;
    const details = document.getElementById("submission-details").value;
    const code = document.getElementById("submission-code").value;
    
    const submission = {
        problem_id: problem_id(), // TODO validate language and problem id in server
        language: language,
        details: details,
        code: code,
    };

    const response = await fetch("/submission", {
        method: "post",
        headers: { "Content-Type": "application/json" },
        credentials: "include",
        body: JSON.stringify(submission)
    });

    if (response.ok) {
        const result = await response.json();
        window.location.href = "index.html";
    } else {
        document.getElementById("error-message").innerHTML = "error submitting solution, try again";
        document.getElementById("error").hidden = false;
    }
}

async function init() {
    const response = await fetch(`/problem/${problem_id()}`);
    const problem = await response.json();

    const problem_div = document.getElementById("submission-problem");
    
    const title = document.createElement("h2");
    title.innerHTML = `Problem: ${problem.title}`;
    problem_div.appendChild(title);
    
    const description = document.createElement("p");
    description.innerHTML = problem.description;
    problem_div.appendChild(description);
}