Alisa/native/AddData.qml
2024-07-12 10:49:51 +02:00

157 lines
4.2 KiB
QML

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 2.15
import QtQuick.Controls 2.15
Window {
SystemPalette { id: activeColors; colorGroup: SystemPalette.Active }
SystemPalette { id: inactiveColors; colorGroup: SystemPalette.Inactive }
SystemPalette { id: disabledColors; colorGroup: SystemPalette.Disabled }
function getColors() {
return dataScreen.active ? activeColors : inactiveColors;
}
id: dataScreen
width: 640
height: 480
visible: true
color: getColors().window
title: qsTr("Alisa - Add License")
property string authToken: ""
ListModel {
id: groupsModel
}
TextField {
id: nameT
anchors {
horizontalCenter: parent.horizontalCenter
topMargin: 5
top: parent.top // Assuming there's no loginL defined in your code
}
placeholderText: qsTr("Name")
Keys.onReturnPressed: keyT.focus = true
}
TextField {
id: keyT
anchors {
horizontalCenter: parent.horizontalCenter
topMargin: 5
top: nameT.bottom
}
placeholderText: qsTr("Key")
Keys.onReturnPressed: amountT.focus = true
}
TextField {
id: amountT
anchors {
horizontalCenter: parent.horizontalCenter
topMargin: 5
top: keyT.bottom
}
placeholderText: qsTr("Amount")
inputMethodHints: Qt.ImhDigitsOnly
validator: DoubleValidator {
bottom: 0
top: 1000000
}
onTextChanged: {
text = text.replace(/[^0-9]/g, "")
}
Keys.onReturnPressed: noteT.focus = true
}
TextField {
id: noteT
anchors {
horizontalCenter: parent.horizontalCenter
topMargin: 5
top: amountT.bottom
}
placeholderText: qsTr("Notes")
Keys.onReturnPressed: groupSelector.focus = true
}
ComboBox {
id: groupSelector
anchors {
horizontalCenter: parent.horizontalCenter
topMargin: 5
top: noteT.bottom
}
model: groupsModel
textRole: "name"
Keys.onReturnPressed: submitB.focus = true
}
Button {
id: submitB
anchors {
horizontalCenter: parent.horizontalCenter
topMargin: 5
top: groupSelector.bottom
}
text: qsTr("Submit")
onClicked: submit()
Keys.onReturnPressed: submit()
}
function submit() {
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://api.clan-war.net/api/v1/licenses");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer " + authToken);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status != 200) {
console.log("Submission failed: " + xhr.status);
}
}
}
var data = JSON.stringify({
name: nameT.text,
key: keyT.text,
note: noteT.text,
amount: parseInt(amountT.text),
group_id: groupSelector.currentIndex !== -1 ? groupsModel.get(groupSelector.currentIndex).id : "",
});
xhr.send(data);
dataScreen.close()
}
function fetchGroups() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.clan-war.net/api/v1/licenses");
xhr.setRequestHeader("Authorization", "Bearer " + authToken);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
groupsModel.clear();
for (var i = 0; i < response.length; i++) {
groupsModel.append(response[i]);
}
} else {
console.log("Failed to fetch groups: " + xhr.status);
}
}
}
xhr.send();
}
Component.onCompleted: fetchGroups()
}