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