-
Notifications
You must be signed in to change notification settings - Fork 70
/
ngrx-counter.component.ts
37 lines (31 loc) · 1.07 KB
/
ngrx-counter.component.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
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { decrement, increment, reset } from '../../actions/counter.actions';
import { CounterState } from '../../reducers/counter.reducer';
import { AppState } from '../../shared/app-state';
import { selectCounter } from '../../shared/selectors';
@Component({
selector: 'app-ngrx-counter',
templateUrl: './ngrx-counter.component.html',
styleUrls: ['./ngrx-counter.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NgRxCounterComponent {
public count$: Observable<CounterState>;
constructor(private store: Store<AppState>) {
this.count$ = store.pipe(select(selectCounter));
}
public increment(): void {
this.store.dispatch(increment());
}
public decrement(): void {
this.store.dispatch(decrement());
}
public reset(newCount: string): void {
const count = parseInt(newCount, 10);
if (!Number.isNaN(count)) {
this.store.dispatch(reset({ count }));
}
}
}