import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { UserCollectionService } from '../../../../core/services/userCollection.service'; import { UserCollection } from '../../../../shared/models/userCollection.model'; import { DatePipe } from '@angular/common'; import { CollectionEntry } from '../../../../shared/models/dtos/collection-entry.dto'; import { CollectionEditModalComponent } from '../collection-edit-modal/collection-edit-modal.component'; import { MatDialog } from '@angular/material/dialog'; import { ConfirmModalComponent } from '../../../../shared/components/modals/confirm-modal/confirm-modal.component'; import { ScreenshotService } from '../../../../core/services/screenshot.service'; @Component({ selector: 'app-userCollection-info-page', templateUrl: './userCollection-info-page.component.html', styleUrls: ['./userCollection-info-page.component.css'], providers: [DatePipe], }) export class UserCollectionInfoPageComponent implements OnInit { collection: UserCollection = {} as UserCollection; createdAt: string = ''; updatedAt: string = ''; viewMode: string = 'grid'; screenMode: boolean = false; constructor( private route: ActivatedRoute, private userCollectionService: UserCollectionService, private screenshotService: ScreenshotService, private datePipe: DatePipe, public dialog: MatDialog, private router: Router ) {} ngOnInit(): void { const id = this.route.snapshot.paramMap.get('id'); if (id == null) return; // todo redirect if no id is provided this.userCollectionService.getCollection(id.toString()).subscribe( (data) => { this.collection = data as unknown as UserCollection; this.collection.entries = this.collection.entries as CollectionEntry[]; this.createdAt = this.getCreatedDate()?.toString() || ''; this.updatedAt = this.getUpdatedAt()?.toString() || ''; // console.log( // `Collection "${this.collection.name}" received:`, // this.collection // ); }, (error) => { console.error('Error occurred:', error); } ); } getListId() { return this.collection.id + ''; } getCreatedDate() { return this.datePipe.transform(this.collection.createdAt, 'dd MMMM, yyyy'); } // TODO use a relativeDate pipe getUpdatedAt() { const updatedAt = this.collection.updatedAt; // First, check if updatedAt is a valid date string or timestamp const modifiedDate = new Date(updatedAt); // Check if the date is invalid if (isNaN(modifiedDate.getTime())) { // console.error('Invalid date:', updatedAt); // Handle invalid date, perhaps return a fallback message or undefined return 'Date not available'; } const now = new Date(); const diffInMs = now.getTime() - modifiedDate.getTime(); const diffInDays = diffInMs / (1000 * 60 * 60 * 24); let isPlural = ''; if (diffInDays < 7) { // Less than a week, show relative time if (diffInDays < 1) { const diffInHours = diffInMs / (1000 * 60 * 60); if (diffInHours < 1) { const diffInMinutes = diffInMs / (1000 * 60); if (diffInMinutes >= 2) isPlural = 's'; return `${Math.floor(diffInMinutes)} minute${isPlural} ago`; } if (diffInHours >= 2) isPlural = 's'; return `${Math.floor(diffInHours)} hour${isPlural} ago`; } if (diffInDays >= 2) isPlural = 's'; return `${Math.floor(diffInDays)} day${isPlural} ago`; } else { // More than a week, show formatted date return this.datePipe.transform(modifiedDate, 'dd MMMM, yyyy'); } } openEditModal() { if (!this.collection) return; const dialogRef = this.dialog.open(CollectionEditModalComponent, { width: '360px', data: { name: this.collection.name, description: this.collection.description, user_rating: this.collection.user_rating || 0, poster: this.collection.poster_path, }, }); dialogRef.afterClosed().subscribe((result) => { if (result) { if (result.formData == null) return; this.userCollectionService .updateCollection(this.collection.id, result.formData) .subscribe({ next: (userCollection) => { if (userCollection) { // TODO confirm notification // console.log(`User Collection updated: `, userCollection); this.collection = userCollection; } }, error: (error) => { console.error('Error updating userCollection:', error); // TODO error notification }, }); } }); } switchViewMode() { this.viewMode = this.viewMode == 'grid' ? 'table' : 'grid'; } deleteCollection() { if (!this.collection) return; const dialogRef = this.dialog.open(ConfirmModalComponent, { width: '400px', data: { title: 'Confirmation', message: 'Are you sure you want to delete this collection? This action cannot be undone and you will be unable to recover any data.', type: 'danger', }, }); dialogRef.afterClosed().subscribe((result) => { if (result) { if (result.confirmed != true) return; this.userCollectionService .deleteCollection(this.collection.id) .subscribe({ next: () => { // TODO confirm notification this.router.navigate(['/collections']); }, error: (error) => { console.error('Error updating userCollection:', error); // TODO error notification }, }); } }); } shareScreenshot() { this.screenMode = true; const oldViewMode = this.viewMode; this.viewMode = 'grid'; this.screenshotService.capture('gridDiv').then(dataUrl => { this.copyToClipboard(dataUrl); }).catch(error => { console.error('Error capturing screenshot:', error); }); this.screenMode = false; this.viewMode = oldViewMode; } private copyToClipboard(dataUrl: string) { const img = new Image(); img.src = dataUrl; const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); img.onload = () => { canvas.width = img.width; canvas.height = img.height; ctx!.drawImage(img, 0, 0); canvas.toBlob(blob => { const item = new ClipboardItem({ 'image/png': blob as Blob }); navigator.clipboard.write([item]); }); }; } }