|
| 1 | +import {Component, OnInit} from '@angular/core'; |
| 2 | +import {AlbumGetDto} from "../../album/albums-view/album-get.model"; |
| 3 | +import {UserModel} from "../../services/user.model"; |
| 4 | +import {ActivatedRoute, Router, RouterLink} from "@angular/router"; |
| 5 | +import {AlbumService} from "../../services/album.service"; |
| 6 | +import {AuthService} from "../../services/auth.service"; |
| 7 | +import {AlbumElementsComponent} from "../album-elements/album-elements.component"; |
| 8 | +import {DatePipe, NgClass, NgIf} from "@angular/common"; |
| 9 | +import {ItemNoFoundComponent} from "../../shared/item-no-found/item-no-found.component"; |
| 10 | +import {ModalComponent} from "../../shared/modal/modal.component"; |
| 11 | +import {AlbumElementService} from "../../services/album-element.service"; |
| 12 | +import {AlbumElementViewGetModel} from "./album-element-view-get.model"; |
| 13 | + |
| 14 | +@Component({ |
| 15 | + selector: 'app-album-element-view', |
| 16 | + standalone: true, |
| 17 | + imports: [ |
| 18 | + AlbumElementsComponent, |
| 19 | + DatePipe, |
| 20 | + ItemNoFoundComponent, |
| 21 | + ModalComponent, |
| 22 | + NgIf, |
| 23 | + NgClass, |
| 24 | + RouterLink |
| 25 | + ], |
| 26 | + templateUrl: './album-element-view.component.html', |
| 27 | + styleUrl: './album-element-view.component.css' |
| 28 | +}) |
| 29 | +export class AlbumElementViewComponent implements OnInit { |
| 30 | + element: AlbumElementViewGetModel | null = null; |
| 31 | + isMyAlbumElement: boolean = false; |
| 32 | + isModalOpen = false; |
| 33 | + errorMessage = ''; |
| 34 | + |
| 35 | + user: UserModel | undefined; |
| 36 | + |
| 37 | + constructor( |
| 38 | + private route: ActivatedRoute, |
| 39 | + private albumService: AlbumService, |
| 40 | + private albumElementService: AlbumElementService, |
| 41 | + private router: Router, |
| 42 | + private authService: AuthService |
| 43 | + ) {} |
| 44 | + |
| 45 | + ngOnInit(): void { |
| 46 | + const albumElementId = this.route.snapshot.paramMap.get('id'); |
| 47 | + |
| 48 | + if (albumElementId) { |
| 49 | + this.albumElementService.getAlbumElementById(albumElementId).subscribe({ |
| 50 | + next: (element) => { |
| 51 | + this.element = element; |
| 52 | + this.isMyAlbumElement = this.ifCurrentUserIsOwner(); |
| 53 | + }, |
| 54 | + error: (err) => { |
| 55 | + this.errorMessage = `Failed to load album element (${err.status})`; |
| 56 | + console.error('Failed to load album element:', err); |
| 57 | + }, |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Handle Edit button |
| 63 | + |
| 64 | + ifCurrentUserIsOwner(): boolean { |
| 65 | + this.authService.user().subscribe(user => { |
| 66 | + this.user = user; |
| 67 | + }); |
| 68 | + |
| 69 | + if (this.user && this.element) { |
| 70 | + return this.element.album.author.id === this.user.id; |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + editAlbumElement(): void { |
| 75 | + if (this.element) { |
| 76 | + this.router.navigate(['/album-element-edit', this.element.id]).then(r => |
| 77 | + console.log('Edit album element:', r)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + openModal(): void { |
| 82 | + this.isModalOpen = true; |
| 83 | + } |
| 84 | + |
| 85 | + closeModal(): void { |
| 86 | + this.isModalOpen = false; |
| 87 | + } |
| 88 | + |
| 89 | + confirmDelete(): void { |
| 90 | + if (this.element) { |
| 91 | + this.albumElementService.deleteAlbumElement(this.element.id).subscribe({ |
| 92 | + next: () => { |
| 93 | + this.router.navigate(['/profile'], { queryParams: { my: true } }).then(() => { |
| 94 | + console.log('Album element deleted'); |
| 95 | + }); |
| 96 | + |
| 97 | + this.closeModal(); |
| 98 | + }, |
| 99 | + error: (err) => { |
| 100 | + this.errorMessage = `Failed to delete album element(${err.status})`; |
| 101 | + console.error('Failed to delete album element:', err); |
| 102 | + }, |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments