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