refactor(ui): refine action bar style
[e-mobility-charging-stations-simulator.git] / ui / web / src / main.ts
index 1b4d7ac607a6177f9aa8d20e1704493c5f86003d..25f92db011de67480df34a95dc48c33dec318b94 100644 (file)
@@ -1,13 +1,58 @@
 import { createApp } from 'vue'
-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 app = createApp(App)
+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
+  }
+  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')
+      })
+  })
+}
 
 fetch('/config.json')
-  .then(response => response.json())
-  .then(config => {
-    app.config.globalProperties.$UIClient = UIClient.getInstance(config)
-    app.use(router).mount('#app')
+  .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(config)
+      })
+      .catch(error => {
+        // TODO: add code for UI notifications or other error handling logic
+        console.error('Error at app configuration JSON deserialization:', error)
+      })
+  })
+  .catch(error => {
+    // TODO: add code for UI notifications or other error handling logic
+    console.error('Error at fetching app configuration:', error)
   })