fix: ensure charging stations array global property is initialized
[e-mobility-charging-stations-simulator.git] / ui / web / src / main.ts
CommitLineData
66a7748d 1import { createApp } from 'vue'
cea23fa0 2import ToastPlugin from 'vue-toast-notification'
57c0ba05 3import type { ConfigurationData, ResponsePayload } from './types'
13c19b7b 4import { router } from '@/router'
9d76f5ec
JB
5import { UIClient } from '@/composables'
6import App from '@/App.vue'
dd9a3329 7import 'vue-toast-notification/dist/theme-bootstrap.css'
32de5a57 8
57c0ba05 9const initializeApp = (config: ConfigurationData) => {
a64b9a64
JB
10 const app = createApp(App)
11 app.config.errorHandler = (error, instance, info) => {
12 console.error('Error:', error)
13 console.info('Vue instance:', instance)
14 console.info('Error info:', info)
c317ae3e 15 // TODO: add code for UI notifications or other error handling logic
a64b9a64 16 }
30fc5f08
JB
17 app.config.globalProperties.$configuration = config
18 app.config.globalProperties.$chargingStations = []
19 app.config.globalProperties.$uiClient = UIClient.getInstance(
20 app.config.globalProperties.$configuration.uiServer
21 )
ca1e5439 22 app.config.globalProperties.$uiClient.registerWSEventListener('open', () => {
57c0ba05
JB
23 app.config.globalProperties.$uiClient
24 .listChargingStations()
25 .then((response: ResponsePayload) => {
26 app.config.globalProperties.$chargingStations = response.chargingStations
27 })
28 .catch((error: Error) => {
c317ae3e 29 // TODO: add code for UI notifications or other error handling logic
57c0ba05
JB
30 console.error('Error at fetching charging stations:', error)
31 throw error
32 })
33 .finally(() => {
cea23fa0 34 app.use(router).use(ToastPlugin).mount('#app')
57c0ba05
JB
35 })
36 })
a64b9a64 37}
9d76f5ec
JB
38
39fetch('/config.json')
40 .then(response => response.json())
a64b9a64
JB
41 .catch(error => {
42 console.error('Error at fetching app configuration:', error)
43 throw error
44 })
9d76f5ec 45 .then(config => {
a64b9a64
JB
46 initializeApp(config)
47 })
48 .catch(error => {
49 console.error('Error at initializing app:', error)
9e1d6e03 50 throw error
9d76f5ec 51 })