Skip to content

Refactor: remove subscriptions in products #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: basic/final
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const routes: Routes = [
{
path: 'products',
loadChildren: () =>
import('./products-overview/products-overview.module').then(m => m.ProductsOverviewModule)
import('./products/products.module').then(m => m.ProductsModule)
}
];

Expand Down
28 changes: 0 additions & 28 deletions src/app/products-overview/products-overview.component.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/app/products-overview/products-overview.module.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ProductsOverviewComponent } from './products-overview.component';
import { ProductsOverviewComponent } from './products-list.component';
import { Type } from "@angular/core";
import { By } from "@angular/platform-browser";
import { ProductsComponent } from "../products/products.component";
Expand Down
32 changes: 32 additions & 0 deletions src/app/products/list/products-list.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Component } from '@angular/core';
import { Product } from "../../shared/product";
import { ProductsService } from "../shared/products.service";
import { Observable, Subject } from "rxjs";
import { distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';

@Component({
templateUrl: './products-list.component.html',
styleUrls: ['./products-list.component.scss']
})
export class ProductsListComponent {
private readonly searchSubj = new Subject<string>();
public readonly products$: Observable<Product[]>

query: string;

constructor(private productsService: ProductsService) {
const search$ = this.searchSubj.asObservable().pipe(
map(s => s.toLowerCase()),
distinctUntilChanged(),
startWith('')
);
this.products$ = search$.pipe(
switchMap(filter => this.productsService.getFiltered$({filter}))
);
}

onSearch(query: string): void {
this.searchSubj.next(query);
}

}
12 changes: 9 additions & 3 deletions src/app/products/products.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { ProductsComponent } from "./products.component";
import { ProductComponent } from "./product/product.component";
import { SharedModule } from "../shared/shared.module";
import { SearchComponent } from './search/search.component';
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { ProductsFilterPipe } from './shared/products-filter.pipe';
import { ProductsListComponent } from './list/products-list.component';

const routes: Routes = [
{ path: '', component: ProductsListComponent}
];

@NgModule({
declarations: [ProductsComponent, ProductComponent, SearchComponent, ProductsFilterPipe],
declarations: [ProductsComponent, ProductsListComponent, ProductComponent, SearchComponent, ProductsFilterPipe],
imports: [
CommonModule,
SharedModule,
FormsModule,
ReactiveFormsModule
ReactiveFormsModule,
RouterModule.forChild(routes)
],
exports: [ProductsComponent, SearchComponent, ProductsFilterPipe]
})
export class ProductsModule { }
17 changes: 3 additions & 14 deletions src/app/products/shared/products.service.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import { Injectable } from '@angular/core';
import { Product } from "../../shared/product";
import { Observable, Subject } from "rxjs";
import { Observable } from "rxjs";
import { HttpClient } from "@angular/common/http";

@Injectable({
providedIn: 'root'
})
export class ProductsService {
products$: Observable<Product[]>;
error$: Observable<unknown>;

private productsSubject = new Subject<Product[]>();
private errorSubject = new Subject<unknown>();

constructor(private httpClient: HttpClient) {
this.products$ = this.productsSubject.asObservable();
this.error$ = this.errorSubject.asObservable();
}

get({ filter}: { filter?: string} = {}): void {
this.httpClient.get<Product[]>(`/api/products${filter ? `?filter=${filter}` : ''}`)
.subscribe(
products => this.productsSubject.next(products),
error => this.errorSubject.next(error)
);
getFiltered$({ filter}: { filter?: string} = {}): Observable<Product[]>{
return this.httpClient.get<Product[]>(`/api/products${filter ? `?filter=${filter}` : ''}`);
}
}