refactor: cleanup default params in error handlers
[e-mobility-charging-stations-simulator.git] / ui / web / src / components / actions / AddChargingStations.vue
index 7a2f8a59ef86cc8c727a5f587c7cbd5e55558557..a95cba2b07a9afdf72cf03a2be97b81def9db712 100644 (file)
@@ -1,8 +1,13 @@
 <template>
-  <h2>Action Add Charging Stations</h2>
+  <h1 id="action">Add Charging Stations</h1>
   <p>Template:</p>
-  <select v-if="state.ready" v-model="state.template">
-    <option v-for="template in app?.appContext.config.globalProperties.$templates">
+  <select :key="state.renderTemplates" v-model="state.template">
+    <option disabled value="">Please select a template</option>
+    <option
+      v-for="template in $templates.value"
+      v-show="Array.isArray($templates.value) && $templates.value.length > 0"
+      :key="template"
+    >
       {{ template }}
     </option>
   </select>
   <input
     id="number-of-stations"
     v-model="state.numberOfStations"
-    type="text"
+    type="number"
+    min="1"
     name="number-of-stations"
     placeholder="number of stations"
   />
+  <p>Template options overrides:</p>
+  <ul id="template-options">
+    <li>
+      Supervision url:
+      <input
+        id="supervision-url"
+        v-model.trim="state.supervisionUrl"
+        type="url"
+        name="supervision-url"
+        placeholder="wss://"
+      />
+    </li>
+    <li>
+      Auto start:
+      <input v-model="state.autoStart" type="checkbox" true-value="true" false-value="false" />
+    </li>
+    <li>
+      Persistent configuration:
+      <input
+        v-model="state.persistentConfiguration"
+        type="checkbox"
+        true-value="true"
+        false-value="false"
+      />
+    </li>
+    <li>
+      OCPP strict compliance:
+      <input
+        v-model="state.ocppStrictCompliance"
+        type="checkbox"
+        true-value="true"
+        false-value="false"
+      />
+    </li>
+    <li>
+      Performance statistics:
+      <input
+        v-model="state.enableStatistics"
+        type="checkbox"
+        true-value="true"
+        false-value="false"
+      />
+    </li>
+  </ul>
   <br />
   <Button
+    id="action-button"
     @click="
       () => {
-        uiClient
-          .addChargingStations(state.template, state.numberOfStations)
+        $uiClient
+          .addChargingStations(state.template, state.numberOfStations, {
+            supervisionUrls: state.supervisionUrl.length > 0 ? state.supervisionUrl : undefined,
+            autoStart: convertToBoolean(state.autoStart),
+            persistentConfiguration: convertToBoolean(state.persistentConfiguration),
+            ocppStrictCompliance: convertToBoolean(state.ocppStrictCompliance),
+            enableStatistics: convertToBoolean(state.enableStatistics)
+          })
           .then(() => {
             $toast.success('Charging stations successfully added')
           })
           })
       }
     "
-    >Add Charging Stations</Button
   >
-  <Button @click="$router.push({ name: 'charging-stations' })">Cancel</Button>
+    Add Charging Stations
+  </Button>
 </template>
 
 <script setup lang="ts">
-import { getCurrentInstance, onMounted, reactive } from 'vue'
-import { useToast } from 'vue-toast-notification'
+import { getCurrentInstance, ref, watch } from 'vue'
+
 import Button from '@/components/buttons/Button.vue'
-import type { ResponsePayload } from '@/types'
+import { convertToBoolean, randomUUID } from '@/composables'
 
-const state = reactive({
-  ready: false,
+const state = ref<{
+  renderTemplates: `${string}-${string}-${string}-${string}-${string}`
+  template: string
+  numberOfStations: number
+  supervisionUrl: string
+  autoStart: boolean
+  persistentConfiguration: boolean
+  ocppStrictCompliance: boolean
+  enableStatistics: boolean
+}>({
+  renderTemplates: randomUUID(),
   template: '',
-  numberOfStations: 1
+  numberOfStations: 1,
+  supervisionUrl: '',
+  autoStart: false,
+  persistentConfiguration: true,
+  ocppStrictCompliance: true,
+  enableStatistics: false
 })
 
-const app = getCurrentInstance()
-const uiClient = app?.appContext.config.globalProperties.$uiClient
-
-const $toast = useToast()
-
-onMounted(() => {
-  uiClient
-    .listTemplates()
-    .then((response: ResponsePayload) => {
-      if (app != null && app.appContext.config.globalProperties.$templates == null) {
-        app.appContext.config.globalProperties.$templates = response.templates
-      }
-    })
-    .catch((error: Error) => {
-      $toast.error('Error at fetching charging station templates')
-      console.error('Error at fetching charging station templates:', error)
-    })
-    .finally(() => {
-      state.ready = true
-    })
+watch(getCurrentInstance()!.appContext.config.globalProperties.$templates, () => {
+  state.value.renderTemplates = randomUUID()
 })
 </script>
 
 <style>
 #number-of-stations {
+  width: 15%;
   text-align: center;
 }
+
+#supervision-url {
+  width: 90%;
+  text-align: left;
+}
+
+#template-options {
+  list-style: circle inside;
+  text-align: left;
+}
 </style>