From b628f3862a95ca1eedbdc59f54dba46166613e7f Mon Sep 17 00:00:00 2001 From: Felipe Luna Date: Fri, 1 Mar 2024 08:40:32 -0300 Subject: [PATCH] feat(home): add log directive --- .../src/lib/guards/auth/auth.guard.spec.ts | 3 +- .../lib/directives/log/log.directive.spec.ts | 33 +++++++++++++ .../src/lib/directives/log/log.directive.ts | 46 +++++++++++++++++++ modules/feature/home/src/lib/home.module.ts | 2 + .../home/src/lib/home/home.component.html | 7 ++- .../home/src/lib/home/home.component.ts | 5 ++ 6 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 modules/feature/home/src/lib/directives/log/log.directive.spec.ts create mode 100644 modules/feature/home/src/lib/directives/log/log.directive.ts diff --git a/modules/data-access/auth/src/lib/guards/auth/auth.guard.spec.ts b/modules/data-access/auth/src/lib/guards/auth/auth.guard.spec.ts index 83db6d3..52412b4 100644 --- a/modules/data-access/auth/src/lib/guards/auth/auth.guard.spec.ts +++ b/modules/data-access/auth/src/lib/guards/auth/auth.guard.spec.ts @@ -1,7 +1,8 @@ import { TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; -import { AuthService, authGuard } from 'auth-data-access'; import { of } from 'rxjs'; +import { AuthService } from '../../auth/auth.service'; +import { authGuard } from './auth.guard'; describe('authGuard', () => { it('should return true when user is not truthy', () => { diff --git a/modules/feature/home/src/lib/directives/log/log.directive.spec.ts b/modules/feature/home/src/lib/directives/log/log.directive.spec.ts new file mode 100644 index 0000000..474d476 --- /dev/null +++ b/modules/feature/home/src/lib/directives/log/log.directive.spec.ts @@ -0,0 +1,33 @@ +import { Component } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { RouterTestingModule } from '@angular/router/testing'; +import { mockProducts } from 'product-data-access'; +import { ProductCardComponent } from 'modules/ui/product/src/components/product-card.component'; +import { LogDirective } from './log.directive'; + +@Component({ + selector: 'lib-host-component', + template: ` + + `, +}) +class HostComponent { + product = mockProducts[0]; +} + +describe('LogDirective', () => { + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [HostComponent], + imports: [LogDirective, ProductCardComponent, RouterTestingModule], + }).compileComponents(); + }); + + it('should have cursor pointer', () => { + const fixture = TestBed.createComponent(HostComponent); + fixture.detectChanges(); + const card: HTMLElement = + fixture.nativeElement.querySelector('lib-product-card'); + expect(card.style.cursor).toBe('pointer'); + }); +}); diff --git a/modules/feature/home/src/lib/directives/log/log.directive.ts b/modules/feature/home/src/lib/directives/log/log.directive.ts new file mode 100644 index 0000000..a0e5748 --- /dev/null +++ b/modules/feature/home/src/lib/directives/log/log.directive.ts @@ -0,0 +1,46 @@ +import { + Directive, + ElementRef, + EventEmitter, + HostListener, + Input, + OnInit, + Output, + Renderer2, + inject, +} from '@angular/core'; +import { Router } from '@angular/router'; +import { ProductCardComponent } from 'modules/ui/product/src/components/product-card.component'; + +@Directive({ + selector: '[libLog]', + standalone: true, +}) +export class LogDirective implements OnInit { + @Input() id = ''; + @Output() doubleClick = new EventEmitter(); + + router = inject(Router); + productCardComponent = inject(ProductCardComponent); + elementRef = inject(ElementRef); + renderer = inject(Renderer2); + + ngOnInit(): void { + this.renderer.setStyle(this.elementRef.nativeElement, 'cursor', 'pointer'); + } + + @HostListener('click', ['$event']) + onClick(): void { + // eslint-disable-next-line no-console + console.log('Cliclou no CARD: ', this.id); + //this.router.navigate(['product',this.id]); + // eslint-disable-next-line no-console + console.log(this.elementRef.nativeElement); + this.router.navigate(['product', this.productCardComponent.product.id]); + } + + @HostListener('dblclick', ['$event']) + onDoubleClick(): void { + this.doubleClick.emit(); + } +} diff --git a/modules/feature/home/src/lib/home.module.ts b/modules/feature/home/src/lib/home.module.ts index 4512157..e6fa5e1 100644 --- a/modules/feature/home/src/lib/home.module.ts +++ b/modules/feature/home/src/lib/home.module.ts @@ -4,12 +4,14 @@ import { RouterModule } from '@angular/router'; import { homeRoutes } from './lib.routes'; import { HomeComponent } from './home/home.component'; import { ProductCardComponent } from 'product-ui'; +import { LogDirective } from './directives/log/log.directive'; @NgModule({ imports: [ CommonModule, RouterModule.forChild(homeRoutes), ProductCardComponent, + LogDirective, ], declarations: [HomeComponent], }) diff --git a/modules/feature/home/src/lib/home/home.component.html b/modules/feature/home/src/lib/home/home.component.html index 1864987..792fb99 100644 --- a/modules/feature/home/src/lib/home/home.component.html +++ b/modules/feature/home/src/lib/home/home.component.html @@ -4,7 +4,12 @@

Produtos recomendados para você

@for (product of products$ | async; track product.id) { - + } @empty { Não temos produtos recomendados para você por enquanto. } diff --git a/modules/feature/home/src/lib/home/home.component.ts b/modules/feature/home/src/lib/home/home.component.ts index 839313e..78ec7fd 100644 --- a/modules/feature/home/src/lib/home/home.component.ts +++ b/modules/feature/home/src/lib/home/home.component.ts @@ -10,4 +10,9 @@ export class HomeComponent { constructor(private recommendedProductsService: RecommendedProductsService) {} products$ = this.recommendedProductsService.getProducts(); + + log() { + // eslint-disable-next-line no-console + console.log('Nosso DoubleClick funcionou'); + } }