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