refactor: cleanup vue.js global properties usage
[e-mobility-charging-stations-simulator.git] / ui / web / src / views / ChargingStationsView.vue
CommitLineData
32de5a57 1<template>
ca1e5439 2 <Container id="charging-stations-container">
239bd875
JB
3 <Container id="buttons-container">
4 <Container
5 v-show="Array.isArray(uiServerConfigurations) && uiServerConfigurations.length > 1"
6 id="ui-server-container"
0344ad2b 7 >
239bd875
JB
8 <select
9 id="ui-server-selector"
10 v-model="state.uiServerIndex"
11 @change="
12 () => {
13 if (
14 getFromLocalStorage<number>('uiServerConfigurationIndex', 0) !== state.uiServerIndex
15 ) {
7e315430 16 $uiClient.setConfiguration($configuration.value.uiServer[state.uiServerIndex])
aa9b0a1e 17 registerWSEventListeners()
7e315430 18 $uiClient.registerWSEventListener(
239bd875
JB
19 'open',
20 () => {
21 setToLocalStorage<number>('uiServerConfigurationIndex', state.uiServerIndex)
22 clearToggleButtons()
23 $router.currentRoute.value.name !== 'charging-stations' &&
24 $router.push({ name: 'charging-stations' })
25 },
26 { once: true }
27 )
7e315430 28 $uiClient.registerWSEventListener(
239bd875
JB
29 'error',
30 () => {
31 state.uiServerIndex = getFromLocalStorage<number>(
32 'uiServerConfigurationIndex',
33 0
34 )
7e315430
JB
35 $uiClient.setConfiguration(
36 $configuration.value.uiServer[
239bd875
JB
37 getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
38 ]
39 )
aa9b0a1e 40 registerWSEventListeners()
239bd875
JB
41 },
42 { once: true }
43 )
44 }
45 }
46 "
0344ad2b 47 >
239bd875
JB
48 <option
49 v-for="uiServerConfiguration in uiServerConfigurations"
50 :value="uiServerConfiguration.index"
51 >
52 {{
53 uiServerConfiguration.configuration.name ?? uiServerConfiguration.configuration.host
54 }}
55 </option>
56 </select>
57 </Container>
240fa4da
JB
58 <ToggleButton
59 :id="'simulator'"
60 :key="state.renderSimulator"
61 :status="state.simulatorState?.started"
62 :on="() => startSimulator()"
63 :off="() => stopSimulator()"
ec7366ed 64 :class="simulatorButtonClass"
240fa4da 65 >
ec7366ed 66 {{ simulatorButtonMessage }}
240fa4da 67 </ToggleButton>
2610da71
JB
68 <ToggleButton
69 :id="'add-charging-stations'"
d64ea57b 70 :key="state.renderAddChargingStations"
2610da71
JB
71 :shared="true"
72 :on="
73 () => {
74 $router.push({ name: 'add-charging-stations' })
75 }
76 "
77 :off="
78 () => {
79 $router.push({ name: 'charging-stations' })
80 }
81 "
83468764
JB
82 @clicked="
83 () => {
84 state.renderChargingStations = randomUUID()
85 }
86 "
2610da71 87 >
1eb5f592 88 Add Charging Stations
2610da71 89 </ToggleButton>
b9d447d2
JB
90 <ReloadButton
91 id="reload-button"
26cf7d99
JB
92 :loading="state.gettingChargingStations"
93 @click="getChargingStations()"
b9d447d2 94 />
13c19b7b 95 </Container>
5c0e9352 96 <CSTable
7e315430 97 v-show="Array.isArray($chargingStations.value) && $chargingStations.value.length > 0"
3eea3ebc 98 :key="state.renderChargingStations"
7e315430 99 :charging-stations="$chargingStations.value"
83468764
JB
100 @need-refresh="
101 () => {
102 state.renderAddChargingStations = randomUUID()
103 state.renderChargingStations = randomUUID()
104 }
105 "
5c0e9352 106 />
32de5a57
LM
107 </Container>
108</template>
109
110<script setup lang="ts">
aa9b0a1e 111import { computed, getCurrentInstance, onMounted, onUnmounted, ref } from 'vue'
cea23fa0 112import { useToast } from 'vue-toast-notification'
66a7748d 113import CSTable from '@/components/charging-stations/CSTable.vue'
e8237645 114import type { ResponsePayload, SimulatorState, UIServerConfigurationSection } from '@/types'
66a7748d
JB
115import Container from '@/components/Container.vue'
116import ReloadButton from '@/components/buttons/ReloadButton.vue'
2610da71 117import {
b767fda7 118 deleteFromLocalStorage,
2610da71
JB
119 getFromLocalStorage,
120 getLocalStorage,
121 randomUUID,
2610da71
JB
122 setToLocalStorage
123} from '@/composables'
124import ToggleButton from '@/components/buttons/ToggleButton.vue'
a4868fd7 125
240fa4da
JB
126const state = ref<{
127 renderSimulator: `${string}-${string}-${string}-${string}-${string}`
128 renderAddChargingStations: `${string}-${string}-${string}-${string}-${string}`
129 renderChargingStations: `${string}-${string}-${string}-${string}-${string}`
26cf7d99
JB
130 gettingSimulatorState: boolean
131 gettingTemplates: boolean
132 gettingChargingStations: boolean
e8237645 133 simulatorState?: SimulatorState
240fa4da
JB
134 uiServerIndex: number
135}>({
136 renderSimulator: randomUUID(),
137 renderAddChargingStations: randomUUID(),
138 renderChargingStations: randomUUID(),
26cf7d99
JB
139 gettingSimulatorState: false,
140 gettingTemplates: false,
141 gettingChargingStations: false,
240fa4da
JB
142 uiServerIndex: getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
143})
144
ec7366ed
JB
145const simulatorButtonClass = computed<string>(() =>
146 state.value.simulatorState?.started === true ? 'simulator-stop-button' : 'simulator-start-button'
147)
148const simulatorButtonMessage = computed<string>(
149 () =>
1fa3425a 150 `${state.value.simulatorState?.started === true ? 'Stop' : 'Start'} Simulator${state.value.simulatorState?.version != null ? ` (${state.value.simulatorState.version})` : ''}`
ec7366ed
JB
151)
152
d64ea57b
JB
153const clearToggleButtons = (): void => {
154 for (const key in getLocalStorage()) {
155 if (key.includes('toggle-button')) {
b767fda7 156 deleteFromLocalStorage(key)
d64ea57b
JB
157 }
158 }
26cf7d99
JB
159 state.value.renderChargingStations = randomUUID()
160 state.value.renderAddChargingStations = randomUUID()
d64ea57b
JB
161}
162
97cd0ef3
JB
163const app = getCurrentInstance()
164
d64ea57b 165const clearChargingStations = (): void => {
26cf7d99 166 if (app != null) {
7e315430 167 app.appContext.config.globalProperties.$chargingStations.value = []
26cf7d99 168 }
3eea3ebc 169 state.value.renderChargingStations = randomUUID()
d64ea57b
JB
170}
171
240fa4da
JB
172const uiClient = app?.appContext.config.globalProperties.$uiClient
173
97cd0ef3
JB
174const $toast = useToast()
175
240fa4da 176const getSimulatorState = (): void => {
26cf7d99
JB
177 if (state.value.gettingSimulatorState === false) {
178 state.value.gettingSimulatorState = true
3b0c6e17 179 uiClient
26cf7d99 180 .simulatorState()
3b0c6e17 181 .then((response: ResponsePayload) => {
26cf7d99 182 state.value.simulatorState = response.state as SimulatorState
3b0c6e17
JB
183 })
184 .catch((error: Error) => {
26cf7d99
JB
185 $toast.error('Error at fetching simulator state')
186 console.error('Error at fetching simulator state:', error)
3b0c6e17 187 })
240fa4da 188 .finally(() => {
26cf7d99
JB
189 state.value.renderSimulator = randomUUID()
190 state.value.gettingSimulatorState = false
240fa4da 191 })
26cf7d99
JB
192 }
193}
194
97cd0ef3
JB
195const getTemplates = (): void => {
196 if (state.value.gettingTemplates === false) {
197 state.value.gettingTemplates = true
198 uiClient
199 .listTemplates()
200 .then((response: ResponsePayload) => {
201 if (app != null) {
7e315430 202 app.appContext.config.globalProperties.$templates.value = response.templates
97cd0ef3
JB
203 }
204 })
205 .catch((error: Error) => {
206 if (app != null) {
7e315430 207 app.appContext.config.globalProperties.$templates.value = []
97cd0ef3
JB
208 }
209 $toast.error('Error at fetching charging station templates')
210 console.error('Error at fetching charging station templates:', error)
211 })
212 .finally(() => {
213 state.value.gettingTemplates = false
214 })
215 }
916fe456
JB
216}
217
26cf7d99
JB
218const getChargingStations = (): void => {
219 if (state.value.gettingChargingStations === false) {
220 state.value.gettingChargingStations = true
57c0ba05
JB
221 uiClient
222 .listChargingStations()
223 .then((response: ResponsePayload) => {
224 if (app != null) {
7e315430 225 app.appContext.config.globalProperties.$chargingStations.value = response.chargingStations
57c0ba05
JB
226 }
227 })
228 .catch((error: Error) => {
916fe456 229 if (app != null) {
7e315430 230 app.appContext.config.globalProperties.$chargingStations.value = []
916fe456 231 }
cea23fa0 232 $toast.error('Error at fetching charging stations')
57c0ba05
JB
233 console.error('Error at fetching charging stations:', error)
234 })
235 .finally(() => {
26cf7d99
JB
236 state.value.renderChargingStations = randomUUID()
237 state.value.gettingChargingStations = false
57c0ba05 238 })
2113b3c6 239 }
32de5a57 240}
5a010bf0 241
aa9b0a1e
JB
242const getData = (): void => {
243 getSimulatorState()
244 getTemplates()
245 getChargingStations()
246}
247
248const registerWSEventListeners = () => {
249 uiClient.registerWSEventListener('open', getData)
97cd0ef3
JB
250 uiClient.registerWSEventListener('error', clearChargingStations)
251 uiClient.registerWSEventListener('close', clearChargingStations)
252}
253
aa9b0a1e
JB
254const unregisterWSEventListeners = () => {
255 uiClient.unregisterWSEventListener('open', getData)
256 uiClient.unregisterWSEventListener('error', clearChargingStations)
257 uiClient.unregisterWSEventListener('close', clearChargingStations)
258}
259
97cd0ef3 260onMounted(() => {
aa9b0a1e
JB
261 registerWSEventListeners()
262})
263
264onUnmounted(() => {
265 unregisterWSEventListeners()
97cd0ef3
JB
266})
267
268const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] =
7e315430 269 app?.appContext.config.globalProperties.$configuration.value.uiServer.map(
97cd0ef3
JB
270 (configuration: UIServerConfigurationSection, index: number) => ({
271 index,
272 configuration
273 })
274 )
275
fa5d129a 276const startSimulator = (): void => {
cea23fa0
JB
277 uiClient
278 .startSimulator()
279 .then(() => {
280 $toast.success('Simulator successfully started')
281 })
282 .catch((error: Error) => {
283 $toast.error('Error at starting simulator')
284 console.error('Error at starting simulator:', error)
285 })
240fa4da
JB
286 .finally(() => {
287 getSimulatorState()
288 })
5a010bf0 289}
fa5d129a 290const stopSimulator = (): void => {
cea23fa0
JB
291 uiClient
292 .stopSimulator()
293 .then(() => {
5c0e9352 294 if (app != null) {
7e315430 295 app.appContext.config.globalProperties.$chargingStations.value = []
5c0e9352 296 }
cea23fa0
JB
297 $toast.success('Simulator successfully stopped')
298 })
299 .catch((error: Error) => {
300 $toast.error('Error at stopping simulator')
301 console.error('Error at stopping simulator:', error)
302 })
240fa4da
JB
303 .finally(() => {
304 getSimulatorState()
305 })
5a010bf0 306}
32de5a57
LM
307</script>
308
309<style>
ca1e5439 310#charging-stations-container {
1d41bc6b 311 height: fit-content;
32de5a57 312 width: 100%;
5a010bf0 313 display: flex;
32de5a57 314 flex-direction: column;
5a010bf0
JB
315}
316
0344ad2b
JB
317#ui-server-container {
318 display: flex;
239bd875 319 justify-content: center;
754d8199 320 border-style: outset;
0344ad2b
JB
321}
322
323#ui-server-selector {
324 width: 100%;
239bd875 325 background-color: rgb(239, 239, 239);
c6308554 326 font: small-caption;
0344ad2b
JB
327 text-align: center;
328}
329
239bd875 330#ui-server-selector:hover {
bad93db3 331 background-color: rgb(229, 229, 229);
239bd875
JB
332}
333
13c19b7b
JB
334#buttons-container {
335 display: flex;
336 flex-direction: row;
337}
338
6027002f 339.simulator-start-button {
3802683b
JB
340 color: ivory;
341 background-color: green;
342}
343
fec03138 344.simulator-start-button:hover {
fec03138
JB
345 background-color: rgb(0, 98, 0);
346}
347
6027002f 348.simulator-stop-button {
3802683b
JB
349 color: ivory;
350 background-color: red;
351}
352
fec03138
JB
353.simulator-stop-button:hover {
354 background-color: rgb(225, 0, 0);
355}
356
14ee627a
JB
357#action-button {
358 flex: none;
878855a2
JB
359}
360
32de5a57 361#reload-button {
3802683b 362 color: ivory;
9dc8b66f 363 background-color: blue;
c6308554 364 font-size: 2rem;
32de5a57
LM
365}
366
367#reload-button:hover {
9dc8b66f 368 background-color: rgb(0, 0, 225);
32de5a57
LM
369}
370
371#reload-button:active {
4b10e4f9 372 background-color: darkblue;
32de5a57 373}
229d8c34
JB
374
375#action {
916f0920 376 min-width: max-content;
3802683b 377 color: ivory;
229d8c34 378 background-color: black;
916f0920 379 padding: 0.8%;
229d8c34 380}
32de5a57 381</style>