fix(ui): close add charging stations action at UI server switch
[e-mobility-charging-stations-simulator.git] / ui / web / src / views / ChargingStationsView.vue
CommitLineData
32de5a57 1<template>
ca1e5439 2 <Container id="charging-stations-container">
47521384
JB
3 <Container
4 v-show="Array.isArray(uiServerConfigurations) && uiServerConfigurations.length > 1"
5 id="ui-server-container"
6 >
0344ad2b 7 <select
0344ad2b
JB
8 id="ui-server-selector"
9 v-model="state.uiServerIndex"
10 @change="
11 () => {
916fe456
JB
12 if (
13 getFromLocalStorage<number>('uiServerConfigurationIndex', 0) !== state.uiServerIndex
14 ) {
15 app?.appContext.config.globalProperties.$uiClient.setConfiguration(
16 app?.appContext.config.globalProperties.$configuration.uiServer[state.uiServerIndex]
17 )
18 initializeWSEventListeners()
19 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
20 'open',
21 () => {
22 setToLocalStorage<number>('uiServerConfigurationIndex', state.uiServerIndex)
18c7fecb 23 delete app?.appContext.config.globalProperties.$templates
de3e2a49
JB
24 $router.currentRoute.value.name === 'add-charging-stations' &&
25 $router.push({ name: 'charging-stations' })
916fe456
JB
26 },
27 { once: true }
28 )
29 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
30 'error',
31 () => {
32 state.uiServerIndex = getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
33 app?.appContext.config.globalProperties.$uiClient.setConfiguration(
34 app?.appContext.config.globalProperties.$configuration.uiServer[
35 getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
36 ]
37 )
38 initializeWSEventListeners()
39 },
40 { once: true }
41 )
0344ad2b
JB
42 }
43 }
44 "
45 >
46 <option
47 v-for="uiServerConfiguration in uiServerConfigurations"
48 :value="uiServerConfiguration.index"
49 >
258666f9 50 {{ uiServerConfiguration.configuration.name ?? uiServerConfiguration.configuration.host }}
0344ad2b
JB
51 </option>
52 </select>
53 </Container>
13c19b7b 54 <Container id="buttons-container">
14ee627a
JB
55 <Button @click="startSimulator()">Start Simulator</Button>
56 <Button @click="stopSimulator()">Stop Simulator</Button>
57 <Button @click="$router.push({ name: 'add-charging-stations' })">
1eb5f592
JB
58 Add Charging Stations
59 </Button>
b9d447d2
JB
60 <ReloadButton
61 id="reload-button"
a4868fd7 62 :loading="state.loading"
86545028 63 @click="loadChargingStations(() => (state.renderChargingStationsList = randomUUID()))"
b9d447d2 64 />
13c19b7b 65 </Container>
5c0e9352 66 <CSTable
7378b34a 67 v-show="
5c0e9352
JB
68 Array.isArray(app?.appContext.config.globalProperties.$chargingStations) &&
69 app?.appContext.config.globalProperties.$chargingStations.length > 0
70 "
86545028 71 :key="state.renderChargingStationsList"
5c0e9352
JB
72 :charging-stations="app?.appContext.config.globalProperties.$chargingStations"
73 />
32de5a57
LM
74 </Container>
75</template>
76
77<script setup lang="ts">
916fe456 78import { getCurrentInstance, onMounted, reactive } from 'vue'
cea23fa0 79import { useToast } from 'vue-toast-notification'
66a7748d 80import CSTable from '@/components/charging-stations/CSTable.vue'
0344ad2b 81import type { ResponsePayload, UIServerConfigurationSection } from '@/types'
66a7748d
JB
82import Container from '@/components/Container.vue'
83import ReloadButton from '@/components/buttons/ReloadButton.vue'
13c19b7b 84import Button from '@/components/buttons/Button.vue'
0344ad2b 85import { getFromLocalStorage, setToLocalStorage } from '@/composables'
32de5a57 86
a4868fd7
JB
87const randomUUID = (): `${string}-${string}-${string}-${string}-${string}` => {
88 return crypto.randomUUID()
89}
90
916fe456
JB
91const app = getCurrentInstance()
92
93const initializeWSEventListeners = () => {
94 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('open', () => {
95 loadChargingStations(() => (state.renderChargingStationsList = randomUUID()))
96 })
97 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('error', () => {
98 app.appContext.config.globalProperties.$chargingStations = []
99 state.renderChargingStationsList = randomUUID()
100 })
101 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('close', () => {
102 app.appContext.config.globalProperties.$chargingStations = []
103 state.renderChargingStationsList = randomUUID()
104 })
105}
106
107onMounted(() => {
108 initializeWSEventListeners()
109})
110
57c0ba05 111const state = reactive({
86545028 112 renderChargingStationsList: randomUUID(),
a4868fd7 113 loading: false,
0344ad2b 114 uiServerIndex: getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
66a7748d 115})
32de5a57 116
57c0ba05 117const uiClient = app?.appContext.config.globalProperties.$uiClient
0344ad2b
JB
118const uiServerConfigurations: { configuration: UIServerConfigurationSection; index: number }[] =
119 app?.appContext.config.globalProperties.$configuration.uiServer.map(
120 (configuration: UIServerConfigurationSection, index: number) => ({
121 configuration,
122 index
123 })
124 )
57c0ba05 125
cea23fa0
JB
126const $toast = useToast()
127
86545028 128const loadChargingStations = (renderCallback?: () => void): void => {
a4868fd7
JB
129 if (state.loading === false) {
130 state.loading = true
57c0ba05
JB
131 uiClient
132 .listChargingStations()
133 .then((response: ResponsePayload) => {
134 if (app != null) {
135 app.appContext.config.globalProperties.$chargingStations = response.chargingStations
136 }
137 })
138 .catch((error: Error) => {
916fe456
JB
139 if (app != null) {
140 app.appContext.config.globalProperties.$chargingStations = []
141 }
cea23fa0 142 $toast.error('Error at fetching charging stations')
57c0ba05
JB
143 console.error('Error at fetching charging stations:', error)
144 })
145 .finally(() => {
86545028
JB
146 if (renderCallback != null) {
147 renderCallback()
b9d447d2 148 }
a4868fd7 149 state.loading = false
57c0ba05 150 })
2113b3c6 151 }
32de5a57 152}
5a010bf0 153
fa5d129a 154const startSimulator = (): void => {
cea23fa0
JB
155 uiClient
156 .startSimulator()
157 .then(() => {
158 $toast.success('Simulator successfully started')
159 })
160 .catch((error: Error) => {
161 $toast.error('Error at starting simulator')
162 console.error('Error at starting simulator:', error)
163 })
5a010bf0 164}
fa5d129a 165const stopSimulator = (): void => {
cea23fa0
JB
166 uiClient
167 .stopSimulator()
168 .then(() => {
5c0e9352
JB
169 if (app != null) {
170 app.appContext.config.globalProperties.$chargingStations = []
171 }
cea23fa0
JB
172 $toast.success('Simulator successfully stopped')
173 })
174 .catch((error: Error) => {
175 $toast.error('Error at stopping simulator')
176 console.error('Error at stopping simulator:', error)
177 })
5a010bf0 178}
32de5a57
LM
179</script>
180
181<style>
ca1e5439 182#charging-stations-container {
1d41bc6b 183 height: fit-content;
32de5a57 184 width: 100%;
5a010bf0 185 display: flex;
32de5a57 186 flex-direction: column;
5a010bf0
JB
187}
188
0344ad2b
JB
189#ui-server-container {
190 display: flex;
191 flex-direction: row;
192}
193
194#ui-server-selector {
195 width: 100%;
196 text-align: center;
197}
198
13c19b7b
JB
199#buttons-container {
200 display: flex;
201 flex-direction: row;
202}
203
14ee627a
JB
204#action-button {
205 flex: none;
878855a2
JB
206}
207
32de5a57 208#reload-button {
5a010bf0 209 flex: auto;
32de5a57 210 color: white;
9dc8b66f 211 background-color: blue;
13c19b7b 212 font-size: 1.5rem;
32de5a57 213 font-weight: bold;
aee67dee
JB
214 align-items: center;
215 justify-content: center;
32de5a57
LM
216}
217
218#reload-button:hover {
9dc8b66f 219 background-color: rgb(0, 0, 225);
32de5a57
LM
220}
221
222#reload-button:active {
9dc8b66f 223 background-color: red;
32de5a57 224}
229d8c34
JB
225
226#action {
227 color: white;
228 background-color: black;
229 padding: 1%;
230}
32de5a57 231</style>