From 6e16292f0629eeaeb3341cc014470eb912b0e57f Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 17 Jan 2024 18:57:34 -0300 Subject: [PATCH] debugging adding an asset --- API/api.cpp | 10 ++-- qml/Application/Assets.qml | 47 +++++++++++++--- qml/Application/Main.qml | 4 ++ qml/Application/Root.qml | 10 ++++ qml/Forms/AddAsset.qml | 106 +++++++++++++++++++++++++++++++++++++ qml/Forms/CMakeLists.txt | 1 + 6 files changed, 169 insertions(+), 9 deletions(-) create mode 100644 qml/Forms/AddAsset.qml diff --git a/API/api.cpp b/API/api.cpp index 567bad1..3be7e04 100644 --- a/API/api.cpp +++ b/API/api.cpp @@ -281,11 +281,13 @@ void API::addAsset(const QString& title, const QString& icon, const QJSValue &fi QUrlQuery params({ {"title", title}, - {"icon", icon} + {"icon", icon}, + {"currency", "1"} }); QNetworkRequest request(createUrl("/addAsset")); request.setHeader(QNetworkRequest::ContentTypeHeader, urlEncoded); + request.setRawHeader("Authorization", "Bearer " + accessToken.toUtf8()); QNetworkReply* reply = network.post(request, params.toString(QUrl::FullyEncoded).toUtf8()); connect(reply, &QNetworkReply::finished, std::bind(&API::onAssetAdded, this, reply, finished) @@ -295,8 +297,8 @@ void API::addAsset(const QString& title, const QString& icon, const QJSValue &fi void API::onAssetAdded(QNetworkReply *reply, const QJSValue &finished) { std::unique_ptr rpl(reply); QNetworkReply::NetworkError error = reply->error(); - std::optional data = readResult(reply); + qDebug() << error; if (error != QNetworkReply::NoError) return callCallback(finished, reply->errorString()); @@ -332,6 +334,8 @@ void API::onPollFinished() { Codes::Poll code = Codes::Poll::unknownError; if (validateResponse(data, resultStructure)) code = Codes::convertPoll(data->value("result").toInt()); + else + qDebug() << ""; QString detail = Codes::description(code); @@ -457,7 +461,7 @@ void API::responseAssets(QNetworkReply *reply) { } QVariantMap::ConstIterator itr = data->find("assets"); - if (itr == data->end() || !itr->canConvert()) { + if (itr == data->constEnd() || !itr->canConvert()) { qDebug() << "Error receiving assets: assets are missing or not in an array"; return; } diff --git a/qml/Application/Assets.qml b/qml/Application/Assets.qml index 6567c5a..95b23d6 100644 --- a/qml/Application/Assets.qml +++ b/qml/Application/Assets.qml @@ -4,13 +4,48 @@ import QtQuick import QtQuick.Controls +import magpie.API + Item { - Label { - anchors.centerIn: parent - text: "This is Assets screen" - font { - pixelSize: 24 - bold: true + signal add + + Column { + anchors.fill: parent + + Label { + text: "This is Assets screen" + font { + pixelSize: 24 + bold: true + } + } + + ListView { + anchors.centerIn: parent + model: API.assets + delegate: Rectangle { + Text { + text: title + } + } + } + } + + Button { + anchors { + bottom: parent.bottom + right: parent.right + bottomMargin: 20 + rightMargin: 20 + } + onClicked: add() + + width: 50 + height: 50 + icon { + name: "list-add" + height: 50 + width: 50 } } } diff --git a/qml/Application/Main.qml b/qml/Application/Main.qml index 342d4c5..85a30cd 100644 --- a/qml/Application/Main.qml +++ b/qml/Application/Main.qml @@ -6,6 +6,8 @@ import QtQuick.Controls import QtQuick.Layouts Item { + signal addAsset + property string currentPage: "home" RowLayout { anchors.fill: parent @@ -53,6 +55,7 @@ Item { StackView { id: content initialItem: home + clip: true Layout.fillWidth: true Layout.fillHeight: true @@ -67,6 +70,7 @@ Item { id: assets Assets { StackView.onActivating: currentPage = "assets" + onAdd: addAsset() } } diff --git a/qml/Application/Root.qml b/qml/Application/Root.qml index ae16d82..c62e292 100644 --- a/qml/Application/Root.qml +++ b/qml/Application/Root.qml @@ -4,6 +4,8 @@ import QtQuick import QtQuick.Controls +import magpie.Forms as Forms + Item { StackView { id: stack @@ -14,7 +16,15 @@ Item { Component { id: main Main { + onAddAsset: stack.push(addAssetForm); + } + } + Component { + id: addAssetForm + Forms.AddAsset { + onCancel: stack.pop() + onSuccess: stack.pop() } } } diff --git a/qml/Forms/AddAsset.qml b/qml/Forms/AddAsset.qml new file mode 100644 index 0000000..9f5b150 --- /dev/null +++ b/qml/Forms/AddAsset.qml @@ -0,0 +1,106 @@ +// SPDX-FileCopyrightText: 2023 Yury Gubich +// SPDX-License-Identifier: GPL-3.0-or-later + +import QtQuick +import QtQuick.Controls + +import magpie.API +import magpie.Components as Components + +Item { + signal success + signal cancel + + Column { + id: inner + spacing: 5 + anchors.centerIn: parent + + Label { + anchors.horizontalCenter: parent.horizontalCenter + text: qsTr("New asset") + font.pixelSize: 14 + } + + Grid { + anchors.horizontalCenter: parent.horizontalCenter + columns: 2 + columnSpacing: 10 + rowSpacing: 5 + verticalItemAlignment: Grid.AlignVCenter + horizontalItemAlignment: Grid.AlignRight + + Label { + text: qsTr("Title") + ":"; + } + + TextField { + id: titleField + } + + Label { + text: qsTr("Icon") + ":"; + } + + TextField { + id: iconField + text: "list-add" + } + + Label { + text: qsTr("Color") + ":"; + } + + TextField { + id: colorField + } + } + + Row { + spacing: 5 + anchors.horizontalCenter: parent.horizontalCenter + Button { + text: qsTr("Cancel") + onClicked: cancel() + } + + Button { + text: qsTr("Create") + onClicked: inner.send(titleField.text, titleField.text, colorField.text) + } + } + + Components.Modal { + id: modal + } + + function send (title, icon, color) { + if (modal.inProgress) + return; + + titleField.text = title; + titleField.text = icon; + colorField.text = color; + + modal.inProgress = true; + modal.status = qsTr("Creating new asset ") + " " + title + "..."; + modal.open(); + + API.addAsset(title, icon, function (err, result) { + if (!modal.inProgress) + return; + + modal.inProgress = false; + if (err) + modal.status = err; + else + modal.status = qsTr("Success"); + + if (!!result) { + modal.close(); + success(); + } + }); + } + } +} diff --git a/qml/Forms/CMakeLists.txt b/qml/Forms/CMakeLists.txt index 549b728..10b86b8 100644 --- a/qml/Forms/CMakeLists.txt +++ b/qml/Forms/CMakeLists.txt @@ -11,6 +11,7 @@ qt_add_qml_module(magpieForms QML_FILES Login.qml Register.qml + AddAsset.qml ) target_link_libraries(magpie PRIVATE magpieForms)