From 0a8281da6076d5800043f38a4d6423d1c402f266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cakib1997=E2=80=9D?= <“akibh805@gmail.comgit config --global user.name “akib1997> Date: Sat, 22 Mar 2025 16:43:57 +0600 Subject: [PATCH] refactor: replace GreetingUtil with a pipe for better testability --- .../secured-feat/secured-feat.component.html | 2 +- .../secured-feat/secured-feat.component.ts | 8 ++++---- src/app/shared/pipes/greeting.pipe.ts | 13 +++++++++++++ src/app/shared/pipes/index.ts | 1 + src/app/shared/util/greeting-util.ts | 14 -------------- src/app/shared/util/index.ts | 1 - 6 files changed, 19 insertions(+), 20 deletions(-) create mode 100644 src/app/shared/pipes/greeting.pipe.ts create mode 100644 src/app/shared/pipes/index.ts delete mode 100644 src/app/shared/util/greeting-util.ts delete mode 100644 src/app/shared/util/index.ts diff --git a/src/app/features/secured-feat/secured-feat.component.html b/src/app/features/secured-feat/secured-feat.component.html index 6ccae29..f036af9 100644 --- a/src/app/features/secured-feat/secured-feat.component.html +++ b/src/app/features/secured-feat/secured-feat.component.html @@ -1,7 +1,7 @@ @if (vm$ | async; as vm) {

- {{ vm.greeting }}, + {{ currentTime() | greeting }}, {{ vm.authUser?.firstName }} {{ vm.authUser?.lastName }} diff --git a/src/app/features/secured-feat/secured-feat.component.ts b/src/app/features/secured-feat/secured-feat.component.ts index 06c3649..6ae8492 100644 --- a/src/app/features/secured-feat/secured-feat.component.ts +++ b/src/app/features/secured-feat/secured-feat.component.ts @@ -1,24 +1,24 @@ import { AsyncPipe } from '@angular/common'; -import { Component, inject } from '@angular/core'; +import { Component, inject, signal } from '@angular/core'; import { MatCardModule } from '@angular/material/card'; import { MatTableModule } from '@angular/material/table'; import { combineLatest, of } from 'rxjs'; import { AUTH_FACADE } from '../../auth'; import { USERS } from '../../core/fake-api'; -import { GreetingUtil } from '../../shared/util'; +import { GreetingPipe } from '../../shared/pipes'; @Component({ selector: 'aa-secured-feat', - imports: [AsyncPipe, MatCardModule, MatTableModule], + imports: [AsyncPipe, MatCardModule, MatTableModule, GreetingPipe], templateUrl: './secured-feat.component.html', }) export class SecuredFeatComponent { private readonly authFacade = inject(AUTH_FACADE); readonly displayedColumns: string[] = ['id', 'name', 'username', 'password']; + readonly currentTime = signal(new Date()); readonly vm$ = combineLatest({ - greeting: of(GreetingUtil.greet()), authUser: this.authFacade.authUser$, users: of(USERS), }); diff --git a/src/app/shared/pipes/greeting.pipe.ts b/src/app/shared/pipes/greeting.pipe.ts new file mode 100644 index 0000000..34d12af --- /dev/null +++ b/src/app/shared/pipes/greeting.pipe.ts @@ -0,0 +1,13 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'greeting', +}) +export class GreetingPipe implements PipeTransform { + transform(date: Date = new Date()): string { + const hour = date.getHours(); + if (hour < 12) return 'Good morning'; + if (hour < 18) return 'Good afternoon'; + return 'Good evening'; + } +} diff --git a/src/app/shared/pipes/index.ts b/src/app/shared/pipes/index.ts new file mode 100644 index 0000000..9bdc121 --- /dev/null +++ b/src/app/shared/pipes/index.ts @@ -0,0 +1 @@ +export * from './greeting.pipe'; diff --git a/src/app/shared/util/greeting-util.ts b/src/app/shared/util/greeting-util.ts deleted file mode 100644 index 7487b6f..0000000 --- a/src/app/shared/util/greeting-util.ts +++ /dev/null @@ -1,14 +0,0 @@ -export class GreetingUtil { - static greet(): string { - const today = new Date(); - const currentHour = today.getHours(); - - if (currentHour < 12) { - return 'Good morning'; - } else if (currentHour < 18) { - return 'Good afternoon'; - } else { - return 'Good evening'; - } - } -} diff --git a/src/app/shared/util/index.ts b/src/app/shared/util/index.ts deleted file mode 100644 index d169f5a..0000000 --- a/src/app/shared/util/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './greeting-util';