build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / ui / web / src / views / ChargingStationsView.vue
index 3549ec7c3ef7f3a3310638d1fd69601ef9c8c4ae..90433782b17a68b413e94cefb92776a3e333cbb2 100644 (file)
 <template>
   <Container id="charging-stations-container">
-    <Container id="buttons-container">
-      <Button id="button" @click="startSimulator()">Start Simulator</Button>
-      <Button id="button" @click="stopSimulator()">Stop Simulator</Button>
-      <Button id="button" @click="$router.push({ name: 'add-charging-stations' })"
-        >Add Charging Stations</Button
+    <Container
+      v-show="Array.isArray(uiServerConfigurations) && uiServerConfigurations.length > 1"
+      id="ui-server-container"
+    >
+      <select
+        id="ui-server-selector"
+        v-model="state.uiServerIndex"
+        @change="
+          () => {
+            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)
+                  delete app?.appContext.config.globalProperties.$templates
+                  $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 }
+              )
+            }
+          }
+        "
       >
+        <option
+          v-for="uiServerConfiguration in uiServerConfigurations"
+          :value="uiServerConfiguration.index"
+        >
+          {{ uiServerConfiguration.configuration.name ?? uiServerConfiguration.configuration.host }}
+        </option>
+      </select>
+    </Container>
+    <Container id="buttons-container">
+      <Button @click="startSimulator()">Start Simulator</Button>
+      <Button @click="stopSimulator()">Stop Simulator</Button>
+      <Button @click="$router.push({ name: 'add-charging-stations' })">
+        Add Charging Stations
+      </Button>
       <ReloadButton
         id="reload-button"
-        :loading="state.isLoading"
-        @click="loadChargingStations(() => $router.go(0))"
+        :loading="state.loading"
+        @click="loadChargingStations(() => (state.renderChargingStationsList = randomUUID()))"
       />
     </Container>
-    <CSTable :charging-stations="app?.appContext.config.globalProperties.$chargingStations ?? []" />
+    <CSTable
+      v-show="
+        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, reactive } from 'vue'
 import { useToast } from 'vue-toast-notification'
 import CSTable from '@/components/charging-stations/CSTable.vue'
-import type { ResponsePayload } from '@/types'
+import type { ResponsePayload, UIServerConfigurationSection } from '@/types'
 import Container from '@/components/Container.vue'
 import ReloadButton from '@/components/buttons/ReloadButton.vue'
 import Button from '@/components/buttons/Button.vue'
+import { getFromLocalStorage, setToLocalStorage } from '@/composables'
+
+const randomUUID = (): `${string}-${string}-${string}-${string}-${string}` => {
+  return crypto.randomUUID()
+}
+
+const app = getCurrentInstance()
+
+const initializeWSEventListeners = () => {
+  app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('open', () => {
+    loadChargingStations(() => (state.renderChargingStationsList = randomUUID()))
+  })
+  app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('error', () => {
+    app.appContext.config.globalProperties.$chargingStations = []
+    state.renderChargingStationsList = randomUUID()
+  })
+  app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('close', () => {
+    app.appContext.config.globalProperties.$chargingStations = []
+    state.renderChargingStationsList = randomUUID()
+  })
+}
+
+onMounted(() => {
+  initializeWSEventListeners()
+})
 
 const state = reactive({
-  isLoading: false
+  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(
+    (configuration: UIServerConfigurationSection, index: number) => ({
+      configuration,
+      index
+    })
+  )
 
 const $toast = useToast()
 
-const loadChargingStations = (reloadCallback?: () => void): void => {
-  if (state.isLoading === false) {
-    state.isLoading = true
+const loadChargingStations = (renderCallback?: () => void): void => {
+  if (state.loading === false) {
+    state.loading = true
     uiClient
       .listChargingStations()
       .then((response: ResponsePayload) => {
@@ -45,14 +136,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.loading = false
       })
   }
 }
@@ -72,6 +166,9 @@ const stopSimulator = (): void => {
   uiClient
     .stopSimulator()
     .then(() => {
+      if (app != null) {
+        app.appContext.config.globalProperties.$chargingStations = []
+      }
       $toast.success('Simulator successfully stopped')
     })
     .catch((error: Error) => {
@@ -89,13 +186,23 @@ const stopSimulator = (): void => {
   flex-direction: column;
 }
 
+#ui-server-container {
+  display: flex;
+  flex-direction: row;
+}
+
+#ui-server-selector {
+  width: 100%;
+  text-align: center;
+}
+
 #buttons-container {
   display: flex;
   flex-direction: row;
 }
 
-#button {
-  flex: auto;
+#action-button {
+  flex: none;
 }
 
 #reload-button {
@@ -115,4 +222,10 @@ const stopSimulator = (): void => {
 #reload-button:active {
   background-color: red;
 }
+
+#action {
+  color: white;
+  background-color: black;
+  padding: 1%;
+}
 </style>