From 57c0ba059e47d46ad545d9783920b53ee56c7184 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 17 Feb 2024 18:38:35 +0100 Subject: [PATCH] refactor(ui): move the charging stations data array to vue.js global properties MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../charging-stations/CSConnector.vue | 10 ++-- .../components/charging-stations/CSData.vue | 12 ++--- ui/web/src/main.ts | 21 ++++++-- ui/web/src/views/ChargingStationsView.vue | 53 ++++++++++--------- 4 files changed, 57 insertions(+), 39 deletions(-) diff --git a/ui/web/src/components/charging-stations/CSConnector.vue b/ui/web/src/components/charging-stations/CSConnector.vue index a2e94b99..81d24fac 100644 --- a/ui/web/src/components/charging-stations/CSConnector.vue +++ b/ui/web/src/components/charging-stations/CSConnector.vue @@ -31,18 +31,18 @@ const props = defineProps<{ idTag?: string }>() -const UIClient = getCurrentInstance()?.appContext.config.globalProperties.$UIClient +const uiClient = getCurrentInstance()?.appContext.config.globalProperties.$uiClient function startTransaction(): void { - UIClient.startTransaction(props.hashId, props.connectorId, props.idTag) + uiClient.startTransaction(props.hashId, props.connectorId, props.idTag) } function stopTransaction(): void { - UIClient.stopTransaction(props.hashId, props.transactionId) + uiClient.stopTransaction(props.hashId, props.transactionId) } function startAutomaticTransactionGenerator(): void { - UIClient.startAutomaticTransactionGenerator(props.hashId, props.connectorId) + uiClient.startAutomaticTransactionGenerator(props.hashId, props.connectorId) } function stopAutomaticTransactionGenerator(): void { - UIClient.stopAutomaticTransactionGenerator(props.hashId, props.connectorId) + uiClient.stopAutomaticTransactionGenerator(props.hashId, props.connectorId) } diff --git a/ui/web/src/components/charging-stations/CSData.vue b/ui/web/src/components/charging-stations/CSData.vue index 5d31cb1e..5eaa17b0 100644 --- a/ui/web/src/components/charging-stations/CSData.vue +++ b/ui/web/src/components/charging-stations/CSData.vue @@ -102,22 +102,22 @@ function getWsState(): string { } } -const UIClient = getCurrentInstance()?.appContext.config.globalProperties.$UIClient +const uiClient = getCurrentInstance()?.appContext.config.globalProperties.$uiClient function startChargingStation(): void { - UIClient.startChargingStation(props.chargingStation.stationInfo.hashId) + uiClient.startChargingStation(props.chargingStation.stationInfo.hashId) } function stopChargingStation(): void { - UIClient.stopChargingStation(props.chargingStation.stationInfo.hashId) + uiClient.stopChargingStation(props.chargingStation.stationInfo.hashId) } function openConnection(): void { - UIClient.openConnection(props.chargingStation.stationInfo.hashId) + uiClient.openConnection(props.chargingStation.stationInfo.hashId) } function closeConnection(): void { - UIClient.closeConnection(props.chargingStation.stationInfo.hashId) + uiClient.closeConnection(props.chargingStation.stationInfo.hashId) } function deleteChargingStation(): void { - UIClient.deleteChargingStation(props.chargingStation.stationInfo.hashId) + uiClient.deleteChargingStation(props.chargingStation.stationInfo.hashId) } diff --git a/ui/web/src/main.ts b/ui/web/src/main.ts index 6cbc4b60..27405eb6 100644 --- a/ui/web/src/main.ts +++ b/ui/web/src/main.ts @@ -1,10 +1,10 @@ import { createApp } from 'vue' -import type { ConfigurationData } from './types' +import type { ConfigurationData, ResponsePayload } from './types' import { router } from '@/router' import { UIClient } from '@/composables' import App from '@/App.vue' -const initializeApp = async (config: ConfigurationData) => { +const initializeApp = (config: ConfigurationData) => { const app = createApp(App) app.config.errorHandler = (error, instance, info) => { console.error('Error:', error) @@ -12,8 +12,21 @@ const initializeApp = async (config: ConfigurationData) => { console.info('Error info:', info) // 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.$uiClient = UIClient.getInstance(config) + app.config.globalProperties.$uiClient.registerWSonOpenListener(async () => { + app.config.globalProperties.$uiClient + .listChargingStations() + .then((response: ResponsePayload) => { + app.config.globalProperties.$chargingStations = response.chargingStations + }) + .catch((error: Error) => { + console.error('Error at fetching charging stations:', error) + throw error + }) + .finally(() => { + app.use(router).mount('#app') + }) + }) } fetch('/config.json') diff --git a/ui/web/src/views/ChargingStationsView.vue b/ui/web/src/views/ChargingStationsView.vue index 328e95a8..db374401 100644 --- a/ui/web/src/views/ChargingStationsView.vue +++ b/ui/web/src/views/ChargingStationsView.vue @@ -3,7 +3,7 @@ - + - + -- 2.34.1