|
|
|
@@ -8,22 +8,19 @@ import { ModalStatus } from '@app/_helpers/modal.states'; |
|
|
|
@Directive() |
|
|
|
export abstract class AbstractImageFormComponent<T extends { [key: string]: any }> extends AbstractDataFormComponent<T> { |
|
|
|
|
|
|
|
// Gemeinsame Eigenschaften für Komponenten mit Bildupload |
|
|
|
selectedFile: File | null = null; |
|
|
|
imageToDelete: string | null = null; |
|
|
|
|
|
|
|
@ViewChild('imageUpload') imageUpload!: ImageUploadComponent; |
|
|
|
|
|
|
|
// Abstrakte Eigenschaften, die von abgeleiteten Klassen implementiert werden müssen |
|
|
|
abstract get imageIriControlName(): string; |
|
|
|
abstract get imageDbId(): string | number | undefined | null; |
|
|
|
|
|
|
|
constructor( |
|
|
|
protected imageUploadService: ImageUploadService, |
|
|
|
formGroup: FormGroup, |
|
|
|
createFunction: ((data: T) => any) | undefined, |
|
|
|
updateFunction: (id: string | number, data: T) => any, |
|
|
|
deleteFunction: (id: string | number) => any, |
|
|
|
protected imageIriControlName: string, // neuer Parameter |
|
|
|
protected imageDbIdPath: string, |
|
|
|
...args: any[] |
|
|
|
) { |
|
|
|
super(formGroup, createFunction, updateFunction, deleteFunction, ...args); |
|
|
|
@@ -75,7 +72,7 @@ export abstract class AbstractImageFormComponent<T extends { [key: string]: any |
|
|
|
if (this.selectedFile && this.imageToDelete) { |
|
|
|
// Fall 1a: Ein neues Bild wurde ausgewählt UND ein altes soll gelöscht werden |
|
|
|
// Zuerst das alte Bild löschen, dann das neue hochladen |
|
|
|
const dbId = this.imageDbId; |
|
|
|
const dbId = this.getNestedProperty(this.data, this.imageDbIdPath); |
|
|
|
if (dbId) { |
|
|
|
this.imageUploadService.deleteImage(dbId).subscribe({ |
|
|
|
next: (success) => { |
|
|
|
@@ -93,9 +90,10 @@ export abstract class AbstractImageFormComponent<T extends { [key: string]: any |
|
|
|
} else if (this.selectedFile) { |
|
|
|
// Fall 1b: Nur ein neues Bild wurde ausgewählt (kein altes vorhanden) |
|
|
|
this.uploadNewFile(); |
|
|
|
} else if (this.imageToDelete && this.imageDbId) { |
|
|
|
} else if (this.imageToDelete) { |
|
|
|
// Fall 2: Nur ein bestehendes Bild soll gelöscht werden |
|
|
|
this.imageUploadService.deleteImage(this.imageDbId).subscribe({ |
|
|
|
const dbId = this.getNestedProperty(this.data, this.imageDbIdPath); |
|
|
|
this.imageUploadService.deleteImage(dbId).subscribe({ |
|
|
|
next: (success) => { |
|
|
|
this.imageToDelete = null; |
|
|
|
super.onSubmit(); |
|
|
|
@@ -133,4 +131,9 @@ export abstract class AbstractImageFormComponent<T extends { [key: string]: any |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
protected getNestedProperty(obj: any, path: string): any { |
|
|
|
if (!obj || !path) return null; |
|
|
|
return path.split('.').reduce((o, p) => (o ? o[p] : null), obj); |
|
|
|
} |
|
|
|
} |