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

View file

@ -29,6 +29,20 @@ Rectangle {
color: hifi.colors.baseGray;
Hifi.QmlCommerce {
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 {
id: hfcBalanceText;
text: commerce.balance();
text: "--";
// Text size
size: hfcBalanceTextLabel.size;
// Anchors
@ -158,7 +172,6 @@ Rectangle {
anchors.left: parent.left;
anchors.bottom: parent.bottom;
width: parent.width;
model: commerce.inventory();
delegate: Item {
width: parent.width;
height: 30;
@ -247,6 +260,8 @@ Rectangle {
switch (message.method) {
case 'updateInventory':
referrerURL = message.referrerURL;
commerce.balance();
commerce.inventory();
break;
default:
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.
//tablet.popFromStack();
break;
case 'checkout_buyClicked':
if (message.success === true) {
tablet.gotoWebScreen(MARKETPLACE_URL + '/items/' + message.itemId, MARKETPLACES_INJECT_SCRIPT_URL);
} else {
tablet.sendToQml({ method: 'buyFailed' });
}
case 'checkout_buySuccess':
tablet.gotoWebScreen(MARKETPLACE_URL + '/items/' + message.itemId, MARKETPLACES_INJECT_SCRIPT_URL);
//tablet.popFromStack();
break;
case 'inventory_itemClicked':