You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HelenaExpressApp/www/assets/js/account.js

254 lines
9.6 KiB
JavaScript

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
function checkAccountStatus(callback) {
if (inStorage("phonenumber")) {
apirequest(SETTINGS.apis.authorstartverify, {
phone: getStorage("phonenumber"),
accountkey: (inStorage("accountkey") ? getStorage("accountkey") : "")
}, function (resp) {
if (resp.status == "OK") {
if (resp.authok && resp.accountok) {
callback(true);
} else if (!resp.authok && resp.accountok) {
// verify phone or email
// openAccountVerify(resp.verifymsg);
callback("noauth", resp.verifymsg);
} else if (!resp.authok && !resp.accountok) {
callback(false);
} else {
callback("badstate");
}
} else {
router.back();
// Server is saying something's wrong, let's clear the account number in case
// the user wants to try a different one.
removeFromStorage("phonenumber");
app.dialog.alert(resp.msg, "Error");
}
}, function (err) {
router.back();
app.dialog.alert("Something went wrong. Try again later.", "Error");
});
} else {
callback(false);
}
}
function openAccountVerify(verifymsg) {
app.dialog.prompt(verifymsg, "Verify Your Account", function (val) {
verifyCode(val);
}, function (cancel) {
// shrug
}, "");
}
/**
* Confirm auth/login code with server and store login key if successful.
* @param {type} code
* @returns {undefined}
*/
function verifyCode(code) {
app.dialog.preloader("Verifying...");
apirequest(SETTINGS.apis.verifyauthcode, {
code: code,
phone: getStorage("phonenumber")
}, function (resp) {
app.dialog.close();
if (resp.status == "OK") {
setStorage("accountkey", resp.authkey);
app.dialog.alert("This device has been successfully linked to your Helena Express account.", "Account verified!");
displayAccountInfo();
} else if (resp.status == "ERROR") {
app.dialog.alert(resp.msg, "Error");
}
}, function (error) {
app.dialog.close();
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
});
}
function displayAccountInfo() {
$("#loyaltyBalanceBox").addClass("display-none");
$("#loyaltyErrorMessage").html("");
if (inStorage("accountkey") && inStorage("phonenumber")) {
} else {
$("#loyaltyErrorMessage").text("Error: No account connected.");
return;
}
apirequest(SETTINGS.apis.getaccountinfo, {
phone: getStorage("phonenumber"),
accountkey: getStorage("accountkey")
}, function (success) {
$("#hasaccountbox").css("display", "");
$("#loadingaccountbox").css("display", "none");
if (success.status == "OK") {
$("#loyaltyCreditBalanceHeading").text(success.credits + " points");
$("#loyaltyBalanceBox").removeClass("display-none");
var canvas = document.createElement('canvas');
bwipjs.toCanvas(canvas, {
bcid: 'code128', // Barcode type
text: success.phone, // Text to encode
scaleX: 5,
scaleY: 1,
includetext: false, // Show human-readable text
textxalign: 'center', // Always good to set this
eclevel: 'M'
});
$("#accountnumberspan").text("Account number: " + success.phone);
document.getElementById("loyaltyBarcodeImg").src = canvas.toDataURL('image/png');
if (success.payments_setup === false) {
$("#addPaymentMethodBox").css("display", "");
}
$("#accountupdateform input#name").val(success.name);
$("#accountupdateform input#email").val(success.email);
$("#accountupdateform input#streetaddress").val(success.streetaddress);
$("#accountupdateform input#zipcode").val(success.zipcode);
} else {
$("#loyaltyBalanceBox").addClass("display-none");
$("#loyaltyErrorMessage").text("Error: " + success.msg);
}
}, function (error) {
$("#loyaltyErrorMessage").text("Error: Couldn't get your account info. Try again later.");
}, "GET");
}
$("#app").on("click", "#setupAccountBtn", function () {
if ($("#accountsetupform input#phonenumber").val() == "") {
app.dialog.alert("Add your phone number.", "Error");
return;
}
if ($("#accountsetupform input#name").val() == "") {
app.dialog.alert("Add your name.", "Error");
return;
}
if ($("#accountsetupform input#email").val() == "") {
app.dialog.alert("Add your email address.", "Error");
return;
}
if ($("#accountsetupform input#streetaddress").val() == "") {
app.dialog.alert("Add your street address.", "Error");
return;
}
if ($("#accountsetupform input#zipcode").val() == "") {
app.dialog.alert("Add your ZIP Code.", "Error");
return;
}
app.dialog.preloader("Creating Account...");
apirequest(SETTINGS.apis.accountregister, {
phone: $("#accountsetupform input#phonenumber").val(),
name: $("#accountsetupform input#name").val(),
email: $("#accountsetupform input#email").val(),
address: $("#accountsetupform input#streetaddress").val(),
zipcode: $("#accountsetupform input#zipcode").val()
}, function (resp) {
app.dialog.close();
if (resp.status == "ERROR") {
app.dialog.alert(resp.msg, "Error");
return;
} else {
setStorage("phonenumber", resp.phone);
router.refreshPage();
}
}, function (error) {
app.dialog.close();
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
});
});
$("#app").on("click", "#updateAccountBtn", function () {
if ($("#accountupdateform input#name").val() == "") {
app.dialog.alert("Add your name.", "Error");
return;
}
if ($("#accountupdateform input#email").val() == "") {
app.dialog.alert("Add your email address.", "Error");
return;
}
if ($("#accountupdateform input#streetaddress").val() == "") {
app.dialog.alert("Add your street address.", "Error");
return;
}
if ($("#accountupdateform input#zipcode").val() == "") {
app.dialog.alert("Add your ZIP Code.", "Error");
return;
}
app.dialog.preloader("Updating Account...");
apirequest(SETTINGS.apis.accountregister, {
phone: getStorage("phonenumber"),
accountkey: getStorage("accountkey"),
name: $("#accountupdateform input#name").val(),
email: $("#accountupdateform input#email").val(),
address: $("#accountupdateform input#streetaddress").val(),
zipcode: $("#accountupdateform input#zipcode").val()
}, function (resp) {
app.dialog.close();
if (resp.status == "ERROR") {
app.dialog.alert(resp.msg, "Error");
return;
} else {
app.popup.close("#accountUpdatePopup", true);
setStorage("phonenumber", resp.phone);
router.refreshPage();
app.dialog.alert("Account details updated.", "Account Updated");
}
}, function (error) {
app.dialog.close();
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
});
});
$("#app").on("click", "#connectExistingAccountBtn", function () {
app.dialog.prompt("Enter your phone number or account number:", "Connect Your Account", function (val) {
var phone = val.replace(/\D/g, '');
if (phone.length < 10) {
app.dialog.alert("Please enter a full 10-digit phone number.", "Oops!");
return;
}
setStorage("phonenumber", phone);
router.refreshPage();
}, function (cancel) {
// shrug
}, "");
});
$("#app").on("popup:open", "#accountUpdatePopup", function () {
app.input.checkEmptyState("#accountupdateform input#name");
app.input.checkEmptyState("#accountupdateform input#email");
app.input.checkEmptyState("#accountupdateform input#streetaddress");
app.input.checkEmptyState("#accountupdateform input#zipcode");
});
function initAccountPage() {
checkAccountStatus(function (result, msg) {
switch (result) {
case true:
displayAccountInfo();
break;
case false:
$("#setupaccountbox").css("display", "");
$("#loadingaccountbox").css("display", "none");
break;
case "noauth":
openAccountVerify(msg);
break;
case "badstate":
app.dialog.alert("Your account is in an unstable state. This shouldn't happen. Please contact us at (406) 389-8988 and we'll fix it.", "Error");
break;
default:
app.dialog.alert("Something went very wrong. Close the app completely (swipe it away from the app switcher) and re-open it. If you continue to get this message, contact us for help: (406) 389-8988", "Error");
break;
}
});
}