refactor(ui): refine action forms
[e-mobility-charging-stations-simulator.git] / ui / web / src / components / charging-stations / CSData.vue
index c0f07792da49311d81d8d40e283d8ca7ad208ad6..94b7ae8dcbb8a839329ccfefda46e354f7fd6bd5 100644 (file)
@@ -1,27 +1,42 @@
 <template>
   <tr class="cs-table__row">
-    <td class="cs-table__column">{{ getId() }}</td>
-    <td class="cs-table__column">{{ getStarted() }}</td>
+    <td class="cs-table__column">
+      {{ props.chargingStation.stationInfo.chargingStationId }}
+    </td>
+    <td class="cs-table__column">{{ props.chargingStation.started === true ? 'Yes' : 'No' }}</td>
     <td class="cs-table__column">
       {{ getSupervisionUrl() }}
     </td>
-    <td class="cs-table__column">{{ getWsState() }}</td>
+    <td class="cs-table__column">{{ getWSState() }}</td>
     <td class="cs-table__column">
-      {{ getRegistrationStatus() }}
+      {{ props.chargingStation?.bootNotificationResponse?.status ?? 'Ø' }}
     </td>
     <td class="cs-table__column">
-      {{ getInfo().templateName }}
+      {{ props.chargingStation.stationInfo.templateName }}
     </td>
-    <td class="cs-table__column">{{ getVendor() }}</td>
-    <td class="cs-table__column">{{ getModel() }}</td>
+    <td class="cs-table__column">{{ props.chargingStation.stationInfo.chargePointVendor }}</td>
+    <td class="cs-table__column">{{ props.chargingStation.stationInfo.chargePointModel }}</td>
     <td class="cs-table__column">
-      {{ getFirmwareVersion() }}
+      {{ props.chargingStation.stationInfo.firmwareVersion ?? 'Ø' }}
     </td>
     <td class="cs-table__column">
       <Button @click="startChargingStation()">Start Charging Station</Button>
       <Button @click="stopChargingStation()">Stop Charging Station</Button>
+      <Button
+        @click="
+          $router.push({
+            name: 'set-supervision-url',
+            params: {
+              hashId: props.chargingStation.stationInfo.hashId,
+              chargingStationId: props.chargingStation.stationInfo.chargingStationId
+            }
+          })
+        "
+        >Set Supervision Url</Button
+      >
       <Button @click="openConnection()">Open Connection</Button>
       <Button @click="closeConnection()">Close Connection</Button>
+      <Button @click="deleteChargingStation()">Delete Charging Station</Button>
     </td>
     <td class="cs-table__connectors-column">
       <table id="connectors-table">
         <tbody id="connectors-table__body">
           <!-- eslint-disable-next-line vue/valid-v-for -->
           <CSConnector
-            v-for="(connector, index) in getConnectors()"
-            :hash-id="getHashId()"
+            v-for="(connector, index) in getConnectorStatuses()"
+            :hash-id="props.chargingStation.stationInfo.hashId"
+            :charging-station-id="props.chargingStation.stationInfo.chargingStationId"
             :connector-id="index + 1"
             :connector="connector"
             :atg-status="getATGStatus(index + 1)"
-            :transaction-id="connector.transactionId"
-            :id-tag="props.idTag"
           />
         </tbody>
       </table>
 </template>
 
 <script setup lang="ts">
-// import { reactive } from 'vue'
 import { getCurrentInstance } from 'vue'
+import { useToast } from 'vue-toast-notification'
 import CSConnector from '@/components/charging-stations/CSConnector.vue'
-import type { ChargingStationData, ChargingStationInfo, ConnectorStatus, Status } from '@/types'
+import Button from '@/components/buttons/Button.vue'
+import type { ChargingStationData, ConnectorStatus, Status } from '@/types'
 
 const props = defineProps<{
   chargingStation: ChargingStationData
-  idTag: string
 }>()
 
