18186e16cf91ac208dd2d205df4700b84961c2bd
[e-mobility-charging-stations-simulator.git] / ui / web / src / main.ts
1 import { createApp } from 'vue'
2 import type { ConfigurationData, ResponsePayload } from './types'
3 import { router } from '@/router'
4 import { UIClient } from '@/composables'
5 import App from '@/App.vue'
6
7 const initializeApp = (config: ConfigurationData) => {
8 const app = createApp(App)
9 app.config.errorHandler = (error, instance, info) => {
10 console.error('Error:', error)
11 console.info('Vue instance:', instance)
12 console.info('Error info:', info)
13 // TODO: add code for UI notifications or other error handling logic
14 }
15 app.config.globalProperties.$uiClient = UIClient.getInstance(config)
16 app.config.globalProperties.$uiClient.registerWSEventListener('open', () => {
17 app.config.globalProperties.$uiClient
18 .listChargingStations()
19 .then((response: ResponsePayload) => {
20 app.config.globalProperties.$chargingStations = response.chargingStations
21 })
22 .catch((error: Error) => {
23 // TODO: add code for UI notifications or other error handling logic
24 console.error('Error at fetching charging stations:', error)
25 throw error
26 })
27 .finally(() => {
28 app.use(router).mount('#app')
29 })
30 })
31 }
32
33 fetch('/config.json')
34 .then(response => response.json())
35 .catch(error => {
36 console.error('Error at fetching app configuration:', error)
37 throw error
38 })
39 .then(config => {
40 initializeApp(config)
41 })
42 .catch(error => {
43 console.error('Error at initializing app:', error)
44 throw error
45 })