allow for addition of Vorgaenge on Vorgang overview
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { getVorgaenge } from '$lib/server/vorgangService';
|
||||
import { createVorgang, getVorgaenge } from '$lib/server/vorgangService';
|
||||
import type { PageServerLoad } from '../../(token-based)/view/$types';
|
||||
import { error } from '@sveltejs/kit';
|
||||
import { error, fail } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async (event) => {
|
||||
if (!event.locals.user) {
|
||||
@@ -13,3 +13,23 @@ export const load: PageServerLoad = async (event) => {
|
||||
vorgangList
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
export const actions = {
|
||||
default: async ({ request }: { request: Request }) => {
|
||||
const data = await request.formData();
|
||||
const vorgangName: string | null = data.get('vorgang') as string;
|
||||
const vorgangPIN: string | null = data.get('pin') as string;
|
||||
|
||||
const err = {};
|
||||
|
||||
const token = createVorgang(vorgangName, vorgangPIN);
|
||||
if (!token) {
|
||||
err.message = "Der Vorgang konnte nicht angelegt werden"
|
||||
return fail(400, err)
|
||||
} else {
|
||||
// success
|
||||
return { token }
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -4,12 +4,62 @@
|
||||
import EmptyList from '$lib/components/EmptyList.svelte';
|
||||
import { API_ROUTES, ROUTE_NAMES } from '../../index.js';
|
||||
|
||||
let { data } = $props();
|
||||
let { data, form } = $props();
|
||||
|
||||
let vorgangList = data.vorgangList;
|
||||
|
||||
let isEmptyList = vorgangList.length === 0;
|
||||
|
||||
let vorgangName = $state('');
|
||||
let vorgangPIN = $state('');
|
||||
let errorMsg = $state('');
|
||||
|
||||
// reset input fields when submission successful
|
||||
$effect(() => {
|
||||
if (form?.token) {
|
||||
vorgangName = '';
|
||||
vorgangPIN = '';
|
||||
errorMsg = '';
|
||||
}
|
||||
});
|
||||
|
||||
async function submitVorgang(ev: Event) {
|
||||
const isValid = inputValid(vorgangName, vorgangPIN);
|
||||
if (!isValid) {
|
||||
ev.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
// continue form action on server
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for required fields
|
||||
* @param vorgangName
|
||||
* @param vorgangPIN
|
||||
* @returns {boolean} Indicates whether input is valid
|
||||
*/
|
||||
function inputValid(vorgangName, vorgangPIN) {
|
||||
if (!(vorgangName || vorgangPIN)) {
|
||||
errorMsg = 'Bitte beide Felder ausfüllen.';
|
||||
return false;
|
||||
} else if (!vorgangName) {
|
||||
errorMsg = 'Bitte einen Vorgangsnamen vergeben.';
|
||||
return false;
|
||||
} else if (!vorgangPIN) {
|
||||
errorMsg = 'Bitte einen Vorgangs-PIN eingeben.';
|
||||
return false;
|
||||
}
|
||||
|
||||
const existing = vorgangList.some((vorg) => vorg.vorgangName === vorgangName);
|
||||
if (existing) {
|
||||
errorMsg = 'Der Name existiert bereits.';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async function delete_item(ev: Event) {
|
||||
let delete_item = window.confirm('Bist du sicher?');
|
||||
|
||||
@@ -80,6 +130,64 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form class="flex flex-col items-center mt-10 space-y-4" method="POST">
|
||||
<div class="flex flex-col sm:flex-row sm:space-x-4 w-full max-w-lg">
|
||||
<div class="flex-1">
|
||||
<label for="vorgang" class="block text-sm font-medium leading-6 text-gray-900">
|
||||
<span class="flex"> Vorgangsname </span>
|
||||
</label>
|
||||
<div class="mt-2">
|
||||
<div
|
||||
class="flex rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-indigo-600"
|
||||
>
|
||||
<input
|
||||
required
|
||||
bind:value={vorgangName}
|
||||
type="text"
|
||||
name="vorgang"
|
||||
id="vorgang"
|
||||
class="block flex-1 border-0 bg-transparent py-1.5 pl-1 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex-1 mt-4 sm:mt-0">
|
||||
<label for="pin" class="block text-sm font-medium leading-6 text-gray-900">
|
||||
<span class="flex"> PIN </span>
|
||||
</label>
|
||||
<div class="mt-2">
|
||||
<div
|
||||
class="flex rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-indigo-600"
|
||||
>
|
||||
<input
|
||||
required
|
||||
type="password"
|
||||
bind:value={vorgangPIN}
|
||||
name="pin"
|
||||
id="pin"
|
||||
class="block flex-1 border-0 bg-transparent py-1.5 pl-1 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if errorMsg}
|
||||
<p>{errorMsg}</p>
|
||||
{/if}
|
||||
{#if form?.message}
|
||||
<p>{form.message}</p>
|
||||
{/if}
|
||||
<button
|
||||
type="submit"
|
||||
on:click={submitVorgang}
|
||||
class="mt-4 bg-indigo-600 text-white px-6 py-2 rounded hover:bg-indigo-700 transition"
|
||||
>
|
||||
Neuen Vorgang hinzufügen
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<style>
|
||||
ul {
|
||||
min-width: 24rem;
|
||||
|
||||
Reference in New Issue
Block a user