feat(ui): add action success/failure notifications
[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">
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"
3a3ba0a2 14 name="number-of-stations"
878855a2
JB
15 placeholder="number of stations"
16 />
17 <br />
18 <Button
19 @click="
20 () => {
21 uiClient
22 .addChargingStations(state.template, state.numberOfStations)
cea23fa0
JB
23 .then(() => {
24 $toast.success('Charging stations successfully added')
25 })
878855a2 26 .catch((error: Error) => {
cea23fa0 27 $toast.error('Error at adding charging stations')
878855a2
JB
28 console.error('Error at adding charging stations:', error)
29 })
30 .finally(() => {
31 $router.push({ name: 'charging-stations' })
32 })
33 }
34 "
35 >Add Charging Stations</Button
36 >
37 <Button @click="$router.push({ name: 'charging-stations' })">Cancel</Button>
7086aac2
JB
38</template>
39
40<script setup lang="ts">
878855a2 41import { getCurrentInstance, onMounted, reactive } from 'vue'
cea23fa0 42import { useToast } from 'vue-toast-notification'
878855a2 43import Button from '@/components/buttons/Button.vue'
3a3ba0a2 44import type { ResponsePayload } from '@/types'
878855a2
JB
45
46const state = reactive({
47 ready: false,
48 template: '',
49 numberOfStations: 1
50})
7086aac2
JB
51
52const app = getCurrentInstance()
53const uiClient = app?.appContext.config.globalProperties.$uiClient
54
cea23fa0
JB
55const $toast = useToast()
56
7086aac2 57onMounted(() => {
b9d447d2
JB
58 uiClient
59 .listTemplates()
60 .then((response: ResponsePayload) => {
61 if (app != null && app.appContext.config.globalProperties.$templates == null) {
62 app.appContext.config.globalProperties.$templates = response.templates
63 }
64 })
65 .catch((error: Error) => {
cea23fa0 66 $toast.error('Error at fetching charging station templates')
b9d447d2
JB
67 console.error('Error at fetching charging station templates:', error)
68 })
878855a2
JB
69 .finally(() => {
70 state.ready = true
71 })
7086aac2
JB
72})
73</script>
74
878855a2
JB
75<style>
76#number-of-stations {
77 text-align: center;
78}
79</style>