forked from verdnatura/hedera-web
146 lines
4.5 KiB
JavaScript
146 lines
4.5 KiB
JavaScript
import { defineStore } from 'pinia';
|
|
import { jApi } from 'boot/axios';
|
|
import useNotify from 'src/composables/useNotify.js';
|
|
import { i18n } from 'src/boot/i18n';
|
|
import { useQuasar } from 'quasar';
|
|
|
|
const { notify } = useNotify();
|
|
|
|
export const useAppStore = defineStore('hedera', {
|
|
state: () => ({
|
|
title: null,
|
|
subtitle: null,
|
|
imageUrl: '',
|
|
useRightDrawer: false,
|
|
rightDrawerOpen: false,
|
|
isHeaderMounted: false,
|
|
menuEssentialLinks: [],
|
|
basketOrderId: null,
|
|
localeDates: {
|
|
days: [],
|
|
months: [],
|
|
daysShort: [],
|
|
monthsShort: []
|
|
}
|
|
}),
|
|
|
|
actions: {
|
|
async getMenuLinks() {
|
|
const sections = await jApi.query('SELECT * FROM myMenu');
|
|
const sectionMap = new Map();
|
|
for (const section of sections) {
|
|
sectionMap.set(section.id, section);
|
|
}
|
|
|
|
const sectionTree = [];
|
|
for (const section of sections) {
|
|
const parent = section.parentFk;
|
|
if (parent) {
|
|
const parentSection = sectionMap.get(parent);
|
|
if (!parentSection) continue;
|
|
let childs = parentSection.childs;
|
|
if (!childs) {
|
|
childs = parentSection.childs = [];
|
|
}
|
|
childs.push(section);
|
|
} else {
|
|
sectionTree.push(section);
|
|
}
|
|
}
|
|
|
|
this.menuEssentialLinks = sectionTree;
|
|
},
|
|
|
|
async loadConfig() {
|
|
const imageUrl = await jApi.getValue('SELECT url FROM imageConfig');
|
|
this.$patch({ imageUrl });
|
|
},
|
|
|
|
getLocaleDates() {
|
|
const { messages, locale } = i18n.global;
|
|
this.localeDates = {
|
|
days: messages.value[locale.value].date.days,
|
|
months: messages.value[locale.value].date.months,
|
|
daysShort: messages.value[locale.value].date.daysShort,
|
|
monthsShort: messages.value[locale.value].date.monthsShort
|
|
};
|
|
},
|
|
|
|
async init() {
|
|
this.getBasketOrderId();
|
|
this.getLocaleDates();
|
|
},
|
|
|
|
getBasketOrderId() {
|
|
this.basketOrderId = localStorage.getItem('hederaBasket');
|
|
},
|
|
|
|
async checkOrder(orderId) {
|
|
const resultSet = await jApi.execQuery(
|
|
'CALL myOrder_checkConfig(#id)',
|
|
{ id: orderId }
|
|
);
|
|
resultSet.fetchValue();
|
|
},
|
|
|
|
async check(checkoutContinue) {
|
|
if (this.basketOrderId) {
|
|
return await this.checkRedirect(checkoutContinue);
|
|
} else {
|
|
this.redirect();
|
|
return false;
|
|
}
|
|
},
|
|
|
|
async checkRedirect(checkoutContinue) {
|
|
try {
|
|
await this.checkOrder(this.basketOrderId);
|
|
return true;
|
|
} catch (err) {
|
|
if (err.exception === 'Vn.Lib.UserError') {
|
|
switch (err.code) {
|
|
case 'orderConfirmed':
|
|
case 'orderNotOwnedByUser':
|
|
await this.redirect();
|
|
break;
|
|
default:
|
|
this.router.push({
|
|
name: 'checkout',
|
|
params: { id: this.basketOrderId },
|
|
query: { continue: checkoutContinue }
|
|
});
|
|
notify(err.message, 'negative');
|
|
}
|
|
return false;
|
|
} else throw err;
|
|
}
|
|
},
|
|
|
|
async redirect() {
|
|
const resultSet = await jApi.execQuery(
|
|
'SELECT COUNT(*) > 0 FROM myOrder'
|
|
);
|
|
if (resultSet.fetchValue()) {
|
|
this.router.push({ name: 'pendingOrders' });
|
|
notify('loadAnOrder', 'warning');
|
|
} else {
|
|
this.router.push({ name: 'checkout' });
|
|
}
|
|
},
|
|
|
|
loadIntoBasket(orderId) {
|
|
if (this.basketOrderId !== orderId) {
|
|
this.basketOrderId = orderId;
|
|
localStorage.setItem('hederaBasket', orderId);
|
|
notify('orderLoadedIntoBasket', 'positive');
|
|
}
|
|
}
|
|
},
|
|
getters: {
|
|
isMobile() {
|
|
const $q = useQuasar();
|
|
return $q?.screen?.width <= 768;
|
|
}
|
|
}
|
|
});
|