fix: ensure error at adding charging stations not stop further
[e-mobility-charging-stations-simulator.git] / ui / web / src / main.ts
index 6dc93195770718d467a2b03bf78934eacf126e21..a39c049ba99052c4e48a2898fe3039508f1aab15 100644 (file)
@@ -1,38 +1,51 @@
-import { createApp } from 'vue'
+import 'vue-toast-notification/dist/theme-bootstrap.css'
+
+import { type App as AppType, createApp, ref } from 'vue'
 import ToastPlugin from 'vue-toast-notification'
-import type { ConfigurationData, ResponsePayload } from '@/types'
-import { router } from '@/router'
-import { UIClient } from '@/composables'
+
 import App from '@/App.vue'
-import 'vue-toast-notification/dist/theme-bootstrap.css'
+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
   }
-  app.config.globalProperties.$configuration = config
-  app.config.globalProperties.$chargingStations = []
-  app.config.globalProperties.$uiClient = UIClient.getInstance(
-    app.config.globalProperties.$configuration.uiServer
-  )
-  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')
-      })
-  })
+  if (!Array.isArray(config.uiServer)) {
+    config.uiServer = [config.uiServer]
+  }
+  if (app.config.globalProperties.$configuration == null) {
+    app.config.globalProperties.$configuration = ref<ConfigurationData>(config)
+  }
+  if (!Array.isArray(app.config.globalProperties.$templates?.value)) {
+    app.config.globalProperties.$templates = ref<string[]>([])
+  }
+  if (!Array.isArray(app.config.globalProperties.$chargingStations?.value)) {
+    app.config.globalProperties.$chargingStations = ref<ChargingStationData[]>([])
+  }
+  if (
+    getFromLocalStorage<number | undefined>('uiServerConfigurationIndex', undefined) == null ||
+    getFromLocalStorage<number>('uiServerConfigurationIndex', 0) >
+      (app.config.globalProperties.$configuration.value.uiServer as UIServerConfigurationSection[])
+        .length -
+        1
+  ) {
+    setToLocalStorage<number>('uiServerConfigurationIndex', 0)
+  }
+  if (app.config.globalProperties.$uiClient == null) {
+    app.config.globalProperties.$uiClient = UIClient.getInstance(
+      (app.config.globalProperties.$configuration.value.uiServer as UIServerConfigurationSection[])[
+        getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
+      ]
+    )
+  }
+  app.use(router).use(ToastPlugin).mount('#app')
 }
 
 fetch('/config.json')
@@ -45,12 +58,7 @@ fetch('/config.json')
     response
       .json()
       .then(config => {
-        try {
-          initializeApp(config)
-        } catch (error) {
-          // TODO: add code for UI notifications or other error handling logic
-          console.error('Error at initializing app:', error)
-        }
+        initializeApp(app, config)
       })
       .catch(error => {
         // TODO: add code for UI notifications or other error handling logic