Skip to content

Commit

Permalink
Merge pull request #24 from FelipeDuarteLuna/19-02-2024 - Directives
Browse files Browse the repository at this point in the history
Add log Directive
  • Loading branch information
FelipeDuarteLuna authored Mar 1, 2024
2 parents d3639ce + b628f38 commit 3115dfd
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
33 changes: 33 additions & 0 deletions modules/feature/home/src/lib/directives/log/log.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -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: `
<lib-product-card libLog [product]="product"></lib-product-card>
`,
})
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');
});
});
46 changes: 46 additions & 0 deletions modules/feature/home/src/lib/directives/log/log.directive.ts
Original file line number Diff line number Diff line change
@@ -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<void>();

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();
}
}
2 changes: 2 additions & 0 deletions modules/feature/home/src/lib/home.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
Expand Down
7 changes: 6 additions & 1 deletion modules/feature/home/src/lib/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ <h2>Produtos recomendados para você</h2>

<div class="product-card-container">
@for (product of products$ | async; track product.id) {
<lib-product-card [product]="product"></lib-product-card>
<lib-product-card
libLog
[id]="product.id"
(doubleClick)="log()"
[product]="product"
></lib-product-card>
} @empty {
<strong>Não temos produtos recomendados para você por enquanto.</strong>
}
Expand Down
5 changes: 5 additions & 0 deletions modules/feature/home/src/lib/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit 3115dfd

Please sign in to comment.