Skip to content

Commit d39a11c

Browse files
Use new decorator interface to fractal stores (#37)
1 parent 1e5baea commit d39a11c

File tree

4 files changed

+27
-36
lines changed

4 files changed

+27
-36
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"dependencies": {
1616
"@angular-redux/form": "^6.3.0",
1717
"@angular-redux/router": "^6.3.0",
18-
"@angular-redux/store": "6.4.5",
18+
"@angular-redux/store": "6.5.0",
1919
"@angular/common": "^4.1.0",
2020
"@angular/compiler": "^4.1.0",
2121
"@angular/core": "^4.1.0",

src/app/animals/animal/component.ts

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,43 @@
1-
import { Component, Input, ChangeDetectionStrategy, OnInit } from '@angular/core';
2-
import { NgRedux, dispatch, select, ObservableStore } from '@angular-redux/store';
1+
import { Component, Input, ChangeDetectionStrategy } from '@angular/core';
2+
import { NgRedux, dispatch, select, select$, WithSubStore } from '@angular-redux/store';
33
import { Observable } from 'rxjs/Observable';
4-
import 'rxjs/add/observable/combineLatest';
54

65
import { IAppState } from '../../store/model';
76
import { animalComponentReducer } from './reducers';
7+
import { IAnimal } from '../model';
8+
9+
export const toSubTotal = (obs$: Observable<IAnimal>): Observable<number> =>
10+
obs$.map(s => s.ticketPrice * s.tickets);
811

912
/**
1013
* Fractal component example.
1114
*/
15+
@WithSubStore({
16+
basePathMethodName: 'getBasePath',
17+
localReducer: animalComponentReducer,
18+
})
1219
@Component({
1320
selector: 'zoo-animal',
1421
templateUrl: './component.html',
1522
styleUrls: ['./component.css'],
1623
changeDetection: ChangeDetectionStrategy.OnPush,
1724
})
18-
export class AnimalComponent implements OnInit {
25+
export class AnimalComponent {
1926
static readonly ADD_TICKET = 'ADD_TICKET';
2027
static readonly REMOVE_TICKET = 'REMOVE_TICKET';
2128

2229
@Input() key: string;
2330
@Input() animalType: string;
2431

25-
private subStore: ObservableStore<any>;
26-
27-
name$: Observable<string>;
28-
numTickets$: Observable<number>;
29-
ticketPrice$: Observable<number>;
30-
subTotal$: Observable<number>;
31-
32-
constructor(private ngRedux: NgRedux<IAppState>) {}
33-
34-
addTicket = () => this.subStore.dispatch({ type: 'ADD_TICKET' });
35-
removeTicket = () => this.subStore.dispatch({ type: 'REMOVE_TICKET' });
32+
@select() readonly name$: Observable<string>;
33+
@select('tickets') readonly numTickets$: Observable<number>;
34+
@select('ticketPrice') readonly ticketPrice$: Observable<number>;
35+
@select$(null, toSubTotal) readonly subTotal$: Observable<number>;
3636

37-
// Can't be done in the constructor because it relies on data from the
38-
// inputs.
39-
ngOnInit() {
40-
this.subStore = this.ngRedux.configureSubStore<any>(
41-
[this.animalType, 'items', this.key],
42-
animalComponentReducer);
43-
this.name$ = this.subStore.select('name');
44-
this.numTickets$ = this.subStore.select(['tickets'])
45-
.map(n => n || 0);
46-
this.ticketPrice$ = this.subStore.select('ticketPrice')
47-
.map(n => n || 1);
37+
getBasePath = () => this.key ?
38+
[ this.animalType, 'items', this.key ] :
39+
null;
4840

49-
this.subTotal$ = Observable.combineLatest(
50-
this.numTickets$,
51-
this.ticketPrice$,
52-
(num, price) => num * price);
53-
}
41+
@dispatch() addTicket = () => ({ type: 'ADD_TICKET' });
42+
@dispatch() removeTicket = () => ({ type: 'REMOVE_TICKET' });
5443
}

src/app/animals/model.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface IAnimal {
1111
animalType: AnimalType;
1212
name: string;
1313
ticketPrice: number;
14+
tickets: number;
1415
}
1516

1617
export interface IAnimalList {
@@ -23,5 +24,6 @@ export const fromServer = (record: any): IAnimal => ({
2324
id: record.name.toLowerCase(),
2425
animalType: record.animalType,
2526
name: record.name,
26-
ticketPrice: record.ticketPrice,
27+
ticketPrice: record.ticketPrice || 0,
28+
tickets: record.tickets || 0,
2729
});

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
version "6.3.0"
1313
resolved "https://registry.yarnpkg.com/@angular-redux/router/-/router-6.3.0.tgz#0ece17ad5a4eb8a40f54d7e3a67e67598f03d2cd"
1414

15-
"@angular-redux/store@^6.4.1":
16-
version "6.4.1"
17-
resolved "https://registry.yarnpkg.com/@angular-redux/store/-/store-6.4.1.tgz#40c9f001a4f09a9caa02310341c702da13e8755b"
15+
"@angular-redux/store@6.5.0":
16+
version "6.5.0"
17+
resolved "https://registry.yarnpkg.com/@angular-redux/store/-/store-6.5.0.tgz#98b59867d6f1b577f8d20d8d43da981d60c7d5c5"
1818

1919
"@angular/[email protected]":
2020
version "1.0.1"

0 commit comments

Comments
 (0)