refactor(ui): cleanup props usage
[e-mobility-charging-stations-simulator.git] / ui / web / src / components / actions / AddChargingStations.vue
1 <template>
2 <h2>Action Add Charging Stations</h2>
3 <p>Template:</p>
4 <select v-if="state.ready" v-model="state.template">
5 <option v-for="template in app?.appContext.config.globalProperties.$templates">
6 {{ template }}
7 </option>
8 </select>
9 <p>Number of stations:</p>
10 <input
11 id="number-of-stations"
12 v-model="state.numberOfStations"
13 type="text"
14 name="number-of-stations"
15 placeholder="number of stations"
16 />
17 <br />
18 <Button
19 @click="
20 () => {
21 uiClient
22 .addChargingStations(state.template, state.numberOfStations)
23 .catch((error: Error) => {
24 // TODO: add code for UI notifications or other error handling logic
25 console.error('Error at adding charging stations:', error)
26 })
27 .finally(() => {
28 $router.push({ name: 'charging-stations' })
29 })
30 }
31 "
32 >Add Charging Stations</Button
33 >
34 <Button @click="$router.push({ name: 'charging-stations' })">Cancel</Button>
35 </template>
36
37 <script setup lang="ts">
38 import { getCurrentInstance, onMounted, reactive } from 'vue'
39 import Button from '@/components/buttons/Button.vue'
40 import type { ResponsePayload } from '@/types'
41
42 const state = reactive({
43 ready: false,
44 template: '',
45 numberOfStations: 1
46 })
47
48 const app = getCurrentInstance()
49 const uiClient = app?.appContext.config.globalProperties.$uiClient
50
51 onMounted(() => {
52 uiClient
53 .listTemplates()
54 .then((response: ResponsePayload) => {
55 if (app != null && app.appContext.config.globalProperties.$templates == null) {
56 app.appContext.config.globalProperties.$templates = response.templates
57 }
58 })
59 .catch((error: Error) => {
60 // TODO: add code for UI notifications or other error handling logic
61 console.error('Error at fetching charging station templates:', error)
62 })
63 .finally(() => {
64 state.ready = true
65 })
66 })
67 </script>
68
69 <style>
70 #number-of-stations {
71 text-align: center;
72 }
73 </style>