mod edit/create same view

This commit is contained in:
Josef Seiringer 2025-10-29 06:48:52 +01:00
parent 57f9acfcdb
commit dd5f928911
3 changed files with 44 additions and 7 deletions

View File

@ -1,6 +1,8 @@
<main class="login-container">
<div class="glass-card create-card">
<h2 class="title">Neuer Kantinen-Eintrag</h2>
<h2 class="title">
{{ isEdit ? "Eintrag bearbeiten" : "Neuer Kantinen-Eintrag" }}
</h2>
<div *ngIf="errorMessage" class="status error">{{ errorMessage }}</div>
@ -34,7 +36,15 @@
Abbrechen
</button>
<button type="submit" class="btn primary" [disabled]="loading">
{{ loading ? "Speichere…" : "Speichern" }}
{{
loading
? isEdit
? "Aktualisiere…"
: "Speichere…"
: isEdit
? "Aktualisieren"
: "Speichern"
}}
</button>
</div>
</form>

View File

@ -1,7 +1,7 @@
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';
import { AppwriteService } from '../../services/appwrite.service';
@Component({
@ -15,6 +15,7 @@ export class CreateComponent {
constructor(
private appwrite: AppwriteService,
private router: Router,
private route: ActivatedRoute,
) {}
byeDate = '';
@ -22,6 +23,25 @@ export class CreateComponent {
belegName = '';
loading = false;
errorMessage: string | null = null;
documentId: string | null = null;
isEdit = false;
ngOnInit(): void {
const params = this.route.snapshot.queryParamMap;
const id = params.get('id');
const date = params.get('date');
const amount = params.get('amount');
const name = params.get('name');
if (id) {
this.documentId = id;
this.isEdit = true;
}
if (date) this.byeDate = date;
if (amount !== null) this.betrag = Number(amount);
if (name) this.belegName = name;
}
async save(): Promise<void> {
if (!this.byeDate || this.betrag === null || Number.isNaN(this.betrag)) {
@ -31,11 +51,17 @@ export class CreateComponent {
this.loading = true;
this.errorMessage = null;
try {
await this.appwrite.createDocument({
const payload = {
ByeDate: this.byeDate,
Betrag: this.betrag,
BelegName: this.belegName,
});
} as const;
if (this.isEdit && this.documentId) {
await this.appwrite.updateDocument(this.documentId, payload as any);
} else {
await this.appwrite.createDocument(payload as any);
}
this.router.navigate(['/list']);
} catch (e: any) {
console.error(e);

View File

@ -59,13 +59,14 @@ export class ListComponent implements OnInit {
return item.szDocumentId ?? index.toString();
}
// Navigation zu einer (noch zu erstellenden) Create-Ansicht
// Navigation zu einer Edit/Create-Ansicht
goToCreate(): void {
// Create Ansicht
this.router.navigate(['/create']);
}
onEdit(item: Kantin): void {
// TODO: Edit-Ansicht bauen. Bis dahin könnte Create mit Prefill genutzt werden.
// Edit-Ansicht
this.router.navigate(['/create'], {
queryParams: {
id: item.szDocumentId,