-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathcomponent.ts
41 lines (34 loc) · 1.32 KB
/
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
38
39
40
41
import { dispatch, select, select$, WithSubStore } from '@angular-redux/store';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Animal } from '../model';
import { animalComponentReducer } from './reducers';
export const toSubTotal = (obs$: Observable<Animal>): Observable<number> =>
obs$.map(s => s.ticketPrice * s.tickets);
/**
* Fractal component example.
*/
@WithSubStore({
basePathMethodName: 'getBasePath',
localReducer: animalComponentReducer,
})
@Component({
selector: 'zoo-animal',
templateUrl: './component.html',
styleUrls: ['./component.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AnimalComponent {
static readonly ADD_TICKET = 'ADD_TICKET';
static readonly REMOVE_TICKET = 'REMOVE_TICKET';
@Input() key: string;
@Input() animalType: string;
@select() readonly name$: Observable<string>;
@select('tickets') readonly numTickets$: Observable<number>;
@select('ticketPrice') readonly ticketPrice$: Observable<number>;
@select$(null, toSubTotal)
readonly subTotal$: Observable<number>;
getBasePath = () => (this.key ? [this.animalType, 'items', this.key] : null);
@dispatch() addTicket = () => ({ type: 'ADD_TICKET' });
@dispatch() removeTicket = () => ({ type: 'REMOVE_TICKET' });
}