refactor: cleanup vue.js global properties usage
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 5 Mar 2024 20:32:25 +0000 (21:32 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 5 Mar 2024 20:32:25 +0000 (21:32 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
ui/web/src/components/actions/AddChargingStations.vue
ui/web/src/components/actions/SetSupervisionUrl.vue
ui/web/src/components/actions/StartTransaction.vue
ui/web/src/global-properties.d.ts [new file with mode: 0644]
ui/web/src/main.ts
ui/web/src/views/ChargingStationsView.vue

index 5eb51f35b3c724425676198124e80c6aaf768714..2cac132291276d62c633309c1d0492231118a625 100644 (file)
@@ -4,11 +4,8 @@
   <select v-model="state.template">
     <option disabled value="">Please select a template</option>
     <option
-      v-for="template in app?.appContext.config.globalProperties.$templates"
-      v-show="
-        Array.isArray(app?.appContext.config.globalProperties.$templates) &&
-        app.appContext.config.globalProperties.$templates.length > 0
-      "
+      v-for="template in $templates.value"
+      v-show="Array.isArray($templates.value) && $templates.value.length > 0"
     >
       {{ template }}
     </option>
@@ -71,7 +68,7 @@
     id="action-button"
     @click="
       () => {
-        uiClient
+        $uiClient
           .addChargingStations(state.template, state.numberOfStations, {
             supervisionUrls: state.supervisionUrl.length > 0 ? state.supervisionUrl : undefined,
             autoStart: convertToBoolean(state.autoStart),
@@ -97,8 +94,7 @@
 </template>
 
 <script setup lang="ts">
-import { getCurrentInstance, ref } from 'vue'
-import { useToast } from 'vue-toast-notification'
+import { ref } from 'vue'
 import Button from '@/components/buttons/Button.vue'
 import { convertToBoolean } from '@/composables'
 
@@ -119,12 +115,6 @@ const state = ref<{
   ocppStrictCompliance: true,
   enableStatistics: false
 })
-
-const app = getCurrentInstance()
-
-const uiClient = app?.appContext.config.globalProperties.$uiClient
-
-const $toast = useToast()
 </script>
 
 <style>
index 2a419677c84b176716ff8722d37ca32a27602263..887b1d4ce42bee2b0a0dd86e910fc4c455081c76 100644 (file)
@@ -14,7 +14,7 @@
     id="action-button"
     @click="
       () => {
-        uiClient
+        $uiClient
           .setSupervisionUrl(hashId, state.supervisionUrl)
           .then(() => {
             $toast.success('Supervision url successfully set')
@@ -34,7 +34,7 @@
 </template>
 
 <script setup lang="ts">
-import { getCurrentInstance, ref } from 'vue'
+import { ref } from 'vue'
 import Button from '@/components/buttons/Button.vue'
 
 defineProps<{
@@ -45,8 +45,6 @@ defineProps<{
 const state = ref<{ supervisionUrl: string }>({
   supervisionUrl: ''
 })
-
-const uiClient = getCurrentInstance()?.appContext.config.globalProperties.$uiClient
 </script>
 
 <style>
index f2a3242f518b6955bb092997c3e7e1fa5382dba2..18145d8d8fad25fbc7467f2acca80aeb9fe33670 100644 (file)
@@ -9,7 +9,7 @@
     id="action-button"
     @click="
       () => {
-        uiClient
+        $uiClient
           .startTransaction(hashId, convertToInt(connectorId), state.idTag)
           .then(() => {
             $toast.success('Transaction successfully started')
@@ -29,7 +29,7 @@
 </template>
 
 <script setup lang="ts">
-import { getCurrentInstance, ref } from 'vue'
+import { ref } from 'vue'
 import Button from '@/components/buttons/Button.vue'
 import { convertToInt } from '@/composables'
 
@@ -42,8 +42,6 @@ defineProps<{
 const state = ref<{ idTag: string }>({
   idTag: ''
 })
-
-const uiClient = getCurrentInstance()?.appContext.config.globalProperties.$uiClient
 </script>
 
 <style>
diff --git a/ui/web/src/global-properties.d.ts b/ui/web/src/global-properties.d.ts
new file mode 100644 (file)
index 0000000..f20cc81
--- /dev/null
@@ -0,0 +1,10 @@
+export {}
+
+declare module 'vue' {
+  interface ComponentCustomProperties {
+    $configuration: Ref<ConfigurationData>
+    $templates: Ref<string[]>
+    $chargingStations: Ref<ChargingStationData[]>
+    $uiClient: UIClient
+  }
+}
index 5d34ae912e8ecb44f36c276b9542e6c0dc2c6bdc..8181374ec60f760296c2aa0788545794dded14ec 100644 (file)
@@ -1,6 +1,6 @@
-import { type App as AppType, createApp } from 'vue'
+import { type App as AppType, createApp, ref } from 'vue'
 import ToastPlugin from 'vue-toast-notification'
-import type { ConfigurationData } from '@/types'
+import type { ChargingStationData, ConfigurationData } from '@/types'
 import { router } from '@/router'
 import { UIClient, getFromLocalStorage, setToLocalStorage } from '@/composables'
 import App from '@/App.vue'
@@ -19,24 +19,24 @@ const initializeApp = (app: AppType, config: ConfigurationData) => {
     config.uiServer = [config.uiServer]
   }
   if (app.config.globalProperties.$configuration == null) {
-    app.config.globalProperties.$configuration = config
+    app.config.globalProperties.$configuration = ref<ConfigurationData>(config)
   }
-  if (!Array.isArray(app.config.globalProperties.$templates)) {
-    app.config.globalProperties.$templates = []
+  if (!Array.isArray(app.config.globalProperties.$templates.value)) {
+    app.config.globalProperties.$templates = ref<string[]>([])
   }
-  if (!Array.isArray(app.config.globalProperties.$chargingStations)) {
-    app.config.globalProperties.$chargingStations = []
+  if (!Array.isArray(app.config.globalProperties.$chargingStations.value)) {
+    app.config.globalProperties.$chargingStations = ref<ChargingStationData[]>([])
   }
   if (
     getFromLocalStorage<number | undefined>('uiServerConfigurationIndex', undefined) == null ||
     getFromLocalStorage<number>('uiServerConfigurationIndex', 0) >
-      app.config.globalProperties.$configuration.uiServer.length - 1
+      app.config.globalProperties.$configuration.value.uiServer.length - 1
   ) {
     setToLocalStorage<number>('uiServerConfigurationIndex', 0)
   }
   if (app.config.globalProperties.$uiClient == null) {
     app.config.globalProperties.$uiClient = UIClient.getInstance(
-      app.config.globalProperties.$configuration.uiServer[
+      app.config.globalProperties.$configuration.value.uiServer[
         getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
       ]
     )
index 621c7122132ca6fd5564816446ccdaeaeb7bd21e..a3f91ec9becf94bf51746325ba8109a00f92b29a 100644 (file)
               if (
                 getFromLocalStorage<number>('uiServerConfigurationIndex', 0) !== state.uiServerIndex
               ) {
-                uiClient.setConfiguration(
-                  app?.appContext.config.globalProperties.$configuration.uiServer[
-                    state.uiServerIndex
-                  ]
-                )
+                $uiClient.setConfiguration($configuration.value.uiServer[state.uiServerIndex])
                 registerWSEventListeners()
-                uiClient.registerWSEventListener(
+                $uiClient.registerWSEventListener(
                   'open',
                   () => {
                     setToLocalStorage<number>('uiServerConfigurationIndex', state.uiServerIndex)
                   },
                   { once: true }
                 )
-                uiClient.registerWSEventListener(
+                $uiClient.registerWSEventListener(
                   'error',
                   () => {
                     state.uiServerIndex = getFromLocalStorage<number>(
                       'uiServerConfigurationIndex',
                       0
                     )
-                    uiClient.setConfiguration(
-                      app?.appContext.config.globalProperties.$configuration.uiServer[
+                    $uiClient.setConfiguration(
+                      $configuration.value.uiServer[
                         getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
                       ]
                     )
       />
     </Container>
     <CSTable
-      v-show="
-        Array.isArray(app?.appContext.config.globalProperties.$chargingStations) &&
-        app.appContext.config.globalProperties.$chargingStations.length > 0
-      "
+      v-show="Array.isArray($chargingStations.value) && $chargingStations.value.length > 0"
       :key="state.renderChargingStations"
-      :charging-stations="app?.appContext.config.globalProperties.$chargingStations"
+      :charging-stations="$chargingStations.value"
       @need-refresh="
         () => {
           state.renderAddChargingStations = randomUUID()
@@ -171,7 +164,7 @@ const app = getCurrentInstance()
 
 const clearChargingStations = (): void => {
   if (app != null) {
-    app.appContext.config.globalProperties.$chargingStations = []
+    app.appContext.config.globalProperties.$chargingStations.value = []
   }
   state.value.renderChargingStations = randomUUID()
 }
@@ -206,12 +199,12 @@ const getTemplates = (): void => {
       .listTemplates()
       .then((response: ResponsePayload) => {
         if (app != null) {
-          app.appContext.config.globalProperties.$templates = response.templates
+          app.appContext.config.globalProperties.$templates.value = response.templates
         }
       })
       .catch((error: Error) => {
         if (app != null) {
-          app.appContext.config.globalProperties.$templates = []
+          app.appContext.config.globalProperties.$templates.value = []
         }
         $toast.error('Error at fetching charging station templates')
         console.error('Error at fetching charging station templates:', error)
@@ -229,12 +222,12 @@ const getChargingStations = (): void => {
       .listChargingStations()
       .then((response: ResponsePayload) => {
         if (app != null) {
-          app.appContext.config.globalProperties.$chargingStations = response.chargingStations
+          app.appContext.config.globalProperties.$chargingStations.value = response.chargingStations
         }
       })
       .catch((error: Error) => {
         if (app != null) {
-          app.appContext.config.globalProperties.$chargingStations = []
+          app.appContext.config.globalProperties.$chargingStations.value = []
         }
         $toast.error('Error at fetching charging stations')
         console.error('Error at fetching charging stations:', error)
@@ -273,7 +266,7 @@ onUnmounted(() => {
 })
 
 const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] =
-  app?.appContext.config.globalProperties.$configuration.uiServer.map(
+  app?.appContext.config.globalProperties.$configuration.value.uiServer.map(
     (configuration: UIServerConfigurationSection, index: number) => ({
       index,
       configuration
@@ -299,7 +292,7 @@ const stopSimulator = (): void => {
     .stopSimulator()
     .then(() => {
       if (app != null) {
-        app.appContext.config.globalProperties.$chargingStations = []
+        app.appContext.config.globalProperties.$chargingStations.value = []
       }
       $toast.success('Simulator successfully stopped')
     })