From d4fb225f8e2b1a927921272c280c6db7811eeca5 Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Fri, 5 Apr 2024 03:34:18 +0000 Subject: [PATCH] add UserCollection info page --- .../list-info/list-info.component.css | 0 .../list-info/list-info.component.html | 34 +++++++ .../list-info/list-info.component.spec.ts | 21 +++++ .../features/list-info/list-info.component.ts | 91 +++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 frontend/src/app/features/list-info/list-info.component.css create mode 100644 frontend/src/app/features/list-info/list-info.component.html create mode 100644 frontend/src/app/features/list-info/list-info.component.spec.ts create mode 100644 frontend/src/app/features/list-info/list-info.component.ts diff --git a/frontend/src/app/features/list-info/list-info.component.css b/frontend/src/app/features/list-info/list-info.component.css new file mode 100644 index 0000000..e69de29 diff --git a/frontend/src/app/features/list-info/list-info.component.html b/frontend/src/app/features/list-info/list-info.component.html new file mode 100644 index 0000000..3c8dcfd --- /dev/null +++ b/frontend/src/app/features/list-info/list-info.component.html @@ -0,0 +1,34 @@ + + + +
+
+
+
{{list.name}}
+ +
+
Created - {{createdAt}} | Updated - {{updatedAt}}
+
{{list.description}}
+
+
+ +
+
+
\ No newline at end of file diff --git a/frontend/src/app/features/list-info/list-info.component.spec.ts b/frontend/src/app/features/list-info/list-info.component.spec.ts new file mode 100644 index 0000000..1e4c24c --- /dev/null +++ b/frontend/src/app/features/list-info/list-info.component.spec.ts @@ -0,0 +1,21 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ListInfoComponent } from './list-info.component'; + +describe('ListInfoComponent', () => { + let component: ListInfoComponent; + let fixture: ComponentFixture; + + beforeEach(() => { + TestBed.configureTestingModule({ + declarations: [ListInfoComponent] + }); + fixture = TestBed.createComponent(ListInfoComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/features/list-info/list-info.component.ts b/frontend/src/app/features/list-info/list-info.component.ts new file mode 100644 index 0000000..50b5900 --- /dev/null +++ b/frontend/src/app/features/list-info/list-info.component.ts @@ -0,0 +1,91 @@ +import { Component } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; +import { ListService } from '../../core/services/list.service'; +import { List } from '../../shared/models/list.model'; +import { Entry } from '../../shared/models/entry.model'; +import { DatePipe } from '@angular/common'; + +@Component({ + selector: 'app-list-info', + templateUrl: './list-info.component.html', + styleUrls: ['./list-info.component.css'], + providers: [DatePipe], +}) +export class ListInfoComponent { + list: List = {} as List; + createdAt: string = ''; + updatedAt: string = ''; + + constructor( + private route: ActivatedRoute, + private listService: ListService, + 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 List; + 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'); + } + } +}