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