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