refactor(ui): move the charging stations data array to vue.js global
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 17 Feb 2024 17:38:35 +0000 (18:38 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 17 Feb 2024 17:38:35 +0000 (18:38 +0100)
      properties

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
ui/web/src/components/charging-stations/CSConnector.vue
ui/web/src/components/charging-stations/CSData.vue
ui/web/src/main.ts
ui/web/src/views/ChargingStationsView.vue

index a2e94b9988a7fbf4f9aa659d70baa9c947b17893..81d24face817efd795563e1e5127968091f1914f 100644 (file)
@@ -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)
 }
 </script>
index 5d31cb1ef1b941487e217a01c86156257c0f6939..5eaa17b012bae731566a2355ed03234b8028fb81 100644 (file)
@@ -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)
 }
 </script>
 
index 6cbc4b608b2ee73478ea1f2deb7d1663ddf42430..27405eb66fa969ace6e7bbd8db4b71a3c6393307 100644 (file)
@@ -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')
index 328e95a8094bb53132c093d602d0968c27ef85de..db374401ff7d51164501da254c839c4b47fbbfe8 100644 (file)
@@ -3,7 +3,7 @@
     <Container id="buttons-container">
       <Button id="simulator-button" @click="startSimulator()">Start Simulator</Button>
       <Button id="simulator-button" @click="stopSimulator()">Stop Simulator</Button>
-      <ReloadButton id="reload-button" :loading="state.isLoading" @click="load()" />
+      <ReloadButton id="reload-button" :loading="state.isLoading" @click="loadChargingStations()" />
     </Container>
     <Container id="inputs-container">
       <input
         placeholder="RFID tag"
       />
     </Container>
-    <CSTable :charging-stations="state.chargingStations" :id-tag="state.idTag" />
+    <CSTable
+      :charging-stations="
+        getCurrentInstance()?.appContext.config.globalProperties.$chargingStations
+      "
+      :id-tag="state.idTag"
+    />
   </Container>
 </template>
 
 <script setup lang="ts">
-import { getCurrentInstance, onMounted, reactive } from 'vue'
+import { getCurrentInstance, reactive } from 'vue'
 import CSTable from '@/components/charging-stations/CSTable.vue'
-import type { ChargingStationData } from '@/types'
+import type { ResponsePayload } from '@/types'
 import Container from '@/components/Container.vue'
 import ReloadButton from '@/components/buttons/ReloadButton.vue'
 import Button from '@/components/buttons/Button.vue'
 
-const UIClient = getCurrentInstance()?.appContext.config.globalProperties.$UIClient
-
-onMounted(() => {
-  UIClient.registerWSonOpenListener(load)
-})
-
-type State = {
-  isLoading: boolean
-  chargingStations: ChargingStationData[]
-  idTag: string
-}
-
-const state: State = reactive({
+const state = reactive({
   isLoading: false,
-  chargingStations: [],
   idTag: ''
 })
 
-async function load(): Promise<void> {
+const app = getCurrentInstance()
+const uiClient = app?.appContext.config.globalProperties.$uiClient
+
+function loadChargingStations(): void {
   if (state.isLoading === false) {
     state.isLoading = true
-    state.chargingStations = (await UIClient.listChargingStations())
-      .chargingStations as ChargingStationData[]
-    state.isLoading = false
+    uiClient
+      .listChargingStations()
+      .then((response: ResponsePayload) => {
+        if (app != null) {
+          app.appContext.config.globalProperties.$chargingStations = response.chargingStations
+        }
+      })
+      .catch((error: Error) => {
+        console.error('Error at fetching charging stations:', error)
+      })
+      .finally(() => {
+        state.isLoading = false
+      })
   }
 }
 
 function startSimulator(): void {
-  UIClient.startSimulator()
+  uiClient.startSimulator()
 }
 function stopSimulator(): void {
-  UIClient.stopSimulator()
+  uiClient.stopSimulator()
 }
 </script>