summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-04-06 16:14:31 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-04-06 16:14:31 +0800
commit23e689f43b56bc5e93d10bc395a28fbeca1a3c75 (patch)
tree6a72af2d23e10a3c0bfeb3d6fa718596c7a7fb31
parent4de22e12be00c0ed45831f73ab861a95573c0592 (diff)
User can select courses to show on widget
-rw-r--r--contents/config/main.xml3
-rw-r--r--contents/ui/configGeneral.qml56
-rw-r--r--contents/ui/main.qml26
3 files changed, 71 insertions, 14 deletions
diff --git a/contents/config/main.xml b/contents/config/main.xml
index 7b86809..1f846eb 100644
--- a/contents/config/main.xml
+++ b/contents/config/main.xml
@@ -8,5 +8,8 @@
<entry name="oauth2Token" type="String">
<default></default>
</entry>
+ <entry name="courses" type="String">
+ <default></default>
+ </entry>
</group>
</kcfg>
diff --git a/contents/ui/configGeneral.qml b/contents/ui/configGeneral.qml
index c13b5ea..fc64389 100644
--- a/contents/ui/configGeneral.qml
+++ b/contents/ui/configGeneral.qml
@@ -4,20 +4,70 @@ import QtQuick.Layouts 1.15
import org.kde.kirigami 2.5 as Kirigami
Kirigami.FormLayout {
- id: page
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/")
+ placeholderText: i18n("https://your.canvas.url")
}
TextField {
id: oauth2Token
Kirigami.FormData.label: i18n("OAuth2 Token:")
- placeholderText: i18n("")
+ placeholderText: i18n("Generate in Canvas web interface")
+ }
+
+ function fetchCourses() {
+ courses.clear()
+ 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 {
+ let json = JSON.parse(xhr.responseText)
+ for (let c of json) {
+ courses.append(`${c.id} ${c.course_code}`)
+ }
+ fetchCoursesStatus.text = i18n(
+ "Done! You can remove courses you don't wish to see on your desktop from the list."
+ )
+ } catch (e) {
+ if (e instanceof SyntaxError) {
+ console.error(`Cannot parse response for ${path} as JSON:\n${xhr.responseText}`)
+ fetchgCoursesStatus.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 {
+ text: "Fetch courses"
+ onClicked: fetchCourses()
+ }
+
+ Label {
+ id: fetchCoursesStatus
+ text: ""
}
}
diff --git a/contents/ui/main.qml b/contents/ui/main.qml
index 09829a3..8d4beb2 100644
--- a/contents/ui/main.qml
+++ b/contents/ui/main.qml
@@ -39,18 +39,22 @@ Item {
}
function syncCanvas() {
- // Get all courses
- callApi("/courses", 20, courses => {
- for (let course of courses) {
- callApi(`/courses/${course.id}/activity_stream`, 10, activityStream => {
- for (let activity of activityStream) {
- if (activity.type == "Announcement") {
- announcementsModel.append({course: course.course_code, title: activity.title})
- }
+ let courses = plasmoid.configuration.courses.split("\n").map(
+ // each line in the "courses" config consists of
+ // a numeric course id, a space, and a course code
+ line => { return line.split(" ", 2) }
+ )
+ announcementsModel.clear()
+ for (let course of courses) {
+ callApi(`/courses/${course[0]}/activity_stream`, 10, activityStream => {
+ // Get activity stream for each course
+ for (let activity of activityStream) {
+ if (activity.type == "Announcement") {
+ announcementsModel.append({course: course[1], title: activity.title})
}
- })
- }
- })
+ }
+ })
+ }
}
Timer {