import {Component, inject, OnInit, signal} from "@angular/core"; import { Picture } from "../../../core/rest-services/picture/model/picture"; import {MatProgressSpinnerModule} from '@angular/material/progress-spinner'; import { MatSnackBar } from "@angular/material/snack-bar"; import { PictureRestService } from "../../../core/rest-services/picture/picture.rest-service"; import { MatIcon } from "@angular/material/icon"; import { MatDialogRef } from "@angular/material/dialog"; import {MatRippleModule} from '@angular/material/core'; import { MatTooltip } from "@angular/material/tooltip"; @Component({ selector: 'app-picture-selection', templateUrl: './picture-selection-dialog.component.html', styleUrl: './picture-selection-dialog.component.scss', imports: [ MatIcon, MatRippleModule, MatProgressSpinnerModule, MatTooltip ] }) export class PictureSelectionDialog implements OnInit { private readonly pictureRestService = inject(PictureRestService); private readonly snackBar = inject(MatSnackBar); private readonly dialogRef = inject(MatDialogRef); isLoading = signal(false); isLoaded = signal(false); pictures: Picture[] = []; ngOnInit(): void { this.isLoading.set(true); this.pictureRestService.getAllOfCurrentUser() .then(pictures => { this.pictures = pictures; }) .catch(error => { if (error.status === 401) { this.dialogRef.close(); } else { const errorMessage = $localize`An error occurred while loading pictures.`; console.error(errorMessage, error); this.snackBar.open(errorMessage, $localize`Close`, { duration: 5000 }); } }) .finally(() => { this.isLoading.set(false); this.isLoaded.set(true); }); } selectPicture(picture: Picture): void { this.dialogRef.close(picture.id); } closeDialog(): void { this.dialogRef.close(); } uploadPicture(fileSelectionEvent: any): void { const pictureFile = fileSelectionEvent.target.files[0]; if (pictureFile) { this.pictureRestService.uploadPicture(pictureFile) .then(pictureId => { this.dialogRef.close(pictureId); }) .catch(error => { const errorMessage = $localize`A technical error occurred while uploading your picture.`; console.error(errorMessage, error); this.snackBar.open(errorMessage, $localize`Close`, { duration: 5000 }); }); } } }