-// type State = {
-//   isTagModalVisible: boolean
-//   idTag: string
-// }
-
-// const state: State = reactive({
-//   isTagModalVisible: false,
-//   idTag: ''
-// })
-
-function getStarted(): string {
-  return props.chargingStation.started === true ? 'Yes' : 'No'
+const getConnectorStatuses = (): ConnectorStatus[] => {
+  if (Array.isArray(props.chargingStation.evses) && props.chargingStation.evses.length > 0) {
+    const connectorsStatus: ConnectorStatus[] = []
+    for (const [evseId, evseStatus] of props.chargingStation.evses.entries()) {
+      if (evseId > 0 && Array.isArray(evseStatus.connectors) && evseStatus.connectors.length > 0) {
+        for (const connectorStatus of evseStatus.connectors) {
+          connectorsStatus.push(connectorStatus)
+        }
+      }
+    }
+    return connectorsStatus
+  }
+  return props.chargingStation.connectors?.slice(1)
 }
-function getATGStatus(connectorId: number): Status | undefined {
+const getATGStatus = (connectorId: number): Status | undefined => {
   return props.chargingStation.automaticTransactionGenerator
     ?.automaticTransactionGeneratorStatuses?.[connectorId - 1]
 }
-function getInfo(): ChargingStationInfo {
-  return props.chargingStation.stationInfo
-}
-function getHashId(): string {
-  return getInfo().hashId
-}
-function getId(): string {
-  return getInfo().chargingStationId ?? 'Ø'
-}
-function getModel(): string {
-  return getInfo().chargePointModel
-}
-function getVendor(): string {
-  return getInfo().chargePointVendor
-}
-function getFirmwareVersion(): string {
-  return getInfo().firmwareVersion ?? 'Ø'
-}
-function getSupervisionUrl(): string {
+const getSupervisionUrl = (): string => {
   const supervisionUrl = new URL(props.chargingStation.supervisionUrl)
   return `${supervisionUrl.protocol}//${supervisionUrl.host.split('.').join('.\u200b')}`
 }
-function getWsState(): string {
+const getWSState = (): string => {
   switch (props.chargingStation?.wsState) {
     case WebSocket.CONNECTING:
       return 'Connecting'
@@ -115,77 +112,98 @@ function getWsState(): string {
       return 'Ø'
   }
 }
-function getRegistrationStatus(): string {
-  return props.chargingStation?.bootNotificationResponse?.status ?? 'Ø'
-}
-function getConnectors(): ConnectorStatus[] {
-  if (Array.isArray(props.chargingStation.evses) && props.chargingStation.evses.length > 0) {
-    const connectorsStatus: ConnectorStatus[] = []
-    for (const [evseId, evseStatus] of props.chargingStation.evses.entries()) {
-      if (evseId > 0 && Array.isArray(evseStatus.connectors) && evseStatus.connectors.length > 0) {
-        for (const connectorStatus of evseStatus.connectors) {
-          connectorsStatus.push(connectorStatus)
-        }
-      }
-    }
-    return connectorsStatus
-  }
-  return props.chargingStation.connectors?.slice(1)
-}
 
-const UIClient = getCurrentInstance()?.appContext.config.globalProperties.$UIClient
+const uiClient = getCurrentInstance()?.appContext.config.globalProperties.$uiClient
 
-function startChargingStation(): void {
-  UIClient.startChargingStation(getHashId())
-}
-function stopChargingStation(): void {
-  UIClient.stopChargingStation(getHashId())
-}
-function openConnection(): void {
-  UIClient.openConnection(getHashId())
-}
-function closeConnection(): void {
-  UIClient.closeConnection(getHashId())
+const $toast = useToast()
+
+const startChargingStation = (): void => {
+  uiClient
+    .startChargingStation(props.chargingStation.stationInfo.hashId)
+    .then(() => {
+      $toast.success('Charging station successfully started')
+    })
+    .catch((error: Error) => {
+      $toast.error('Error at starting charging station')
+      console.error('Error at starting charging station', error)
+    })
+}
+const stopChargingStation = (): void => {
+  uiClient
+    .stopChargingStation(props.chargingStation.stationInfo.hashId)
+    .then(() => {
+      $toast.success('Charging station successfully stopped')
+    })
+    .catch((error: Error) => {
+      $toast.error('Error at stopping charging station')
+      console.error('Error at stopping charging station', error)
+    })
+}
+const openConnection = (): void => {
+  uiClient
+    .openConnection(props.chargingStation.stationInfo.hashId)
+    .then(() => {
+      $toast.success('Connection successfully opened')
+    })
+    .catch((error: Error) => {
+      $toast.error('Error at opening connection')
+      console.error('Error at opening connection', error)
+    })
+}
+const closeConnection = (): void => {
+  uiClient
+    .closeConnection(props.chargingStation.stationInfo.hashId)
+    .then(() => {
+      $toast.success('Connection successfully closed')
+    })
+    .catch((error: Error) => {
+      $toast.error('Error at closing connection')
+      console.error('Error at closing connection', error)
+    })
+}
+const deleteChargingStation = (): void => {
+  uiClient
+    .deleteChargingStation(props.chargingStation.stationInfo.hashId)
+    .then(() => {
+      $toast.success('Charging station successfully deleted')
+    })
+    .catch((error: Error) => {
+      $toast.error('Error at deleting charging station')
+      console.error('Error at deleting charging station', error)
+    })
 }
-// function showTagModal(): void {
-//   state.isTagModalVisible = true
-// }
-// function hideTagModal(): void {
-//   state.isTagModalVisible = false
-// }
 </script>
 
 <style>
 #connectors-table {
   display: flex;
-  background-color: white;
   flex-direction: column;
+  background-color: white;
   overflow: auto hidden;
   border-collapse: collapse;
   empty-cells: show;
 }
 
-#connectors-table__head,
 #connectors-table__body {
   display: flex;
   flex-direction: column;
 }
 
 .connectors-table__row {
-  min-height: 4rem;
   display: flex;
+  flex-direction: row;
   justify-content: center;
   align-items: center;
 }
 
-#connectors-table__head .connectors-table__row {
-  background-color: lightgrey;
-}
-
 .connectors-table__row:nth-of-type(even) {
   background-color: whitesmoke;
 }
 
+#connectors-table__head .connectors-table__row {
+  background-color: lightgrey;
+}
+
 .connectors-table__column {
   width: calc(100% / 5);
   text-align: center;