X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fmain.ts;h=25f92db011de67480df34a95dc48c33dec318b94;hb=229d8c3413d0b1908bfd2ea3239f1887bdb862a2;hp=771d42a7202d77cdc0f783ec4fbc2b59d09cbde7;hpb=9e1d6e03d8c2add7d029b3ca756711ff702b7f99;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/main.ts b/ui/web/src/main.ts index 771d42a7..25f92db0 100644 --- a/ui/web/src/main.ts +++ b/ui/web/src/main.ts @@ -1,31 +1,58 @@ import { createApp } from 'vue' -import type { ConfigurationData } from './types' -import router from '@/router' +import ToastPlugin from 'vue-toast-notification' +import type { ConfigurationData, ResponsePayload } from '@/types' +import { router } from '@/router' import { UIClient } from '@/composables' import App from '@/App.vue' +import 'vue-toast-notification/dist/theme-bootstrap.css' -const initializeApp = async (config: ConfigurationData) => { +const initializeApp = (config: ConfigurationData) => { const app = createApp(App) app.config.errorHandler = (error, instance, info) => { console.error('Error:', error) console.info('Vue instance:', instance) console.info('Error info:', info) - // TODO: Add code for UI notifications or other error handling logic + // TODO: add code for UI notifications or other error handling logic } - app.config.globalProperties.$UIClient = UIClient.getInstance(config) - app.use(router).mount('#app') + app.config.globalProperties.$configuration = config + app.config.globalProperties.$chargingStations = [] + app.config.globalProperties.$uiClient = UIClient.getInstance( + app.config.globalProperties.$configuration.uiServer + ) + app.config.globalProperties.$uiClient.registerWSEventListener('open', () => { + app.config.globalProperties.$uiClient + .listChargingStations() + .then((response: ResponsePayload) => { + app.config.globalProperties.$chargingStations = response.chargingStations + }) + .catch((error: Error) => { + // TODO: add code for UI notifications or other error handling logic + console.error('Error at fetching charging stations:', error) + }) + .finally(() => { + app.use(router).use(ToastPlugin).mount('#app') + }) + }) } fetch('/config.json') - .then(response => response.json()) - .catch(error => { - console.error('Error at fetching app configuration:', error) - throw error - }) - .then(config => { - initializeApp(config) + .then(response => { + if (!response.ok) { + // TODO: add code for UI notifications or other error handling logic + console.error('Failed to fetch app configuration') + return + } + response + .json() + .then(config => { + initializeApp(config) + }) + .catch(error => { + // TODO: add code for UI notifications or other error handling logic + console.error('Error at app configuration JSON deserialization:', error) + }) }) .catch(error => { - console.error('Error at initializing app:', error) - throw error + // TODO: add code for UI notifications or other error handling logic + console.error('Error at fetching app configuration:', error) })