mod edit/create same view
This commit is contained in:
parent
57f9acfcdb
commit
dd5f928911
@ -1,6 +1,8 @@
|
|||||||
<main class="login-container">
|
<main class="login-container">
|
||||||
<div class="glass-card create-card">
|
<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>
|
<div *ngIf="errorMessage" class="status error">{{ errorMessage }}</div>
|
||||||
|
|
||||||
@ -34,7 +36,15 @@
|
|||||||
Abbrechen
|
Abbrechen
|
||||||
</button>
|
</button>
|
||||||
<button type="submit" class="btn primary" [disabled]="loading">
|
<button type="submit" class="btn primary" [disabled]="loading">
|
||||||
{{ loading ? "Speichere…" : "Speichern" }}
|
{{
|
||||||
|
loading
|
||||||
|
? isEdit
|
||||||
|
? "Aktualisiere…"
|
||||||
|
: "Speichere…"
|
||||||
|
: isEdit
|
||||||
|
? "Aktualisieren"
|
||||||
|
: "Speichern"
|
||||||
|
}}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { Component } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { FormsModule } from '@angular/forms';
|
import { FormsModule } from '@angular/forms';
|
||||||
import { Router } from '@angular/router';
|
import { ActivatedRoute, Router } from '@angular/router';
|
||||||
import { AppwriteService } from '../../services/appwrite.service';
|
import { AppwriteService } from '../../services/appwrite.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@ -15,6 +15,7 @@ export class CreateComponent {
|
|||||||
constructor(
|
constructor(
|
||||||
private appwrite: AppwriteService,
|
private appwrite: AppwriteService,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
|
private route: ActivatedRoute,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
byeDate = '';
|
byeDate = '';
|
||||||
@ -22,6 +23,25 @@ export class CreateComponent {
|
|||||||
belegName = '';
|
belegName = '';
|
||||||
loading = false;
|
loading = false;
|
||||||
errorMessage: string | null = null;
|
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> {
|
async save(): Promise<void> {
|
||||||
if (!this.byeDate || this.betrag === null || Number.isNaN(this.betrag)) {
|
if (!this.byeDate || this.betrag === null || Number.isNaN(this.betrag)) {
|
||||||
@ -31,11 +51,17 @@ export class CreateComponent {
|
|||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.errorMessage = null;
|
this.errorMessage = null;
|
||||||
try {
|
try {
|
||||||
await this.appwrite.createDocument({
|
const payload = {
|
||||||
ByeDate: this.byeDate,
|
ByeDate: this.byeDate,
|
||||||
Betrag: this.betrag,
|
Betrag: this.betrag,
|
||||||
BelegName: this.belegName,
|
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']);
|
this.router.navigate(['/list']);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
|||||||
@ -59,13 +59,14 @@ export class ListComponent implements OnInit {
|
|||||||
return item.szDocumentId ?? index.toString();
|
return item.szDocumentId ?? index.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Navigation zu einer (noch zu erstellenden) Create-Ansicht
|
// Navigation zu einer Edit/Create-Ansicht
|
||||||
goToCreate(): void {
|
goToCreate(): void {
|
||||||
|
// Create Ansicht
|
||||||
this.router.navigate(['/create']);
|
this.router.navigate(['/create']);
|
||||||
}
|
}
|
||||||
|
|
||||||
onEdit(item: Kantin): void {
|
onEdit(item: Kantin): void {
|
||||||
// TODO: Edit-Ansicht bauen. Bis dahin könnte Create mit Prefill genutzt werden.
|
// Edit-Ansicht
|
||||||
this.router.navigate(['/create'], {
|
this.router.navigate(['/create'], {
|
||||||
queryParams: {
|
queryParams: {
|
||||||
id: item.szDocumentId,
|
id: item.szDocumentId,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user