Merge pull request #11180 from zfox23/commerce_useAsynchronous

Commerce frontend now uses asynchronous API
This commit is contained in:
Zach Fox 2017-08-14 09:58:51 -07:00 committed by GitHub
commit b3b118c5f7
3 changed files with 58 additions and 53 deletions

View file

@ -24,40 +24,48 @@ Rectangle {
HifiConstants { id: hifi; } HifiConstants { id: hifi; }
id: checkoutRoot; id: checkoutRoot;
property string itemId; property bool inventoryReceived: false;
property string itemHref; property bool balanceReceived: false;
property int balanceAfterPurchase: commerce.balance() - parseInt(itemPriceText.text, 10); property string itemId: "";
property bool alreadyOwned: checkAlreadyOwned(itemId); property string itemHref: "";
property int balanceAfterPurchase: 0;
property bool alreadyOwned: false;
// Style // Style
color: hifi.colors.baseGray; color: hifi.colors.baseGray;
Hifi.QmlCommerce { Hifi.QmlCommerce {
id: commerce; id: commerce;
onBuyResult: { onBuyResult: {
/*
if (buyFailed) {
sendToScript({method: 'checkout_cancelClicked', params: itemId});
} else {
var success = commerce.buy(itemId, parseInt(itemPriceText.text));
sendToScript({method: 'checkout_buyClicked', success: success, itemId: itemId, itemHref: itemHref});
if (success) {
if (urlHandler.canHandleUrl(itemHref)) {
urlHandler.handleUrl(itemHref);
}
}
}
*/
if (failureMessage.length) { if (failureMessage.length) {
console.log('buy failed', failureMessage); buyButton.text = "Buy Failed";
//fixme sendToScript({method: 'checkout_cancelClicked', params: itemId}); buyButton.enabled = false;
} else { } else {
console.log('buy ok'); if (urlHandler.canHandleUrl(itemHref)) {
//fixme sendToScript({method: 'checkout_buyClicked', success: , itemId: itemId, itemHref: itemHref}); urlHandler.handleUrl(itemHref);
}
sendToScript({method: 'checkout_buySuccess', itemId: itemId});
} }
} }
// FIXME: remove these two after testing onBalanceResult: {
onBalanceResult: console.log('balance', balance, failureMessage); if (failureMessage.length) {
onInventoryResult: console.log('inventory', inventory, failureMessage); console.log("Failed to get balance", failureMessage);
} else {
balanceReceived = true;
hfcBalanceText.text = balance;
balanceAfterPurchase = balance - parseInt(itemPriceText.text, 10);
}
}
onInventoryResult: {
if (failureMessage.length) {
console.log("Failed to get inventory", failureMessage);
} else {
inventoryReceived = true;
if (inventory.indexOf(itemId) !== -1) {
alreadyOwned = true;
} else {
alreadyOwned = false;
}
}
}
} }
// //
@ -229,7 +237,7 @@ Rectangle {
} }
RalewayRegular { RalewayRegular {
id: hfcBalanceText; id: hfcBalanceText;
text: commerce.balance(); text: "--";
// Text size // Text size
size: hfcBalanceTextLabel.size; size: hfcBalanceTextLabel.size;
// Anchors // Anchors
@ -366,15 +374,14 @@ Rectangle {
width: parent.width/2 - anchors.leftMargin*2; width: parent.width/2 - anchors.leftMargin*2;
text: "Cancel" text: "Cancel"
onClicked: { onClicked: {
sendToScript({method: 'checkout_cancelClicked', params: itemId}); //fixme sendToScript({method: 'checkout_cancelClicked', params: itemId});
} }
} }
// "Buy" button // "Buy" button
HifiControlsUit.Button { HifiControlsUit.Button {
property bool buyFailed: false; // fixme
id: buyButton; id: buyButton;
enabled: balanceAfterPurchase >= 0 && !alreadyOwned; enabled: balanceAfterPurchase >= 0 && !alreadyOwned && inventoryReceived && balanceReceived;
color: hifi.buttons.black; color: hifi.buttons.black;
colorScheme: hifi.colorSchemes.dark; colorScheme: hifi.colorSchemes.dark;
anchors.top: parent.top; anchors.top: parent.top;
@ -384,7 +391,7 @@ Rectangle {
anchors.right: parent.right; anchors.right: parent.right;
anchors.rightMargin: 20; anchors.rightMargin: 20;
width: parent.width/2 - anchors.rightMargin*2; width: parent.width/2 - anchors.rightMargin*2;
text: alreadyOwned ? "Already Owned" : "Buy"; text: (inventoryReceived && balanceReceived) ? (alreadyOwned ? "Already Owned" : "Buy") : "--";
onClicked: { onClicked: {
commerce.buy(itemId, parseInt(itemPriceText.text)); commerce.buy(itemId, parseInt(itemPriceText.text));
} }
@ -397,16 +404,6 @@ Rectangle {
// //
// FUNCTION DEFINITIONS START // FUNCTION DEFINITIONS START
// //
function checkAlreadyOwned(idToCheck) {
var inventory = commerce.inventory();
if (inventory.indexOf(idToCheck) !== -1) {
return true;
} else {
return false;
}
}
// //
// Function Name: fromScript() // Function Name: fromScript()
// //
@ -428,11 +425,8 @@ Rectangle {
itemAuthorText.text = message.params.itemAuthor; itemAuthorText.text = message.params.itemAuthor;
itemPriceText.text = message.params.itemPrice; itemPriceText.text = message.params.itemPrice;
itemHref = message.params.itemHref; itemHref = message.params.itemHref;
buyButton.buyFailed = false; commerce.balance();
break; commerce.inventory();
case 'buyFailed':
buyButton.text = "Buy Failed";
buyButton.buyFailed = true;
break; break;
default: default:
console.log('Unrecognized message from marketplaces.js:', JSON.stringify(message)); console.log('Unrecognized message from marketplaces.js:', JSON.stringify(message));

View file

@ -29,6 +29,20 @@ Rectangle {
color: hifi.colors.baseGray; color: hifi.colors.baseGray;
Hifi.QmlCommerce { Hifi.QmlCommerce {
id: commerce; id: commerce;
onBalanceResult: {
if (failureMessage.length) {
console.log("Failed to get balance", failureMessage);
} else {
hfcBalanceText.text = balance;
}
}
onInventoryResult: {
if (failureMessage.length) {
console.log("Failed to get inventory", failureMessage);
} else {
inventoryContentsList.model = inventory;
}
}
} }
// //
@ -101,7 +115,7 @@ Rectangle {
} }
RalewayRegular { RalewayRegular {
id: hfcBalanceText; id: hfcBalanceText;
text: commerce.balance(); text: "--";
// Text size // Text size
size: hfcBalanceTextLabel.size; size: hfcBalanceTextLabel.size;
// Anchors // Anchors
@ -158,7 +172,6 @@ Rectangle {
anchors.left: parent.left; anchors.left: parent.left;
anchors.bottom: parent.bottom; anchors.bottom: parent.bottom;
width: parent.width; width: parent.width;
model: commerce.inventory();
delegate: Item { delegate: Item {
width: parent.width; width: parent.width;
height: 30; height: 30;
@ -247,6 +260,8 @@ Rectangle {
switch (message.method) { switch (message.method) {
case 'updateInventory': case 'updateInventory':
referrerURL = message.referrerURL; referrerURL = message.referrerURL;
commerce.balance();
commerce.inventory();
break; break;
default: default:
console.log('Unrecognized message from marketplaces.js:', JSON.stringify(message)); console.log('Unrecognized message from marketplaces.js:', JSON.stringify(message));

View file

@ -204,12 +204,8 @@
// I don't think this is trivial to do since we also want to inject some JS into the DOM. // I don't think this is trivial to do since we also want to inject some JS into the DOM.
//tablet.popFromStack(); //tablet.popFromStack();
break; break;
case 'checkout_buyClicked': case 'checkout_buySuccess':
if (message.success === true) { tablet.gotoWebScreen(MARKETPLACE_URL + '/items/' + message.itemId, MARKETPLACES_INJECT_SCRIPT_URL);
tablet.gotoWebScreen(MARKETPLACE_URL + '/items/' + message.itemId, MARKETPLACES_INJECT_SCRIPT_URL);
} else {
tablet.sendToQml({ method: 'buyFailed' });
}
//tablet.popFromStack(); //tablet.popFromStack();
break; break;
case 'inventory_itemClicked': case 'inventory_itemClicked':