X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fmain.ts;h=f3c9ef036f6fb46445aa2f06f0c482d632e4e1cc;hb=0344ad2b24f8b043b5848a0fea8b5a120d6781db;hp=6cbc4b608b2ee73478ea1f2deb7d1663ddf42430;hpb=13c19b7bd220baedac3b4870057501cb4891acf9;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/main.ts b/ui/web/src/main.ts index 6cbc4b60..f3c9ef03 100644 --- a/ui/web/src/main.ts +++ b/ui/web/src/main.ts @@ -1,31 +1,82 @@ -import { createApp } from 'vue' -import type { ConfigurationData } from './types' +import { type App as AppType, createApp } from 'vue' +import ToastPlugin from 'vue-toast-notification' +import type { ConfigurationData, ResponsePayload } from '@/types' import { router } from '@/router' -import { UIClient } from '@/composables' +import { UIClient, getFromLocalStorage, setToLocalStorage } from '@/composables' import App from '@/App.vue' +import 'vue-toast-notification/dist/theme-bootstrap.css' -const initializeApp = async (config: ConfigurationData) => { - const app = createApp(App) +const app = createApp(App) + +const initializeApp = (app: AppType, config: ConfigurationData) => { app.config.errorHandler = (error, instance, info) => { console.error('Error:', error) console.info('Vue instance:', instance) console.info('Error info:', info) - // TODO: Add code for UI notifications or other error handling logic + // TODO: add code for UI notifications or other error handling logic + } + if (!Array.isArray(config.uiServer)) { + config.uiServer = [config.uiServer] + } + if (app.config.globalProperties.$configuration == null) { + app.config.globalProperties.$configuration = config + } + if (!Array.isArray(app.config.globalProperties.$chargingStations)) { + app.config.globalProperties.$chargingStations = [] + } + if ( + getFromLocalStorage('uiServerConfigurationIndex', undefined) == null || + getFromLocalStorage('uiServerConfigurationIndex', 0) > + app.config.globalProperties.$configuration.uiServer.length - 1 + ) { + setToLocalStorage('uiServerConfigurationIndex', 0) + } + if (app.config.globalProperties.$uiClient == null) { + app.config.globalProperties.$uiClient = UIClient.getInstance( + app.config.globalProperties.$configuration.uiServer[ + getFromLocalStorage('uiServerConfigurationIndex', 0) + ] + ) + app.config.globalProperties.$uiClient.registerWSEventListener('open', () => { + app.config.globalProperties.$uiClient + .listChargingStations() + .then((response: ResponsePayload) => { + app.config.globalProperties.$chargingStations = response.chargingStations + }) + .catch((error: Error) => { + // TODO: add code for UI notifications or other error handling logic + console.error('Error at fetching charging stations:', error) + }) + .finally(() => { + app.use(router).use(ToastPlugin).mount('#app') + }) + }) } - app.config.globalProperties.$UIClient = UIClient.getInstance(config) - app.use(router).mount('#app') } fetch('/config.json') - .then(response => response.json()) - .catch(error => { - console.error('Error at fetching app configuration:', error) - throw error - }) - .then(config => { - initializeApp(config) + .then(response => { + if (!response.ok) { + // TODO: add code for UI notifications or other error handling logic + console.error('Failed to fetch app configuration') + return + } + response + .json() + .then(config => { + try { + initializeApp(app, config) + } catch (error) { + // TODO: add code for UI notifications or other error handling logic + console.error('Error at initializing app:', error) + } + }) + .catch(error => { + // TODO: add code for UI notifications or other error handling logic + console.error('Error at deserializing JSON app configuration:', error) + }) }) .catch(error => { - console.error('Error at initializing app:', error) - throw error + // TODO: add code for UI notifications or other error handling logic + console.error('Error at fetching app configuration:', error) })