X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fmain.ts;h=640adf9f6fd9e225ea6991a3271b13f78f8f1f01;hb=68c6d508a8e4eb7e67c4f174c0f3d305829c2eaf;hp=1c213741953a0e4571411d72f29c7771b3c6b128;hpb=a64b9a64f0cb3b2b9b07ec38ec4001dcc7024a60;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/main.ts b/ui/web/src/main.ts index 1c213741..640adf9f 100644 --- a/ui/web/src/main.ts +++ b/ui/web/src/main.ts @@ -1,31 +1,67 @@ -import { createApp } from 'vue' -import type { ConfigurationData } from './types' -import router from '@/router' -import { UIClient } from '@/composables' +import { type App as AppType, createApp, ref } from 'vue' +import ToastPlugin from 'vue-toast-notification' +import type { ChargingStationData, ConfigurationData } from '@/types' +import { router } from '@/router' +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 = 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.length - 1 + ) { + setToLocalStorage('uiServerConfigurationIndex', 0) } - app.config.globalProperties.$UIClient = UIClient.getInstance(config) - app.config.globalProperties.$UIClient.openWS() - app.use(router).mount('#app') + if (app.config.globalProperties.$uiClient == null) { + app.config.globalProperties.$uiClient = UIClient.getInstance( + app.config.globalProperties.$configuration.value.uiServer[ + 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) + // TODO: add code for UI notifications or other error handling logic + console.error('Error at fetching app configuration:', error) })