summaryrefslogtreecommitdiff
path: root/contents/ui/configCanvas.qml
blob: aed1a3eaf8348c0d6b224445e885974888db9aa0 (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
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import org.kde.kirigami 2.5 as Kirigami

Kirigami.FormLayout {
    property alias cfg_canvasUrl: canvasUrl.text
    property alias cfg_oauth2Token: oauth2Token.text
    property alias cfg_courses: courses.text

    TextField {
        id: canvasUrl
        Kirigami.FormData.label: i18n("Canvas URL:")
        placeholderText: i18n("https://your.canvas.url")
    }

    TextField {
        id: oauth2Token
        Kirigami.FormData.label: i18n("OAuth2 Token:")
        placeholderText: i18n("Generate in Canvas web interface")
    }

    function fetchCourses() {
        fetchCoursesStatus.text = i18n("Fetching…")
        let xhr = new XMLHttpRequest()
        xhr.open("GET", `${canvasUrl.text.replace(/\/$/, "")}/api/v1/courses?per_page=100`)
        xhr.setRequestHeader("Authorization", `Bearer ${oauth2Token.text}`)
        xhr.onload = () => {
            if (xhr.status == 200) {
                try {
                    courses.clear()
                    let json = JSON.parse(xhr.responseText)
                    for (let c of json) {
                        courses.append(`${c.id} ${c.course_code}`)
                    }
                    fetchCoursesStatus.text = i18n(
                        "Done! To remove a course, just delete the line.\n" +
                        "You can also rename a course, but do not change the numeric ID."
                    )
                } catch (e) {
                    if (e instanceof SyntaxError) {
                        console.error(`Cannot parse response for ${path} as JSON:\n${xhr.responseText}`)
                        fetchCoursesStatus.text = i18n("Cannot parse API response")
                    } else { throw e }
                }
            } else {
                console.error(`XHR failed when retrieving /courses (status ${xhr.status}):\n${xhr.responseText}`)
                fetchCoursesStatus.text = i18n("API call failed (HTTP status %1)", xhr.status)
            }
        }
        xhr.send()
    }

    ScrollView {
        Layout.fillWidth: true
        Kirigami.FormData.label: i18n("Courses:")
        TextArea {
            Layout.fillWidth: true
            id: courses
            placeholderText: i18n("Click Fetch courses for a full list")
        }
    }

    Button {
        icon.name: "download"
        text: "Fetch courses"
        onClicked: fetchCourses()
    }

    Label {
        id: fetchCoursesStatus
        text: "Fetching courses from Canvas will overwrite your current config."
    }
}