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