Commit | Line | Data |
---|---|---|
0344ad2b | 1 | import { type App as AppType, createApp } from 'vue' |
cea23fa0 | 2 | import ToastPlugin from 'vue-toast-notification' |
53694372 | 3 | import type { ConfigurationData, ResponsePayload } from '@/types' |
13c19b7b | 4 | import { router } from '@/router' |
0344ad2b | 5 | import { UIClient, getFromLocalStorage, setToLocalStorage } from '@/composables' |
9d76f5ec | 6 | import App from '@/App.vue' |
dd9a3329 | 7 | import 'vue-toast-notification/dist/theme-bootstrap.css' |
32de5a57 | 8 | |
0344ad2b JB |
9 | const app = createApp(App) |
10 | ||
11 | const 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) { | |
22 | app.config.globalProperties.$configuration = config | |
23 | } | |
24 | if (!Array.isArray(app.config.globalProperties.$chargingStations)) { | |
25 | app.config.globalProperties.$chargingStations = [] | |
26 | } | |
27 | if ( | |
28 | getFromLocalStorage<number | undefined>('uiServerConfigurationIndex', undefined) == null || | |
29 | getFromLocalStorage<number>('uiServerConfigurationIndex', 0) > | |
30 | app.config.globalProperties.$configuration.uiServer.length - 1 | |
31 | ) { | |
32 | setToLocalStorage<number>('uiServerConfigurationIndex', 0) | |
33 | } | |
34 | if (app.config.globalProperties.$uiClient == null) { | |
35 | app.config.globalProperties.$uiClient = UIClient.getInstance( | |
36 | app.config.globalProperties.$configuration.uiServer[ | |
37 | getFromLocalStorage<number>('uiServerConfigurationIndex', 0) | |
38 | ] | |
39 | ) | |
40 | app.config.globalProperties.$uiClient.registerWSEventListener('open', () => { | |
41 | app.config.globalProperties.$uiClient | |
42 | .listChargingStations() | |
43 | .then((response: ResponsePayload) => { | |
44 | app.config.globalProperties.$chargingStations = response.chargingStations | |
45 | }) | |
46 | .catch((error: Error) => { | |
47 | // TODO: add code for UI notifications or other error handling logic | |
48 | console.error('Error at fetching charging stations:', error) | |
49 | }) | |
50 | .finally(() => { | |
51 | app.use(router).use(ToastPlugin).mount('#app') | |
52 | }) | |
53 | }) | |
54 | } | |
a64b9a64 | 55 | } |
9d76f5ec JB |
56 | |
57 | fetch('/config.json') | |
53694372 JB |
58 | .then(response => { |
59 | if (!response.ok) { | |
60 | // TODO: add code for UI notifications or other error handling logic | |
61 | console.error('Failed to fetch app configuration') | |
62 | return | |
63 | } | |
64 | response | |
65 | .json() | |
66 | .then(config => { | |
bcb671db | 67 | try { |
0344ad2b | 68 | initializeApp(app, config) |
bcb671db JB |
69 | } catch (error) { |
70 | // TODO: add code for UI notifications or other error handling logic | |
88d3e6ed | 71 | console.error('Error at initializing app:', error) |
bcb671db | 72 | } |
53694372 JB |
73 | }) |
74 | .catch(error => { | |
75 | // TODO: add code for UI notifications or other error handling logic | |
bcb671db | 76 | console.error('Error at deserializing JSON app configuration:', error) |
53694372 | 77 | }) |
a64b9a64 JB |
78 | }) |
79 | .catch(error => { | |
53694372 JB |
80 | // TODO: add code for UI notifications or other error handling logic |
81 | console.error('Error at fetching app configuration:', error) | |
9d76f5ec | 82 | }) |