-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing-facade.ts
43 lines (35 loc) · 1.56 KB
/
listing-facade.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Observable } from 'rxjs';
import { Action, DefaultProjectorFn, MemoizedSelector, Store } from '@ngrx/store';
import { ListingActionCreator } from '../actions/ListingActionCreator';
import { ListingQuery } from '../selectors/ListingQuery';
import { ListingState } from '../reducers/ListingState';
import { IListingFacade } from './IListingFacade';
export abstract class ListingFacade<T, S extends ListingState<T>> implements IListingFacade<T> {
creating$: Observable<boolean> = this.select(this.listingQuery.getCreating);
deleting$: Observable<boolean> = this.select(this.listingQuery.getDeleting);
entities$: Observable<T[]> = this.select(this.listingQuery.getEntities);
loading$: Observable<boolean> = this.select(this.listingQuery.getLoading);
updating$: Observable<boolean> = this.select(this.listingQuery.getUpdating);
protected constructor(
protected actions: ListingActionCreator<T>,
protected listingQuery: ListingQuery<T>,
protected store: Store<S>
) {
}
protected select<R>(selector: MemoizedSelector<S, R, DefaultProjectorFn<R>>) {
return this.store.select(selector)
}
protected dispatch(action: Action): void {
return this.store.dispatch(action);
}
public createEntity(newEntity: Omit<T, 'id'>): void {
this.dispatch(this.actions.createEntity(newEntity));
}
public editEntity(id: string, updatedItem: Partial<T>): void {
const payload = { id, updatedItem };
this.dispatch(this.actions.updateEntity(payload));
}
public deleteEntity(id: string): void {
this.dispatch(this.actions.deleteEntity(id))
}
}