refactor(ui): factor out app initialization
[e-mobility-charging-stations-simulator.git] / ui / web / src / views / ChargingStationsView.vue
index cb8d607d6b79911560061832ff980e8168639db7..a19c14c32283c7d35591552b9486c6a8f048b85a 100644 (file)
@@ -9,20 +9,35 @@
         v-model="state.uiServerIndex"
         @change="
           () => {
-            try {
-              if (
-                getFromLocalStorage<number>('uiServerConfigurationIndex', 0) !== state.uiServerIndex
-              ) {
-                setToLocalStorage<number>('uiServerConfigurationIndex', state.uiServerIndex)
-                app!.appContext.config.globalProperties.$uiClient.setConfiguration(
-                  app?.appContext.config.globalProperties.$configuration.uiServer[
-                    getFromLocalStorage<number>('uiServerConfigurationIndex', state.uiServerIndex)
-                  ]
-                )
-              }
-            } catch (error) {
-              $toast.error('Error at changing UI server configuration')
-              console.error('Error at changing UI server configuration:', error)
+            if (
+              getFromLocalStorage<number>('uiServerConfigurationIndex', 0) !== state.uiServerIndex
+            ) {
+              app?.appContext.config.globalProperties.$uiClient.setConfiguration(
+                app?.appContext.config.globalProperties.$configuration.uiServer[state.uiServerIndex]
+              )
+              initializeWSEventListeners()
+              app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
+                'open',
+                () => {
+                  setToLocalStorage<number>('uiServerConfigurationIndex', state.uiServerIndex)
+                  $router.currentRoute.value.name !== 'charging-stations' &&
+                    $router.push({ name: 'charging-stations' })
+                },
+                { once: true }
+              )
+              app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
+                'error',
+                () => {
+                  state.uiServerIndex = getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
+                  app?.appContext.config.globalProperties.$uiClient.setConfiguration(
+                    app?.appContext.config.globalProperties.$configuration.uiServer[
+                      getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
+                    ]
+                  )
+                  initializeWSEventListeners()
+                },
+                { once: true }
+              )
             }
           }
         "
@@ -31,7 +46,7 @@
           v-for="uiServerConfiguration in uiServerConfigurations"
           :value="uiServerConfiguration.index"
         >
-          {{ uiServerConfiguration.configuration.host }}
+          {{ uiServerConfiguration.configuration.name ?? uiServerConfiguration.configuration.host }}
         </option>
       </select>
     </Container>
@@ -43,8 +58,8 @@
       </Button>
       <ReloadButton
         id="reload-button"
-        :loading="state.isLoading"
-        @click="loadChargingStations(() => $router.go(0))"
+        :loading="state.loading"
+        @click="loadChargingStations(() => (state.renderChargingStationsList = randomUUID()))"
       />
     </Container>
     <CSTable
         Array.isArray(app?.appContext.config.globalProperties.$chargingStations) &&
         app?.appContext.config.globalProperties.$chargingStations.length > 0
       "
+      :key="state.renderChargingStationsList"
       :charging-stations="app?.appContext.config.globalProperties.$chargingStations"
     />
   </Container>
 </template>
 
 <script setup lang="ts">
-import { getCurrentInstance, reactive } from 'vue'
+import { getCurrentInstance, onMounted, ref } from 'vue'
 import { useToast } from 'vue-toast-notification'
 import CSTable from '@/components/charging-stations/CSTable.vue'
 import type { ResponsePayload, UIServerConfigurationSection } from '@/types'
@@ -67,12 +83,50 @@ import ReloadButton from '@/components/buttons/ReloadButton.vue'
 import Button from '@/components/buttons/Button.vue'
 import { getFromLocalStorage, setToLocalStorage } from '@/composables'
 
-const state = reactive({
-  isLoading: false,
+const randomUUID = (): `${string}-${string}-${string}-${string}-${string}` => {
+  return crypto.randomUUID()
+}
+
+const app = getCurrentInstance()
+
+const initializeWSEventListeners = () => {
+  app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('open', () => {
+    uiClient
+      .listTemplates()
+      .then((response: ResponsePayload) => {
+        if (app != null) {
+          app.appContext.config.globalProperties.$templates = response.templates
+        }
+      })
+      .catch((error: Error) => {
+        if (app != null) {
+          app.appContext.config.globalProperties.$templates = []
+        }
+        $toast.error('Error at fetching charging station templates')
+        console.error('Error at fetching charging station templates:', error)
+      })
+    loadChargingStations(() => (state.value.renderChargingStationsList = randomUUID()))
+  })
+  app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('error', () => {
+    app.appContext.config.globalProperties.$chargingStations = []
+    state.value.renderChargingStationsList = randomUUID()
+  })
+  app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('close', () => {
+    app.appContext.config.globalProperties.$chargingStations = []
+    state.value.renderChargingStationsList = randomUUID()
+  })
+}
+
+onMounted(() => {
+  initializeWSEventListeners()
+})
+
+const state = ref({
+  renderChargingStationsList: randomUUID(),
+  loading: false,
   uiServerIndex: getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
 })
 
-const app = getCurrentInstance()
 const uiClient = app?.appContext.config.globalProperties.$uiClient
 const uiServerConfigurations: { configuration: UIServerConfigurationSection; index: number }[] =
   app?.appContext.config.globalProperties.$configuration.uiServer.map(
@@ -84,9 +138,9 @@ const uiServerConfigurations: { configuration: UIServerConfigurationSection; ind
 
 const $toast = useToast()
 
-const loadChargingStations = (reloadCallback?: () => void): void => {
-  if (state.isLoading === false) {
-    state.isLoading = true
+const loadChargingStations = (renderCallback?: () => void): void => {
+  if (state.value.loading === false) {
+    state.value.loading = true
     uiClient
       .listChargingStations()
       .then((response: ResponsePayload) => {
@@ -95,14 +149,17 @@ const loadChargingStations = (reloadCallback?: () => void): void => {
         }
       })
       .catch((error: Error) => {
+        if (app != null) {
+          app.appContext.config.globalProperties.$chargingStations = []
+        }
         $toast.error('Error at fetching charging stations')
         console.error('Error at fetching charging stations:', error)
       })
       .finally(() => {
-        if (reloadCallback != null) {
-          reloadCallback()
+        if (renderCallback != null) {
+          renderCallback()
         }
-        state.isLoading = false
+        state.value.loading = false
       })
   }
 }