-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from FelipeDuarteLuna/08-01-23
08-01-2024 - Observables e Subjects
- Loading branch information
Showing
13 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 13 additions & 1 deletion
14
modules/data-access/product/src/lib/state/cart/cart.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BehaviorSubject, map } from 'rxjs'; | ||
import { Product } from '../../models/product.model'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class CartService { | ||
constructor() {} | ||
private CartSubject = new BehaviorSubject<Product[]>([]); | ||
cart$ = this.CartSubject.asObservable(); | ||
|
||
productQuantity$ = this.CartSubject.pipe(map((products) => products.length)); | ||
|
||
addToCart(product: Product): void { | ||
const cart = this.CartSubject.getValue(); | ||
this.CartSubject.next([...cart, product]); | ||
// eslint-disable-next-line no-console | ||
console.log(this.CartSubject.getValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<button | ||
mat-icon-button | ||
[matBadge]="quantity" | ||
matBadgeColor="warn" | ||
color="accent" | ||
attr.aria-label="Carrinho de compras com {{ quantity }} produtos" | ||
> | ||
<mat-icon>shopping_cart</mat-icon> | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
button { | ||
color: #fff; | ||
} |
21 changes: 21 additions & 0 deletions
21
modules/ui/product/src/components/cart/cart.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { CartComponent } from './cart.component'; | ||
|
||
describe('CartComponent', () => { | ||
let component: CartComponent; | ||
let fixture: ComponentFixture<CartComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [CartComponent], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(CartComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Component, Input } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { MatBadgeModule } from '@angular/material/badge'; | ||
import { MatIconModule } from '@angular/material/icon'; | ||
import { MatButtonModule } from '@angular/material/button'; | ||
|
||
@Component({ | ||
selector: 'lib-cart', | ||
standalone: true, | ||
imports: [CommonModule, MatBadgeModule, MatIconModule, MatButtonModule], | ||
templateUrl: './cart.component.html', | ||
styleUrl: './cart.component.scss', | ||
}) | ||
export class CartComponent { | ||
@Input() quantity!: number | null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './components/product-card.component'; | ||
|
||
export * from './components/cart/cart.component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<lib-header title="Ecommerce"> | ||
<lib-product-search></lib-product-search> | ||
<a> login</a> | ||
<lib-cart [quantity]="cartService.productQuantity$ | async"></lib-cart> | ||
</lib-header> | ||
<router-outlet></router-outlet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters