import { Component } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { UserCollectionService } from '../../../../core/services/userCollection.service'; import { UserCollection } from '../../../../shared/models/userCollection.model'; import { Entry } from '../../../../shared/models/entry.model'; import { DatePipe } from '@angular/common'; @Component({ selector: 'app-userCollection-info-page', templateUrl: './userCollection-info-page.component.html', styleUrls: ['./userCollection-info-page.component.css'], providers: [DatePipe], }) export class UserCollectionInfoPageComponent { list: UserCollection = {} as UserCollection; createdAt: string = ''; updatedAt: string = ''; constructor( private route: ActivatedRoute, private listService: UserCollectionService, private datePipe: DatePipe ) {} ngOnInit(): void { const id = this.route.snapshot.paramMap.get('id'); if (id == null) return; this.listService.getList(id.toString()).subscribe( (data) => { // Handle the successful reception of data. this.list = data as unknown as UserCollection; this.list.entries = this.list.entries as Entry[]; this.createdAt = this.getCreatedDate()?.toString() || ''; this.updatedAt = this.getUpdatedAt()?.toString() || ''; console.log('Data received:', this.list); }, (error) => { // Handle any error that occurs during the subscription console.error('Error occurred:', error); }, () => { // Handle completion of the Observable } ); } getListId() { return this.list.id + ''; } getCreatedDate() { return this.datePipe.transform(this.list.createdAt, 'dd MMMM, yyyy'); } getUpdatedAt() { console.log(1); const updatedAt = this.list.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); 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); return `${Math.floor(diffInMinutes)} minutes ago`; } return `${Math.floor(diffInHours)} hours ago`; } return `${Math.floor(diffInDays)} days ago`; } else { // More than a week, show formatted date // Ensure datePipe is properly injected and available in the component return this.datePipe.transform(modifiedDate, 'dd MMMM, yyyy'); } } }