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