Skip to content

Commit

Permalink
feat(cb2-8144): removes some subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
owen-corrigan-bjss committed Jul 26, 2023
1 parent 94ede89 commit c64da06
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Store, select } from '@ngrx/store';
import { ReferenceDataService } from '@services/reference-data/reference-data.service';
import { PaginationComponent } from '@shared/components/pagination/pagination.component';
import { selectAllReferenceDataByResourceType, selectRefDataBySearchTerm, selectReferenceDataByResourceKey } from '@store/reference-data';
import { Observable, Subject, catchError, filter, map, of, switchMap, take, takeUntil } from 'rxjs';
import { Observable, Subject, catchError, filter, lastValueFrom, map, of, switchMap, take, takeUntil } from 'rxjs';

@Component({
selector: 'app-reference-data-list',
Expand All @@ -22,7 +22,7 @@ export class ReferenceDataListComponent implements OnInit, OnDestroy {
pageStart?: number;
pageEnd?: number;
currentPage?: number;
data: Array<ReferenceDataModelBase> | undefined;
data?: Observable<Array<any> | undefined>;
private destroy$ = new Subject<void>();

public form!: CustomFormGroup;
Expand Down Expand Up @@ -90,7 +90,7 @@ export class ReferenceDataListComponent implements OnInit, OnDestroy {
.subscribe({
next: res => of(!!res)
});
this.store.pipe(select(selectAllReferenceDataByResourceType(this.type)), takeUntil(this.destroy$)).subscribe(items => (this.data = items));
this.data = this.store.pipe(select(selectAllReferenceDataByResourceType(this.type)));
}

get refDataAdminType$(): Observable<any | undefined> {
Expand Down Expand Up @@ -130,14 +130,14 @@ export class ReferenceDataListComponent implements OnInit, OnDestroy {
}

get paginatedItems$(): Observable<any[]> {
return of(this.data?.slice(this.pageStart, this.pageEnd) ?? []);
return this.data!.pipe(map(items => items?.slice(this.pageStart, this.pageEnd) ?? []));
}

get numberOfRecords$(): Observable<number> {
return of(this.data?.length ?? 0);
return this.data!.pipe(map(items => items?.length ?? 0));
}

search(term: string, filter: string) {
async search(term: string, filter: string) {
this.globalErrorService.clearErrors();
const trimmedTerm = term?.trim();
if (!trimmedTerm || !filter) {
Expand All @@ -146,13 +146,13 @@ export class ReferenceDataListComponent implements OnInit, OnDestroy {
return;
}

this.currentPage = 1;
this.store.pipe(select(selectRefDataBySearchTerm(trimmedTerm, this.type, filter)), take(1)).subscribe(items => {
if (items.length === 0) {
this.store.pipe(select(selectRefDataBySearchTerm(trimmedTerm, this.type, filter))).subscribe(items => {
if (!items) {
this.globalErrorService.addError({ error: 'Your search returned no results', anchorLink: 'term' });
} else {
this.currentPage = 1;
this.data = of(items);
this.searchReturned = true;
this.data = items;
this.handlePaginationChange({ start: 0, end: 24 });
this.currentPage = undefined;
}
Expand All @@ -164,7 +164,7 @@ export class ReferenceDataListComponent implements OnInit, OnDestroy {
this.globalErrorService.clearErrors();
if (this.searchReturned) {
this.currentPage = 1;
this.store.pipe(select(selectAllReferenceDataByResourceType(this.type)), take(1)).subscribe(items => (this.data = items));
this.data = this.store.pipe(select(selectAllReferenceDataByResourceType(this.type)));
this.handlePaginationChange({ start: 0, end: 24 });
this.currentPage = undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export const selectRefDataBySearchTerm = (searchTerm: string, referenceDataType:
searchItem.push(obj);
}
});
return searchItem;
if (searchItem.length) {
return searchItem;
}
return;
});

export const selectUserByResourceKey = (resourceKey: string) =>
Expand Down

0 comments on commit c64da06

Please sign in to comment.