Add Form Submit Functionality

This commit is contained in:
Samuel Lorch 2023-04-03 23:44:23 +02:00
parent 54460ca3d5
commit d7f2eb64f9
4 changed files with 47 additions and 12 deletions

View file

@ -15,14 +15,16 @@ const props = defineModel<{
}[], }[],
}[], }[],
modelValue: any, modelValue: any,
submit: (value: any) => boolean,
discard: () => void,
}>(); }>();
let { sections } = $(props); let { sections, submit, discard } = $(props);
</script> </script>
<template> <template>
<ValidationForm as="div" v-slot="{ values }" @submit="false"> <ValidationForm as="form" v-slot="{ values }" @submit="submit">
<template v-for="(section, index) in sections" :key="index"> <template v-for="(section, index) in sections" :key="index">
<h4 v-if="section.title">{{ section.title }}</h4> <h4 v-if="section.title">{{ section.title }}</h4>
<div class="section"> <div class="section">
@ -35,17 +37,39 @@ let { sections } = $(props);
</template> </template>
</div> </div>
</template> </template>
<div class="actions">
<div class="flex-grow"/>
<button>Submit</button>
<div class="space"/>
<button @click="discard">Discard</button>
<div class="flex-grow"/>
</div>
<p>{{ values }}</p> <p>{{ values }}</p>
</ValidationForm> </ValidationForm>
</template> </template>
<style scoped> <style scoped>
form {
display: flex;
flex-direction: column;
}
.section { .section {
display: grid; display: grid;
grid-template-columns: auto 1fr; grid-template-columns: auto 1fr;
padding: 0.5rem; padding: 0.5rem;
gap: 0.5rem; gap: 0.5rem;
} }
.actions {
flex-direction: row;
}
.space {
padding: 0.2rem;
}
.actions > button {
flex-grow: true;
}
h4, h4,
p { p {

View file

@ -1,7 +1,8 @@
export const editTypes: { [key: string]: { [key: string]: any } } = { export const editTypes: { [key: string]: { [key: string]: any } } = {
"firewall": { "firewall": {
name: "Firewall",
"forwardrules": { "forwardrules": {
title: "Forward Rule", name: "ForwardRule",
sections: [ sections: [
{ {
fields: [ fields: [
@ -15,8 +16,9 @@ export const editTypes: { [key: string]: { [key: string]: any } } = {
}, },
}, },
"network": { "network": {
name: "Network",
"interfaces": { "interfaces": {
title: "Interfaces", name: "Interface",
sections: [ sections: [
{ {
fields: [ fields: [

View file

@ -14,11 +14,11 @@ async function update() {
</script> </script>
<template> <template>
<div v-if="editTypes[subsystem][entity]"> <div v-if="editTypes[subsystem][entity]">
<PageHeader :title="'Edit ' + editTypes[subsystem][entity].title"> <PageHeader :title="'Edit ' + editTypes[subsystem][entity].name">
<button @click="update">Update</button> <button @click="update">Update</button>
<button @click="$router.go(-1)">Discard</button> <button @click="$router.go(-1)">Discard</button>
</PageHeader> </PageHeader>
<NiceForm class="scroll cl-secondary" :title="editTypes[subsystem][entity].title" :sections="editTypes[subsystem][entity].sections" v-model="data"/> <NiceForm class="scroll cl-secondary" :sections="editTypes[subsystem][entity].sections" v-model="data"/>
</div> </div>
<div v-else> <div v-else>
<PageHeader title="Error"/> <PageHeader title="Error"/>

View file

@ -1,23 +1,32 @@
<script setup lang="ts"> <script setup lang="ts">
import { editTypes } from "../../../../definitions"; import { editTypes } from "../../../../definitions";
import { apiCall } from "../../../../api";
import { useToast } from 'vue-toast-notification';
const $toast = useToast();
const props = $defineProps<{subsystem: string, entity: string}>(); const props = $defineProps<{subsystem: string, entity: string}>();
const { subsystem, entity } = $(props); const { subsystem, entity } = $(props);
let data = $ref({}); let data = $ref({});
async function create() { async function create(value: any) {
console.debug("value", value);
let res = await apiCall(editTypes[subsystem].name +".Create"+ editTypes[subsystem][entity].name, value);
if (res.Error === null) {
$toast.success("Created " + entity);
$router.go(-1)
} else {
console.debug("error", res);
}
} }
</script> </script>
<template> <template>
<div v-if="editTypes[subsystem][entity]"> <div v-if="editTypes[subsystem][entity]">
<PageHeader :title="'Edit ' + editTypes[subsystem][entity].title"> <PageHeader :title="'Create ' + editTypes[subsystem][entity].name">
<button @click="create">Create</button>
<button @click="$router.go(-1)">Discard</button>
</PageHeader> </PageHeader>
<NiceForm class="scroll cl-secondary" :title="editTypes[subsystem][entity].title" :sections="editTypes[subsystem][entity].sections" v-model="data"/> <NiceForm class="scroll cl-secondary" :submit="create" :discard="() => $router.go(-1)" :sections="editTypes[subsystem][entity].sections" v-model="data"/>
</div> </div>
<div v-else> <div v-else>
<PageHeader title="Error"/> <PageHeader title="Error"/>