@@ -4,29 +4,30 @@ import SearchBar from './search_bar.js';
4
4
5
5
require ( 'isomorphic-fetch' ) ;
6
6
7
- var reduxStore = require ( '../../store.js' ) ;
8
-
9
7
10
8
// A component that stores data (as its internal state).
11
9
// The data is passes to view-only component(s) as properties.
12
10
// The render() method simply sets the properties of view-only components.
13
11
export default class Catalog extends React . Component {
14
12
15
13
16
- constructor ( ) {
17
- super ( ) ;
18
- reduxStore . subscribe ( ( ) => {
14
+ constructor ( props , context ) {
15
+ super ( props , context ) ;
16
+ this . store = this . context . store ;
17
+ this . shouldDisplay = this . shouldDisplay . bind ( this ) ;
18
+
19
+ this . store . subscribe ( ( ) => {
19
20
// FIXME: We need to use render(), not forceUpdate().
20
21
// See comment in src/client.js
21
22
this . forceUpdate ( ) ;
22
23
} ) ;
23
24
24
- this . shouldDisplay = this . shouldDisplay . bind ( this ) ;
25
+
25
26
}
26
27
27
28
28
29
shouldDisplay ( item ) {
29
- let t = reduxStore . getState ( ) . filterText . toLowerCase ( ) ;
30
+ let t = this . store . getState ( ) . filterText . toLowerCase ( ) ;
30
31
return item . brand . toLowerCase ( ) . includes ( t ) ||
31
32
item . name . toLowerCase ( ) . includes ( t ) ;
32
33
}
@@ -36,7 +37,7 @@ export default class Catalog extends React.Component {
36
37
fetch ( this . props . getUrl )
37
38
. then ( res => res . json ( ) )
38
39
. then ( json => {
39
- reduxStore . dispatch ( { type : 'ITEMS_UPDATED' , items : json . items } ) ;
40
+ this . store . dispatch ( { type : 'ITEMS_UPDATED' , items : json . items } ) ;
40
41
} )
41
42
. catch ( err => {
42
43
// TODO: Change the state of this component, to indicate an error
@@ -47,9 +48,9 @@ export default class Catalog extends React.Component {
47
48
48
49
49
50
render ( ) {
50
- let displayedItems = reduxStore . getState ( ) . items . filter ( this . shouldDisplay ) ;
51
+ let displayedItems = this . store . getState ( ) . items . filter ( this . shouldDisplay ) ;
51
52
let onTextChanged = ( t ) => {
52
- reduxStore . dispatch ( { type : 'FILTER_TEXT_CHANGED' , filterText : t } ) ;
53
+ this . store . dispatch ( { type : 'FILTER_TEXT_CHANGED' , filterText : t } ) ;
53
54
} ;
54
55
55
56
return (
@@ -71,3 +72,9 @@ Catalog.propTypes = {
71
72
Catalog . defaultProps = {
72
73
getUrl : '/api/v1/products'
73
74
} ;
75
+
76
+ // This will make this.context available.
77
+ // The store will be provided by the Provider component.
78
+ Catalog . contextTypes = {
79
+ store : React . PropTypes . object . isRequired
80
+ } ;
0 commit comments