hedera-web/js/hedera/basket.js

70 lines
1.4 KiB
JavaScript

module.exports = class {
constructor(app) {
this.app = app;
let orderId = localStorage.getItem('hederaBasket');
if (orderId) orderId = parseInt(orderId);
this.orderId = orderId;
}
async checkOrder(orderId) {
const resultSet = await this.app.conn.execQuery(
'CALL myOrder_checkConfig(#id)',
{id: orderId}
);
resultSet.fetchValue();
}
async check() {
if (this.orderId) {
return await this.checkRedirect();
} else {
this.app.hash.setAll({form: 'ecomerce/checkout'});
return false;
}
}
async checkRedirect() {
try {
await this.checkOrder(this.orderId);
return true;
} catch(err) {
if (err.exception == 'Vn.Lib.UserError') {
const dst = {
form: 'ecomerce/checkout',
continue: 'catalog'
};
switch(err.code) {
case 'orderConfirmed':
case 'orderNotOwnedByUser':
this.constructor.unload();
break;
default:
dst.id = this.orderId;
Htk.Toast.showError(err.message);
}
this.app.hash.setAll(dst);
return false;
} else
throw err;
}
}
async load(orderId) {
this.loadIntoBasket(orderId);
if (await this.checkRedirect())
this.app.hash.setAll({
form: 'ecomerce/catalog'
});
}
loadIntoBasket(orderId) {
if (this.orderId != orderId) {
localStorage.setItem('hederaBasket', orderId);
this.orderId = orderId;
Htk.Toast.showMessage(_('OrderLoadedIntoBasket'));
}
}
static unload() {
localStorage.removeItem('hederaBasket');
}
};