refactor(ui): revert wrongly introduced code to handle multiples UI
[e-mobility-charging-stations-simulator.git] / ui / web / src / main.ts
index 1c213741953a0e4571411d72f29c7771b3c6b128..97f0167a740f8b5390eee6c1882f31933d287101 100644 (file)
@@ -1,20 +1,35 @@
 import { createApp } from 'vue'
-import type { ConfigurationData } from './types'
-import router from '@/router'
+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'
 
-const initializeApp = async (config: ConfigurationData) => {
+const initializeApp = (config: ConfigurationData) => {
   const app = createApp(App)
   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.openWS()
-  app.use(router).mount('#app')
+  app.config.globalProperties.$uiClient = UIClient.getInstance(config.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)
+        throw error
+      })
+      .finally(() => {
+        app.use(router).use(ToastPlugin).mount('#app')
+      })
+  })
 }
 
 fetch('/config.json')
@@ -28,4 +43,5 @@ fetch('/config.json')
   })
   .catch(error => {
     console.error('Error at initializing app:', error)
+    throw error
   })