fix(ui): handle vue.js app initialization error
[e-mobility-charging-stations-simulator.git] / ui / web / src / main.ts
CommitLineData
66a7748d 1import { createApp } from 'vue'
cea23fa0 2import ToastPlugin from 'vue-toast-notification'
53694372 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 30 console.error('Error at fetching charging stations:', error)
57c0ba05
JB
31 })
32 .finally(() => {
cea23fa0 33 app.use(router).use(ToastPlugin).mount('#app')
57c0ba05
JB
34 })
35 })
a64b9a64 36}
9d76f5ec
JB
37
38fetch('/config.json')
53694372
JB
39 .then(response => {
40 if (!response.ok) {
41 // TODO: add code for UI notifications or other error handling logic
42 console.error('Failed to fetch app configuration')
43 return
44 }
45 response
46 .json()
47 .then(config => {
bcb671db
JB
48 try {
49 initializeApp(config)
50 } catch (error) {
51 // TODO: add code for UI notifications or other error handling logic
52 console.error('Error at initializing the app:', error)
53 }
53694372
JB
54 })
55 .catch(error => {
56 // TODO: add code for UI notifications or other error handling logic
bcb671db 57 console.error('Error at deserializing JSON app configuration:', error)
53694372 58 })
a64b9a64
JB
59 })
60 .catch(error => {
53694372
JB
61 // TODO: add code for UI notifications or other error handling logic
62 console.error('Error at fetching app configuration:', error)
9d76f5ec 63 })