fix: clear UI server cache at simulator stop
[e-mobility-charging-stations-simulator.git] / ui / web / src / views / ChargingStationsView.vue
CommitLineData
32de5a57 1<template>
ca1e5439 2 <Container id="charging-stations-container">
13c19b7b 3 <Container id="buttons-container">
14ee627a
JB
4 <Button @click="startSimulator()">Start Simulator</Button>
5 <Button @click="stopSimulator()">Stop Simulator</Button>
6 <Button @click="$router.push({ name: 'add-charging-stations' })">
1eb5f592
JB
7 Add Charging Stations
8 </Button>
b9d447d2
JB
9 <ReloadButton
10 id="reload-button"
11 :loading="state.isLoading"
12 @click="loadChargingStations(() => $router.go(0))"
13 />
13c19b7b 14 </Container>
5c0e9352
JB
15 <CSTable
16 v-if="
17 Array.isArray(app?.appContext.config.globalProperties.$chargingStations) &&
18 app?.appContext.config.globalProperties.$chargingStations.length > 0
19 "
20 :charging-stations="app?.appContext.config.globalProperties.$chargingStations"
21 />
32de5a57
LM
22 </Container>
23</template>
24
25<script setup lang="ts">
57c0ba05 26import { getCurrentInstance, reactive } from 'vue'
cea23fa0 27import { useToast } from 'vue-toast-notification'
66a7748d 28import CSTable from '@/components/charging-stations/CSTable.vue'
57c0ba05 29import type { ResponsePayload } from '@/types'
66a7748d
JB
30import Container from '@/components/Container.vue'
31import ReloadButton from '@/components/buttons/ReloadButton.vue'
13c19b7b 32import Button from '@/components/buttons/Button.vue'
32de5a57 33
57c0ba05 34const state = reactive({
c317ae3e 35 isLoading: false
66a7748d 36})
32de5a57 37
57c0ba05
JB
38const app = getCurrentInstance()
39const uiClient = app?.appContext.config.globalProperties.$uiClient
40
cea23fa0
JB
41const $toast = useToast()
42
fa5d129a 43const loadChargingStations = (reloadCallback?: () => void): void => {
2113b3c6
JB
44 if (state.isLoading === false) {
45 state.isLoading = true
57c0ba05
JB
46 uiClient
47 .listChargingStations()
48 .then((response: ResponsePayload) => {
49 if (app != null) {
50 app.appContext.config.globalProperties.$chargingStations = response.chargingStations
51 }
52 })
53 .catch((error: Error) => {
cea23fa0 54 $toast.error('Error at fetching charging stations')
57c0ba05
JB
55 console.error('Error at fetching charging stations:', error)
56 })
57 .finally(() => {
b9d447d2
JB
58 if (reloadCallback != null) {
59 reloadCallback()
60 }
57c0ba05
JB
61 state.isLoading = false
62 })
2113b3c6 63 }
32de5a57 64}
5a010bf0 65
fa5d129a 66const startSimulator = (): void => {
cea23fa0
JB
67 uiClient
68 .startSimulator()
69 .then(() => {
70 $toast.success('Simulator successfully started')
71 })
72 .catch((error: Error) => {
73 $toast.error('Error at starting simulator')
74 console.error('Error at starting simulator:', error)
75 })
5a010bf0 76}
fa5d129a 77const stopSimulator = (): void => {
cea23fa0
JB
78 uiClient
79 .stopSimulator()
80 .then(() => {
5c0e9352
JB
81 if (app != null) {
82 app.appContext.config.globalProperties.$chargingStations = []
83 }
cea23fa0
JB
84 $toast.success('Simulator successfully stopped')
85 })
86 .catch((error: Error) => {
87 $toast.error('Error at stopping simulator')
88 console.error('Error at stopping simulator:', error)
89 })
5a010bf0 90}
32de5a57
LM
91</script>
92
93<style>
ca1e5439 94#charging-stations-container {
1d41bc6b 95 height: fit-content;
32de5a57 96 width: 100%;
5c0e9352 97 position: absolute;
5a010bf0 98 display: flex;
32de5a57 99 flex-direction: column;
5a010bf0
JB
100}
101
13c19b7b
JB
102#buttons-container {
103 display: flex;
104 flex-direction: row;
105}
106
14ee627a
JB
107#action-button {
108 flex: none;
878855a2
JB
109}
110
32de5a57 111#reload-button {
5a010bf0 112 flex: auto;
32de5a57 113 color: white;
9dc8b66f 114 background-color: blue;
13c19b7b 115 font-size: 1.5rem;
32de5a57 116 font-weight: bold;
aee67dee
JB
117 align-items: center;
118 justify-content: center;
32de5a57
LM
119}
120
121#reload-button:hover {
9dc8b66f 122 background-color: rgb(0, 0, 225);
32de5a57
LM
123}
124
125#reload-button:active {
9dc8b66f 126 background-color: red;
32de5a57 127}
229d8c34
JB
128
129#action {
130 color: white;
131 background-color: black;
132 padding: 1%;
133}
32de5a57 134</style>