refactor(ui): fix templates code formatting
[e-mobility-charging-stations-simulator.git] / ui / web / src / components / actions / AddChargingStations.vue
CommitLineData
7086aac2
JB
1<template>
2 <h2>Action Add Charging Stations</h2>
878855a2
JB
3 <p>Template:</p>
4 <select v-if="state.ready" v-model="state.template">
b221407f 5 <option disabled value="">Please select a template</option>
878855a2
JB
6 <option v-for="template in app?.appContext.config.globalProperties.$templates">
7 {{ template }}
8 </option>
9 </select>
10 <p>Number of stations:</p>
11 <input
12 id="number-of-stations"
13 v-model="state.numberOfStations"
d18fc1e3
JB
14 type="number"
15 min="1"
3a3ba0a2 16 name="number-of-stations"
878855a2
JB
17 placeholder="number of stations"
18 />
19 <br />
20 <Button
21 @click="
22 () => {
23 uiClient
24 .addChargingStations(state.template, state.numberOfStations)
cea23fa0
JB
25 .then(() => {
26 $toast.success('Charging stations successfully added')
27 })
878855a2 28 .catch((error: Error) => {
cea23fa0 29 $toast.error('Error at adding charging stations')
878855a2
JB
30 console.error('Error at adding charging stations:', error)
31 })
32 .finally(() => {
33 $router.push({ name: 'charging-stations' })
34 })
35 }
36 "
878855a2 37 >
1eb5f592
JB
38 Add Charging Stations
39 </Button>
878855a2 40 <Button @click="$router.push({ name: 'charging-stations' })">Cancel</Button>
7086aac2
JB
41</template>
42
43<script setup lang="ts">
878855a2 44import { getCurrentInstance, onMounted, reactive } from 'vue'
cea23fa0 45import { useToast } from 'vue-toast-notification'
878855a2 46import Button from '@/components/buttons/Button.vue'
3a3ba0a2 47import type { ResponsePayload } from '@/types'
878855a2
JB
48
49const state = reactive({
50 ready: false,
51 template: '',
52 numberOfStations: 1
53})
7086aac2
JB
54
55const app = getCurrentInstance()
56const uiClient = app?.appContext.config.globalProperties.$uiClient
57
cea23fa0
JB
58const $toast = useToast()
59
7086aac2 60onMounted(() => {
b9d447d2
JB
61 uiClient
62 .listTemplates()
63 .then((response: ResponsePayload) => {
64 if (app != null && app.appContext.config.globalProperties.$templates == null) {
65 app.appContext.config.globalProperties.$templates = response.templates
66 }
67 })
68 .catch((error: Error) => {
cea23fa0 69 $toast.error('Error at fetching charging station templates')
b9d447d2
JB
70 console.error('Error at fetching charging station templates:', error)
71 })
878855a2
JB
72 .finally(() => {
73 state.ready = true
74 })
7086aac2
JB
75})
76</script>
77
878855a2
JB
78<style>
79#number-of-stations {
d18fc1e3 80 width: 15%;
878855a2
JB
81 text-align: center;
82}
83</style>