hedera-web/js/hedera/basket.js

78 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2023-01-30 10:08:26 +00:00
module.exports = class {
constructor(app) {
this.app = app;
let orderId = localStorage.getItem('hederaBasket');
if (orderId) orderId = parseInt(orderId);
this.orderId = orderId;
}
2023-02-23 13:32:13 +00:00
async checkOrder(orderId) {
2023-01-30 10:08:26 +00:00
const resultSet = await this.app.conn.execQuery(
2023-02-15 16:13:25 +00:00
'CALL myOrder_checkConfig(#id)',
2023-01-30 10:08:26 +00:00
{id: orderId}
);
2023-02-15 13:07:09 +00:00
resultSet.fetchValue();
}
async check(checkoutContinue) {
2023-02-23 13:32:13 +00:00
if (this.orderId) {
return await this.checkRedirect(checkoutContinue);
2023-02-23 13:32:13 +00:00
} else {
this.redirect();
2023-02-23 13:32:13 +00:00
return false;
}
}
async checkRedirect(checkoutContinue) {
2023-02-15 13:07:09 +00:00
try {
2023-02-23 13:32:13 +00:00
await this.checkOrder(this.orderId);
2023-02-15 13:07:09 +00:00
return true;
} catch(err) {
2023-02-23 13:32:13 +00:00
if (err.exception == 'Vn.Lib.UserError') {
2023-02-23 15:50:49 +00:00
switch(err.code) {
case 'orderConfirmed':
case 'orderNotOwnedByUser':
this.constructor.unload();
await this.redirect();
break;
default:
this.app.hash.setAll({
form: 'ecomerce/checkout',
id: this.orderId,
continue: checkoutContinue
});
Htk.Toast.showError(err.message);
2023-02-23 13:32:13 +00:00
}
return false;
} else
throw err;
2023-01-30 10:08:26 +00:00
}
}
async redirect() {
const resultSet = await this.app.conn.execQuery(
'SELECT COUNT(*) > 0 FROM myOrder');
if (resultSet.fetchValue()) {
this.app.hash.setAll({form: 'ecomerce/pending'});
Htk.Toast.showMessage(_('Load an order'));
} else {
this.app.hash.setAll({form: 'ecomerce/checkout'});
}
}
2023-01-30 10:08:26 +00:00
async load(orderId) {
2023-02-15 13:07:09 +00:00
this.loadIntoBasket(orderId);
if (await this.checkRedirect('catalog'))
2023-02-23 13:32:13 +00:00
this.app.hash.setAll({
form: 'ecomerce/catalog'
});
2023-02-15 13:07:09 +00:00
}
loadIntoBasket(orderId) {
2023-01-30 10:08:26 +00:00
if (this.orderId != orderId) {
localStorage.setItem('hederaBasket', orderId);
this.orderId = orderId;
2023-02-15 13:07:09 +00:00
Htk.Toast.showMessage(_('OrderLoadedIntoBasket'));
2023-01-30 10:08:26 +00:00
}
2023-02-15 13:07:09 +00:00
}
static unload() {
localStorage.removeItem('hederaBasket');
2023-01-30 10:08:26 +00:00
}
};