diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-04-05 17:18:14 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-04-05 17:18:14 +0800 |
commit | 670655326ec305797b2c460b42e7ee53b057082e (patch) | |
tree | 2795d5312f3e95ba48af93f2cb210a7bf0e8e0f1 /contents/ui/main.qml | |
parent | dc920a81f58d745be895817d136b144c1665481b (diff) |
Add basic canvas API request and related config
Diffstat (limited to 'contents/ui/main.qml')
-rw-r--r-- | contents/ui/main.qml | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/contents/ui/main.qml b/contents/ui/main.qml index c78ba5e..33c12b6 100644 --- a/contents/ui/main.qml +++ b/contents/ui/main.qml @@ -2,6 +2,7 @@ import QtQuick 2.15 import QtQuick.Layouts 1.15 import QtQuick.Controls 2.15 +import org.kde.plasma.plasmoid 2.0 import org.kde.plasma.core 2.0 as PlasmaCore import org.kde.plasma.components 2.0 as PlasmaComponents import org.kde.plasma.components 3.0 as PlasmaComponents3 @@ -11,11 +12,31 @@ Item { width: PlasmaCore.Units.gridUnit * 20 height: PlasmaCore.Units.gridUnit * 40 + readonly property string canvasUrl: plasmoid.configuration.canvasUrl + readonly property string apiEndpoint: `${canvasUrl.replace(/\/$/, "")}/api/v1` + readonly property string oauth2Token: plasmoid.configuration.oauth2Token + readonly property string authHeader: `Bearer ${oauth2Token}` + function syncCanvas() { - // TODO: networking - for (let i = 0; i < 8; i++) { - announcementsModel.append({course: `VZ${i}01`, title: "Something"}) + let xhr = new XMLHttpRequest() + xhr.open("GET", `${apiEndpoint}/courses`) + xhr.setRequestHeader("Authorization", authHeader) + xhr.onload = function () { + if (xhr.status == 200) { + try { + let courses = JSON.parse(xhr.responseText) + for (let c of courses) { + announcementsModel.append({course: c.name, title: ""}) + } + } catch (e) { + if (e instanceof SyntaxError) { console.error("Cannot parse JSON:") } + else { throw e } + } + } else { + console.error("XHR failed") + } } + xhr.send() } Timer { @@ -25,9 +46,15 @@ Item { onTriggered: syncCanvas(); } + Plasmoid.preferredRepresentation: Plasmoid.fullRepresentation + + Plasmoid.fullRepresentation: main + ColumnLayout { id: main anchors.fill: parent + width: PlasmaCore.Units.gridUnit * 20 + height: PlasmaCore.Units.gridUnit * 40 PlasmaExtras.Heading { level: 1 |