refactor(ui): cleanup initial data fetching code
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 5 Mar 2024 14:02:46 +0000 (15:02 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 5 Mar 2024 14:02:46 +0000 (15:02 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
ui/web/src/components/actions/AddChargingStations.vue
ui/web/src/views/ChargingStationsView.vue

index 8946e82a168f4ba9f7f28abc25627661d4531905..5eb51f35b3c724425676198124e80c6aaf768714 100644 (file)
@@ -121,6 +121,7 @@ const state = ref<{
 })
 
 const app = getCurrentInstance()
+
 const uiClient = app?.appContext.config.globalProperties.$uiClient
 
 const $toast = useToast()
index 0468c597dae8fdc3997974aeb9dc3311d51b7f5c..109803f4beb8c2827c058cccddd9ae1235cbec8c 100644 (file)
               if (
                 getFromLocalStorage<number>('uiServerConfigurationIndex', 0) !== state.uiServerIndex
               ) {
-                app?.appContext.config.globalProperties.$uiClient.setConfiguration(
+                uiClient.setConfiguration(
                   app?.appContext.config.globalProperties.$configuration.uiServer[
                     state.uiServerIndex
                   ]
                 )
                 initializeWSEventListeners()
-                app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
+                uiClient.registerWSEventListener(
                   'open',
                   () => {
                     setToLocalStorage<number>('uiServerConfigurationIndex', state.uiServerIndex)
                   },
                   { once: true }
                 )
-                app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
+                uiClient.registerWSEventListener(
                   'error',
                   () => {
                     state.uiServerIndex = getFromLocalStorage<number>(
                       'uiServerConfigurationIndex',
                       0
                     )
-                    app?.appContext.config.globalProperties.$uiClient.setConfiguration(
+                    uiClient.setConfiguration(
                       app?.appContext.config.globalProperties.$configuration.uiServer[
                         getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
                       ]
@@ -157,8 +157,6 @@ const simulatorButtonMessage = computed<string>(
     `${state.value.simulatorState?.started === true ? 'Stop' : 'Start'} Simulator${state.value.simulatorState?.version != null ? ` (${state.value.simulatorState.version})` : ''}`
 )
 
-const app = getCurrentInstance()
-
 const clearToggleButtons = (): void => {
   for (const key in getLocalStorage()) {
     if (key.includes('toggle-button')) {
@@ -169,6 +167,8 @@ const clearToggleButtons = (): void => {
   state.value.renderAddChargingStations = randomUUID()
 }
 
+const app = getCurrentInstance()
+
 const clearChargingStations = (): void => {
   if (app != null) {
     app.appContext.config.globalProperties.$chargingStations = []
@@ -178,6 +178,8 @@ const clearChargingStations = (): void => {
 
 const uiClient = app?.appContext.config.globalProperties.$uiClient
 
+const $toast = useToast()
+
 const getSimulatorState = (): void => {
   if (state.value.gettingSimulatorState === false) {
     state.value.gettingSimulatorState = true
@@ -197,55 +199,29 @@ const getSimulatorState = (): void => {
   }
 }
 
-const initializeWSEventListeners = () => {
-  app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('open', () => {
-    getSimulatorState()
-    if (state.value.gettingTemplates === false) {
-      state.value.gettingTemplates = true
-      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)
-        })
-        .finally(() => {
-          state.value.gettingTemplates = false
-        })
-    }
-    getChargingStations()
-  })
-  app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
-    'error',
-    clearChargingStations
-  )
-  app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
-    'close',
-    clearChargingStations
-  )
+const getTemplates = (): void => {
+  if (state.value.gettingTemplates === false) {
+    state.value.gettingTemplates = true
+    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)
+      })
+      .finally(() => {
+        state.value.gettingTemplates = false
+      })
+  }
 }
 
-onMounted(() => {
-  initializeWSEventListeners()
-})
-
-const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] =
-  app?.appContext.config.globalProperties.$configuration.uiServer.map(
-    (configuration: UIServerConfigurationSection, index: number) => ({
-      index,
-      configuration
-    })
-  )
-
-const $toast = useToast()
-
 const getChargingStations = (): void => {
   if (state.value.gettingChargingStations === false) {
     state.value.gettingChargingStations = true
@@ -270,6 +246,28 @@ const getChargingStations = (): void => {
   }
 }
 
+const initializeWSEventListeners = () => {
+  uiClient.registerWSEventListener('open', () => {
+    getSimulatorState()
+    getTemplates()
+    getChargingStations()
+  })
+  uiClient.registerWSEventListener('error', clearChargingStations)
+  uiClient.registerWSEventListener('close', clearChargingStations)
+}
+
+onMounted(() => {
+  initializeWSEventListeners()
+})
+
+const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] =
+  app?.appContext.config.globalProperties.$configuration.uiServer.map(
+    (configuration: UIServerConfigurationSection, index: number) => ({
+      index,
+      configuration
+    })
+  )
+
 const startSimulator = (): void => {
   uiClient
     .startSimulator()