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