X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fmain.ts;h=a39c049ba99052c4e48a2898fe3039508f1aab15;hb=47a41cfff7de841a931f5694dbcddaf235e91bdd;hp=bce88243188951771f87f1b3bade16661a3411b6;hpb=0e60d4cc18d6b2ea9a8d69906a078109bcc94cdc;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/main.ts b/ui/web/src/main.ts index bce88243..a39c049b 100644 --- a/ui/web/src/main.ts +++ b/ui/web/src/main.ts @@ -1,44 +1,71 @@ -import { createApp } from 'vue' -import type { ConfigurationData, ResponsePayload } from './types' -import { router } from '@/router' -import { UIClient } from '@/composables' +import 'vue-toast-notification/dist/theme-bootstrap.css' + +import { type App as AppType, createApp, ref } from 'vue' +import ToastPlugin from 'vue-toast-notification' + import App from '@/App.vue' +import { getFromLocalStorage, setToLocalStorage, UIClient } from '@/composables' +import { router } from '@/router' +import type { ChargingStationData, ConfigurationData, UIServerConfigurationSection } from '@/types' + +const app = createApp(App) -const initializeApp = (config: ConfigurationData) => { - 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 } - app.config.globalProperties.$uiClient = UIClient.getInstance(config) - app.config.globalProperties.$uiClient.registerWSonOpenListener(() => { - app.config.globalProperties.$uiClient - .listChargingStations() - .then((response: ResponsePayload) => { - app.config.globalProperties.$chargingStations = response.chargingStations - }) - .catch((error: Error) => { - console.error('Error at fetching charging stations:', error) - throw error - }) - .finally(() => { - app.use(router).mount('#app') - }) - }) + if (!Array.isArray(config.uiServer)) { + config.uiServer = [config.uiServer] + } + if (app.config.globalProperties.$configuration == null) { + app.config.globalProperties.$configuration = ref(config) + } + if (!Array.isArray(app.config.globalProperties.$templates?.value)) { + app.config.globalProperties.$templates = ref([]) + } + if (!Array.isArray(app.config.globalProperties.$chargingStations?.value)) { + app.config.globalProperties.$chargingStations = ref([]) + } + if ( + getFromLocalStorage('uiServerConfigurationIndex', undefined) == null || + getFromLocalStorage('uiServerConfigurationIndex', 0) > + (app.config.globalProperties.$configuration.value.uiServer as UIServerConfigurationSection[]) + .length - + 1 + ) { + setToLocalStorage('uiServerConfigurationIndex', 0) + } + if (app.config.globalProperties.$uiClient == null) { + app.config.globalProperties.$uiClient = UIClient.getInstance( + (app.config.globalProperties.$configuration.value.uiServer as UIServerConfigurationSection[])[ + getFromLocalStorage('uiServerConfigurationIndex', 0) + ] + ) + } + app.use(router).use(ToastPlugin).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 => { + initializeApp(app, config) + }) + .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) })