2026-04-06 09:24:22 +02:00
|
|
|
import Alpine from 'alpinejs';
|
|
|
|
|
|
|
|
|
|
import { logout, restoreSession, verifyConnection } from '../api/auth.js';
|
|
|
|
|
import { listKitchens } from '../api/kitchens.js';
|
|
|
|
|
import { APP_NAME } from './config.js';
|
|
|
|
|
import { createRouter, navigate } from './router.js';
|
|
|
|
|
import { createAppStore } from './store.js';
|
|
|
|
|
import { appShell } from '../components/app-shell.js';
|
|
|
|
|
import { registerFeatureData } from '../features/register.js';
|
|
|
|
|
|
|
|
|
|
async function installServiceWorker() {
|
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
|
await navigator.serviceWorker.register('/service-worker.js');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function bootstrapApp() {
|
|
|
|
|
const store = createAppStore();
|
|
|
|
|
Alpine.store('app', store);
|
|
|
|
|
|
|
|
|
|
registerFeatureData(Alpine, store);
|
|
|
|
|
|
|
|
|
|
const appRoot = document.querySelector('#app');
|
|
|
|
|
appRoot.innerHTML = appShell(APP_NAME);
|
|
|
|
|
Alpine.initTree(appRoot);
|
|
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
|
Alpine,
|
|
|
|
|
store,
|
|
|
|
|
outlet: document.querySelector('#route-view'),
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-06 17:07:14 +02:00
|
|
|
function applyKitchens(kitchens) {
|
|
|
|
|
store.setKitchens(kitchens);
|
|
|
|
|
if (!store.activeKitchen && kitchens.length) {
|
|
|
|
|
store.setActiveKitchen(kitchens[0]);
|
|
|
|
|
}
|
|
|
|
|
return kitchens;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 09:24:22 +02:00
|
|
|
window.__loncApp = {
|
|
|
|
|
navigate,
|
|
|
|
|
async refreshKitchens() {
|
2026-04-06 17:07:14 +02:00
|
|
|
return applyKitchens(await listKitchens(store));
|
2026-04-06 09:24:22 +02:00
|
|
|
},
|
|
|
|
|
async restoreSession() {
|
|
|
|
|
try {
|
2026-04-06 17:07:14 +02:00
|
|
|
const result = await restoreSession(store);
|
|
|
|
|
if (result?.kitchens) {
|
|
|
|
|
applyKitchens(result.kitchens);
|
|
|
|
|
} else if (store.isConnected) {
|
2026-04-06 09:24:22 +02:00
|
|
|
await window.__loncApp.refreshKitchens();
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (window.location.hash !== '#/login') {
|
|
|
|
|
navigate('/login');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
async verifyConnection() {
|
2026-04-06 17:07:14 +02:00
|
|
|
const result = await verifyConnection(store);
|
|
|
|
|
if (result?.kitchens) {
|
|
|
|
|
applyKitchens(result.kitchens);
|
|
|
|
|
} else if (store.isConnected) {
|
2026-04-06 09:24:22 +02:00
|
|
|
await window.__loncApp.refreshKitchens();
|
|
|
|
|
}
|
2026-04-06 17:07:14 +02:00
|
|
|
return result;
|
2026-04-06 09:24:22 +02:00
|
|
|
},
|
|
|
|
|
async logout() {
|
|
|
|
|
await logout(store);
|
|
|
|
|
navigate('/login');
|
|
|
|
|
},
|
|
|
|
|
router,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener('online', () => {
|
|
|
|
|
store.addAlert({ type: 'success', message: 'Connection restored.' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.addEventListener('offline', () => {
|
|
|
|
|
store.addAlert({
|
|
|
|
|
type: 'warning',
|
|
|
|
|
message: 'You are offline. Cached screens stay available, but API actions may fail.',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.__loncApp
|
|
|
|
|
.restoreSession()
|
|
|
|
|
.finally(() => router.start())
|
|
|
|
|
.catch(() => router.start());
|
|
|
|
|
|
|
|
|
|
installServiceWorker().catch(() => {
|
|
|
|
|
store.addAlert({
|
|
|
|
|
type: 'warning',
|
|
|
|
|
message: 'PWA installation support could not be initialized.',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|