Skip to content

Commit f0c2bb2

Browse files
author
Pietro Grandi
committed
reset again, back to simplest case
1 parent 66932b8 commit f0c2bb2

File tree

5 files changed

+95
-11
lines changed

5 files changed

+95
-11
lines changed

app/app.component.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1-
import { Component } from '@angular/core';
1+
import {Component, OnInit} from '@angular/core';
2+
import {SearchBox} from "./components/search-box/search-box.component";
3+
import {CurrentSearch, searchReducer} from "./components/youtube-search/search.reducer";
4+
import {Store, provideStore} from "@ngrx/store";
5+
import {Observable} from "rxjs/Rx";
26

37
@Component({
4-
58
selector: 'my-app',
6-
7-
template: `<h1>{{title}}</h1>`,
9+
providers: [
10+
provideStore({ currentSearch: searchReducer })
11+
],
12+
directives: [ SearchBox ],
13+
template: `
14+
<h1>{{title}}</h1>
15+
<div class="row">
16+
<search-box [store]="store"></search-box>
17+
</div>
18+
<div class="row">
19+
<courses-list [searchResults]="results"></courses-list>
20+
</div>
21+
`
822

923
})
1024

11-
export class AppComponent {
25+
export class AppComponent implements OnInit {
1226

1327
title = 'One Source of Truth for Angular 2';
1428

29+
private currentSearch: Observable<CurrentSearch>;
30+
31+
constructor(
32+
private store: Store<CurrentSearch>
33+
) {
34+
this.currentSearch = this.store.select<CurrentSearch>('currentSearch');
35+
}
36+
37+
ngOnInit() {
38+
this.currentSearch.subscribe((state: CurrentSearch) => {
39+
console.log(state);
40+
});
41+
}
42+
1543
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {Observable} from "rxjs/Rx";
2+
import {ElementRef, OnInit, Component, Input} from "@angular/core";
3+
import {CurrentSearch} from "../youtube-search/search.reducer";
4+
import {Store} from "@ngrx/store";
5+
6+
@Component({
7+
inputs: ['store'],
8+
selector: 'search-box',
9+
template: `
10+
<input type="text" class="form-control" placeholder="Search" autofocus>
11+
`
12+
})
13+
14+
export class SearchBox implements OnInit {
15+
16+
@Input()
17+
store: Store<any>;
18+
19+
constructor(private el: ElementRef) {}
20+
21+
ngOnInit(): void {
22+
Observable.fromEvent(this.el.nativeElement, 'keyup')
23+
.map((e: any) => e.target.value)
24+
.filter((text: string) => text.length > 3 || text.length == 0)
25+
.debounceTime(250)
26+
.subscribe((text: string) =>
27+
this.store.dispatch({
28+
type: 'TEXT',
29+
payload: {
30+
text: text
31+
}
32+
})
33+
);
34+
}
35+
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ActionReducer, Action } from '@ngrx/store';
2+
3+
export interface CurrentSearch {
4+
text: string
5+
}
6+
7+
export const searchReducer: ActionReducer<CurrentSearch> = (state: CurrentSearch, action: Action) => {
8+
switch (action.type) {
9+
case 'TEXT':
10+
const update = { text: action.payload.text };
11+
return Object.assign({}, state, update);
12+
default:
13+
return state;
14+
}
15+
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
"@angular/http": "2.0.0-rc.2",
2121
"@angular/platform-browser": "2.0.0-rc.2",
2222
"@angular/platform-browser-dynamic": "2.0.0-rc.2",
23-
"systemjs": "0.19.27",
23+
"@ngrx/core": "^1.0.0",
24+
"@ngrx/store": "^2.0.0",
2425
"es6-shim": "^0.35.0",
26+
"systemjs": "0.19.27",
2527
"reflect-metadata": "^0.1.3",
2628
"rxjs": "5.0.0-beta.6",
2729
"zone.js": "^0.6.12"

systemjs.config.js

100644100755
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
(function(global) {
22
// map tells the System loader where to look for things
33
var map = {
4-
'app': 'app',
5-
'rxjs': 'node_modules/rxjs',
4+
'app': 'app',
5+
'@angular': 'node_modules/@angular',
66
'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
7-
'@angular': 'node_modules/@angular'
7+
'rxjs': 'node_modules/rxjs',
8+
'@ngrx': 'node_modules/@ngrx'
89
};
910
// packages tells the System loader how to load when no filename and/or no extension
1011
var packages = {
@@ -19,7 +20,9 @@
1920
'@angular/platform-browser',
2021
'@angular/platform-browser-dynamic',
2122
'@angular/testing',
22-
'@angular/upgrade'
23+
'@angular/upgrade',
24+
'@ngrx/core',
25+
'@ngrx/store'
2326
];
2427
// add package entries for angular packages in the form '@angular/common': { main: 'index.js', defaultExtension: 'js' }
2528
packageNames.forEach(function(pkgName) {
@@ -28,6 +31,6 @@
2831
var config = {
2932
map: map,
3033
packages: packages
31-
}
34+
};
3235
System.config(config);
3336
})(this);

0 commit comments

Comments
 (0)