<template>
- <Container id="charging-stations">
+ <Container id="charging-stations-container">
<Container id="buttons-container">
- <Button id="simulator-button" @click="startSimulator()">Start Simulator</Button>
- <Button id="simulator-button" @click="stopSimulator()">Stop Simulator</Button>
- <ReloadButton id="reload-button" :loading="state.isLoading" @click="load()" />
- </Container>
- <Container id="inputs-container">
- <input
- id="idtag-field"
- v-model="state.idTag"
- type="text"
- name="idtag-field"
- placeholder="RFID tag"
+ <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
+ >
+ <ReloadButton
+ id="reload-button"
+ :loading="state.isLoading"
+ @click="loadChargingStations(() => $router.go(0))"
/>
</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 { getCurrentInstance, onMounted, reactive } from 'vue'
+import { getCurrentInstance, reactive } from 'vue'
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 Button from '@/components/buttons/Button.vue'
-const UIClient = getCurrentInstance()?.appContext.config.globalProperties.$UIClient
-
-onMounted(() => {
- UIClient.registerWSonOpenListener(load)
+const state = reactive({
+ isLoading: false
})
-type State = {
- isLoading: boolean
- chargingStations: ChargingStationData[]
- idTag: string
-}
-
-const state: State = reactive({
- isLoading: false,
- chargingStations: [],
- idTag: ''
-})
+const app = getCurrentInstance()
+const uiClient = app?.appContext.config.globalProperties.$uiClient
-async function load(): Promise<void> {
+function loadChargingStations(reloadCallback?: () => void): void {
if (state.isLoading === false) {
state.isLoading = true
- state.chargingStations = (await UIClient.listChargingStations())
- .chargingStations as ChargingStationData[]
- state.isLoading = false
+ uiClient
+ .listChargingStations()
+ .then((response: ResponsePayload) => {
+ if (app != null) {
+ app.appContext.config.globalProperties.$chargingStations = response.chargingStations
+ }
+ })
+ .catch((error: Error) => {
+ // TODO: add code for UI notifications or other error handling logic
+ console.error('Error at fetching charging stations:', error)
+ })
+ .finally(() => {
+ if (reloadCallback != null) {
+ reloadCallback()
+ }
+ state.isLoading = false
+ })
}
}
function startSimulator(): void {
- UIClient.startSimulator()
+ uiClient.startSimulator()
}
function stopSimulator(): void {
- UIClient.stopSimulator()
+ uiClient.stopSimulator()
}
</script>
<style>
-#charging-stations {
+#charging-stations-container {
height: fit-content;
width: 100%;
display: flex;
flex-direction: row;
}
-#inputs-container {
- display: flex;
- flex-direction: row;
+#button {
+ flex: auto;
}
#reload-button {
background-color: blue;
font-size: 1.5rem;
font-weight: bold;
- text-align: center;
+ align-items: center;
+ justify-content: center;
}
#reload-button:hover {
#reload-button:active {
background-color: red;
}
-
-#simulator-button {
- flex: auto;
-}
-
-#idtag-field {
- flex: auto;
- font-size: 1.5rem;
- text-align: center;
-}
</style>