refactor(ui): refine action bar style
[e-mobility-charging-stations-simulator.git] / ui / web / src / views / ChargingStationsView.vue
index 707cce54c5ab93f67b716809da432dffcd21d2db..8df77d92429646b638ce576b7e2df4085b22f6db 100644 (file)
 <template>
-  <Container id="charging-stations">
-    <Button id="simulator-button" @click="startSimulator()">Start Simulator</Button>
-    <Button id="simulator-button" @click="stopSimulator()">Stop Simulator</Button>
-    <Container id="inputs-container">
-      <input
-        id="idtag-field"
-        v-model="state.idTag"
-        type="text"
-        name="idtag-field"
-        placeholder="RFID tag"
+  <Container id="charging-stations-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))"
       />
-      <ReloadButton id="reload-button" :loading="state.isLoading" @click="load()" />
     </Container>
-    <CSTable :charging-stations="state.chargingStations" :id-tag="state.idTag" />
+    <CSTable :charging-stations="app?.appContext.config.globalProperties.$chargingStations" />
   </Container>
 </template>
 
 <script setup lang="ts">
-import { onMounted, reactive } from 'vue'
+import { getCurrentInstance, reactive } from 'vue'
+import { useToast } from 'vue-toast-notification'
 import CSTable from '@/components/charging-stations/CSTable.vue'
-import type { ChargingStationData } from '@/types'
+import type { ResponsePayload } from '@/types'
 import Container from '@/components/Container.vue'
 import ReloadButton from '@/components/buttons/ReloadButton.vue'
-import { UIClient } from '@/composables/UIClient'
+import Button from '@/components/buttons/Button.vue'
 
-const UIClientInstance = UIClient.getInstance()
-
-onMounted(() => {
-  UIClientInstance.registerWSonOpenListener(load)
+const state = reactive({
+  isLoading: false
 })
 
-type State = {
-  isLoading: boolean
-  chargingStations: ChargingStationData[]
-  idTag: string
-}
+const app = getCurrentInstance()
+const uiClient = app?.appContext.config.globalProperties.$uiClient
 
-const state: State = reactive({
-  isLoading: false,
-  chargingStations: [],
-  idTag: '',
-})
+const $toast = useToast()
 
-async function load(): Promise<void> {
-  if (state.isLoading === true) return
-  state.isLoading = true
-  const listChargingStationsPayload = await UIClientInstance.listChargingStations()
-  state.chargingStations =
-    listChargingStationsPayload.chargingStations as unknown as ChargingStationData[]
-  state.isLoading = false
+const loadChargingStations = (reloadCallback?: () => void): void => {
+  if (state.isLoading === false) {
+    state.isLoading = true
+    uiClient
+      .listChargingStations()
+      .then((response: ResponsePayload) => {
+        if (app != null) {
+          app.appContext.config.globalProperties.$chargingStations = response.chargingStations
+        }
+      })
+      .catch((error: Error) => {
+        $toast.error('Error at fetching charging stations')
+        console.error('Error at fetching charging stations:', error)
+      })
+      .finally(() => {
+        if (reloadCallback != null) {
+          reloadCallback()
+        }
+        state.isLoading = false
+      })
+  }
 }
 
-function startSimulator(): void {
-  UIClientInstance.startSimulator()
+const startSimulator = (): void => {
+  uiClient
+    .startSimulator()
+    .then(() => {
+      $toast.success('Simulator successfully started')
+    })
+    .catch((error: Error) => {
+      $toast.error('Error at starting simulator')
+      console.error('Error at starting simulator:', error)
+    })
 }
-function stopSimulator(): void {
-  UIClientInstance.stopSimulator()
+const stopSimulator = (): void => {
+  uiClient
+    .stopSimulator()
+    .then(() => {
+      $toast.success('Simulator successfully stopped')
+    })
+    .catch((error: Error) => {
+      $toast.error('Error at stopping simulator')
+      console.error('Error at stopping simulator:', error)
+    })
 }
 </script>
 
 <style>
-#charging-stations {
-  height: 100%;
+#charging-stations-container {
+  height: fit-content;
   width: 100%;
-  padding: 30px;
-  background-color: rgb(233, 227, 227);
   display: flex;
   flex-direction: column;
-  gap: 0.5%;
 }
 
-#inputs-container {
+#buttons-container {
   display: flex;
-  justify-content: space-between;
+  flex-direction: row;
+}
+
+#action-button {
+  flex: none;
 }
 
 #reload-button {
   flex: auto;
-  padding: 5px 15px;
-  background-color: rgb(25, 118, 210);
-  border-radius: 5px;
   color: white;
-  font-size: 35px;
+  background-color: blue;
+  font-size: 1.5rem;
   font-weight: bold;
+  align-items: center;
+  justify-content: center;
 }
 
 #reload-button:hover {
-  background-color: rgb(10, 113, 195);
+  background-color: rgb(0, 0, 225);
 }
 
 #reload-button:active {
-  background-color: rgb(255, 113, 195);
+  background-color: red;
 }
 
-#simulator-button {
-  flex: auto;
-}
-
-#idtag-field {
-  flex: auto;
+#action {
+  color: white;
+  background-color: black;
+  padding: 1%;
 }
 </style>