Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
juanatsap committed Mar 11, 2021
1 parent ea1cecf commit 2091293
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 32 deletions.
4 changes: 2 additions & 2 deletions html/modals/purchase-modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ <h4>Sizes</h4>
<input class="spinner-input" type="hidden" value="1">
</div>

<a class="button is-solid purchase-button accent-button raised" href="#" onclick="purchaseProduct(this)">
<a class="button is-solid purchase-button raised" onclick="purchaseProduct(this)">
<span>Buy Product For</span>
</a>
<a class="button is-solid grey-button" href="javascript:hideModal('purchase')">
<a class="button is-solid grey-button" onclick="javascript:hideModal('purchase')">
<span>Cancel</span>
</a>
</div>
Expand Down
2 changes: 1 addition & 1 deletion html/sample-content/ecommerce.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h3>Overview</h3>

<div class="inner-wrap">
<div class="stat-block">
<div class="stat-number">{{data.purchasedProducts}}</div>
<div class="stat-number">{{data.wallet.purchasedProducts}}</div>
<span>Adquired Products</span>
</div>
<div class="stat-block is-bordered">
Expand Down
38 changes: 21 additions & 17 deletions js/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
const query = document.querySelector.bind(document);
const queryAll = document.querySelectorAll.bind(document);
const logConfigFile = false; // Shows/hides config file into the console
var showLog = false;
var showEventsLog = false;
var LOGS = true;
var showLog = LOGS;
var showEventsLog = LOGS;
var currentUser = null;

/** *****************************************************/
Expand Down Expand Up @@ -113,7 +114,7 @@ function initPage(user) {
const url = window.location.href;
if (url.indexOf("edit-profile") <= 0) {
user.data.memberType =
user.data.purchasedProducts && user.data.purchasedProducts < 5 ?
user.data.wallet.purchasedProducts && user.data.wallet.purchasedProducts < 5 ?
"Standard" :
"Golden";
user.data.since = user.created.substr(0, 10);
Expand Down Expand Up @@ -143,28 +144,31 @@ function purchaseProduct(element) {
);
purchaseModalButton.classList.add("is-loading");

const purchasedProducts = Number(currentUser.data.purchasedProducts + 1);
const purchasedProducts = Number(currentUser.data.wallet.purchasedProducts + 1);
const price = Number(
element.parentElement.parentElement
.querySelector("#quickview-price")
.innerText.trim()
.substr(1)
);
const credits = Number(currentUser.data.credits - price);
const credits = Number(currentUser.data.wallet.credits - price);
const uid = currentUser.UID;

gigya.accounts.setAccountInfo({
data: { purchasedProducts, credits },
callback: function(event) {
log("Product bought", "SET ACCOUNT INFO");
purchaseModalButton.classList.remove("is-loading");
hideModal("purchase");
// We update the session currentUser and then send it to
currentUser.data.purchasedProducts = purchasedProducts;
currentUser.data.credits = credits;
initPage(currentUser);
},
});
// Checking from frontend. BAD
if (credits && purchasedProducts) {
gigya.accounts.setAccountInfo({
data: { "wallet": { purchasedProducts, credits } },
callback: function(event) {
log("Product bought", "SET ACCOUNT INFO");
purchaseModalButton.classList.remove("is-loading");
hideModal("purchase");
// We update the session currentUser and then send it to
currentUser.data.wallet.purchasedProducts = purchasedProducts;
currentUser.data.wallet.credits = credits;
initPage(currentUser);
},
});
}

// THIS IS THE GOOD WAY!!! BACKEND
// const id_token = 'whatever'; // generate a token with getJWT
Expand Down
41 changes: 29 additions & 12 deletions js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ function includeConfigCss(config) {
*/
function showLoggedHTML(user) {
// Put some dummy data if this does not exist
if (!user.data.purchasedProducts) {
user.data.purchasedProducts = 4;
if (!user.data.wallet.purchasedProducts) {
user.data.wallet.purchasedProducts = 4;
}

if (!user.data.credits) {
user.data.credits = 165;
if (!user.data.wallet.credits) {
user.data.wallet.credits = 165;
}

/* Hide Registration Screenset */
Expand Down Expand Up @@ -313,12 +313,12 @@ function loadSampleContent(user) {
// Get price and fix currency if needed
let fixedCurrency = "$10000";
let classCurrency = "";
if (user.data && user.data.credits) {
if (user.data && user.data.wallet.credits) {
fixedCurrency =
user.data.credits > 0 ?
"$" + user.data.credits :
"-$" + user.data.credits * -1;
classCurrency = user.data.credits > 0 ? "" : "has-text-danger";
user.data.wallet.credits > 0 ?
"$" + user.data.wallet.credits :
"-$" + user.data.wallet.credits * -1;
classCurrency = user.data.wallet.credits > 0 ? "" : "has-text-danger";
}
if (!user.data) {
user.data = {};
Expand Down Expand Up @@ -669,10 +669,17 @@ function showPurchaseModal(element) {
const imagePath = content && content.querySelector('img').getAttribute('src');
const title = content && content.querySelector(".product-info h3").innerText;
const description = content && content.querySelector(".product-info p").innerText;
const buttonText = sourceElement.srcElement.innerText;
const buttonText = sourceElement && sourceElement.srcElement.parentElement.innerText;
const price = buttonText ? Number(buttonText.substr(1)) : "-";
const hasCredits = currentUser && currentUser.data && currentUser.data.wallet && currentUser.data.wallet.credits - price > 0;
const isLogged = currentUser && currentUser.status !== "FAIL";
const enabledButton = !isLogged || hasCredits;
const priceClass = enabledButton ? "accent-button" : "red-button";
if (!imagePath || !title || !description || !buttonText) {

debugger;
}
// Get proper text for button
const isLogged = currentUser && currentUser.status !== "FAIL";
const textForButton = isLogged ? "Buy product for &nbsp;" + buttonText : "LOGIN to buy this product";

// inject the card content into the modal
Expand All @@ -682,7 +689,17 @@ function showPurchaseModal(element) {
purchaseModal.querySelector("#quickview-price").innerText = buttonText;
purchaseModal.querySelector(".product-image img").src = imagePath;

purchaseModal.querySelector(".purchase-button span").innerHTML = textForButton;
const purchaseButton = purchaseModal.querySelector(".purchase-button");
purchaseButton.querySelector("span").innerHTML = textForButton;
purchaseButton.classList.remove("is-loading");

// Check the status of the button (first we clean it from previous state)
purchaseButton.classList.remove("is-disabled", "red-button", "accent-button");
purchaseButton.classList.add(priceClass);
if (!enabledButton) {
purchaseButton.classList.add('is-disabled');
}

});
}

Expand Down

0 comments on commit 2091293

Please sign in to comment.