build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / ui / web / src / main.ts
CommitLineData
84ec8d34
JB
1import 'vue-toast-notification/dist/theme-bootstrap.css'
2
7e315430 3import { type App as AppType, createApp, ref } from 'vue'
cea23fa0 4import ToastPlugin from 'vue-toast-notification'
84ec8d34 5
9d76f5ec 6import App from '@/App.vue'
84ec8d34
JB
7import { getFromLocalStorage, setToLocalStorage, UIClient } from '@/composables'
8import { router } from '@/router'
9import type { ChargingStationData, ConfigurationData, UIServerConfigurationSection } from '@/types'
32de5a57 10
0344ad2b
JB
11const app = createApp(App)
12
13const initializeApp = (app: AppType, config: ConfigurationData) => {
a64b9a64
JB
14 app.config.errorHandler = (error, instance, info) => {
15 console.error('Error:', error)
16 console.info('Vue instance:', instance)
17 console.info('Error info:', info)
c317ae3e 18 // TODO: add code for UI notifications or other error handling logic
a64b9a64 19 }
0344ad2b
JB
20 if (!Array.isArray(config.uiServer)) {
21 config.uiServer = [config.uiServer]
22 }
23 if (app.config.globalProperties.$configuration == null) {
7e315430 24 app.config.globalProperties.$configuration = ref<ConfigurationData>(config)
0344ad2b 25 }
68c6d508 26 if (!Array.isArray(app.config.globalProperties.$templates?.value)) {
7e315430 27 app.config.globalProperties.$templates = ref<string[]>([])
3b0c6e17 28 }
68c6d508 29 if (!Array.isArray(app.config.globalProperties.$chargingStations?.value)) {
7e315430 30 app.config.globalProperties.$chargingStations = ref<ChargingStationData[]>([])
0344ad2b
JB
31 }
32 if (
33 getFromLocalStorage<number | undefined>('uiServerConfigurationIndex', undefined) == null ||
34 getFromLocalStorage<number>('uiServerConfigurationIndex', 0) >
f4b3f35d
JB
35 (app.config.globalProperties.$configuration.value.uiServer as UIServerConfigurationSection[])
36 .length -
37 1
0344ad2b
JB
38 ) {
39 setToLocalStorage<number>('uiServerConfigurationIndex', 0)
40 }
41 if (app.config.globalProperties.$uiClient == null) {
42 app.config.globalProperties.$uiClient = UIClient.getInstance(
f4b3f35d 43 (app.config.globalProperties.$configuration.value.uiServer as UIServerConfigurationSection[])[
0344ad2b
JB
44 getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
45 ]
46 )
0344ad2b 47 }
916fe456 48 app.use(router).use(ToastPlugin).mount('#app')
a64b9a64 49}
9d76f5ec
JB
50
51fetch('/config.json')
53694372
JB
52 .then(response => {
53 if (!response.ok) {
54 // TODO: add code for UI notifications or other error handling logic
55 console.error('Failed to fetch app configuration')
56 return
57 }
58 response
59 .json()
60 .then(config => {
916fe456 61 initializeApp(app, config)
53694372
JB
62 })
63 .catch(error => {
64 // TODO: add code for UI notifications or other error handling logic
bcb671db 65 console.error('Error at deserializing JSON app configuration:', error)
53694372 66 })
a64b9a64
JB
67 })
68 .catch(error => {
53694372
JB
69 // TODO: add code for UI notifications or other error handling logic
70 console.error('Error at fetching app configuration:', error)
9d76f5ec 71 })