Skip to content

Commit 5725781

Browse files
committed
refactor(example): remove @ngrx/db
format fixes as per PR import order change as per PR remove generic typing as per PR
1 parent f1a50ce commit 5725781

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

projects/example-app/src/app/books/effects/collection.effects.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { Injectable } from '@angular/core';
2+
import { Actions, Effect, ofType } from '@ngrx/effects';
3+
import { Action } from '@ngrx/store';
4+
import { defer, Observable, of } from 'rxjs';
5+
import { catchError, map, mergeMap, switchMap } from 'rxjs/operators';
6+
import { Book } from '@example-app/books/models/book';
27
import {
38
CollectionApiActions,
49
CollectionPageActions,
510
SelectedBookPageActions,
611
} from '@example-app/books/actions';
7-
import { Book } from '@example-app/books/models/book';
812
import { BookStorageService } from '@example-app/core/services/book-storage.service';
9-
import { Actions, Effect, ofType } from '@ngrx/effects';
10-
import { Action } from '@ngrx/store';
11-
import { defer, Observable, of } from 'rxjs';
12-
import { catchError, map, mergeMap, switchMap } from 'rxjs/operators';
13-
1413
@Injectable()
1514
export class CollectionEffects {
1615
/**

projects/example-app/src/app/core/services/book-storage.service.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe('BookStorageService', () => {
4040
const expected = cold('(-a|)', { a: true });
4141
expect(fixture.supported()).toBeObservable(expected);
4242
});
43+
4344
it('should throw error if localStorage provider not available', () => {
4445
TestBed.resetTestingModule().configureTestingModule({
4546
providers: [
@@ -77,6 +78,7 @@ describe('BookStorageService', () => {
7778
);
7879
localStorageFake.setItem.mockClear();
7980
});
81+
8082
it('should add multiple items', () => {
8183
const result = [...persistedCollection, book1, book3];
8284
const expected = cold('(-a|)', { a: result });
@@ -105,6 +107,7 @@ describe('BookStorageService', () => {
105107
);
106108
localStorageFake.getItem.mockClear();
107109
});
110+
108111
it('should remove multiple items from collection', () => {
109112
const filterCollection = persistedCollection.filter(
110113
f => f.id !== book4.id
@@ -120,6 +123,7 @@ describe('BookStorageService', () => {
120123
);
121124
localStorageFake.getItem.mockClear();
122125
});
126+
123127
it('should ignore items not present in collection', () => {
124128
const filterCollection = persistedCollection;
125129
const expected = cold('(-a|)', { a: filterCollection });

projects/example-app/src/app/core/services/book-storage.service.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ import { Book } from '@example-app/books/models/book';
1111
export interface IBookStorageService {
1212
supported(): Observable<boolean>;
1313
deleteCollection(): Observable<boolean>;
14-
addToCollection<T = Book>(records: T[]): Observable<T[]>;
14+
addToCollection(records: Book[]): Observable<Book[]>;
1515
getCollection(): Observable<Book[]>;
16-
removeFromCollection<T extends { id: string } = Book>(
17-
ids: Array<string>
18-
): Observable<T[]>;
16+
removeFromCollection<Book>(ids: Array<string>): Observable<Book[]>;
1917
}
2018

2119
export const LOCAL_STORAGE_TOKEN = new InjectionToken(
@@ -32,28 +30,26 @@ export class BookStorageService implements IBookStorageService {
3230
: throwError('Local Storage Not Supported');
3331
}
3432

35-
getCollection<T = Book>(): Observable<T[]> {
33+
getCollection(): Observable<Book[]> {
3634
return this.supported().pipe(
3735
map(_ => this.storage.getItem(this.collectionKey)),
3836
map((value: string | null) => (value ? JSON.parse(value) : []))
3937
);
4038
}
4139

42-
addToCollection<T = Book>(records: T[]): Observable<T[]> {
43-
return this.getCollection<T>().pipe(
44-
map((value: T[]) => [...value, ...records]),
45-
tap((value: T[]) =>
40+
addToCollection(records: Book[]): Observable<Book[]> {
41+
return this.getCollection().pipe(
42+
map((value: Book[]) => [...value, ...records]),
43+
tap((value: Book[]) =>
4644
this.storage.setItem(this.collectionKey, JSON.stringify(value))
4745
)
4846
);
4947
}
5048

51-
removeFromCollection<T extends { id: string } = Book>(
52-
ids: Array<string>
53-
): Observable<T[]> {
54-
return this.getCollection<T>().pipe(
55-
map((value: T[]) => value.filter(item => !ids.includes(item.id))),
56-
tap((value: T[]) =>
49+
removeFromCollection(ids: Array<string>): Observable<Book[]> {
50+
return this.getCollection().pipe(
51+
map((value: Book[]) => value.filter(item => !ids.includes(item.id))),
52+
tap((value: Book[]) =>
5753
this.storage.setItem(this.collectionKey, JSON.stringify(value))
5854
)
5955
);

0 commit comments

Comments
 (0)