|
2 | 2 | // I have my normal class component CLIENT SIDE |
3 | 3 | import React from 'react'; |
4 | 4 | import PropTypes from 'prop-types'; |
| 5 | +import pickBy from 'lodash.pickby'; |
5 | 6 |
|
6 | 7 | import ArticleList from './ArticleList'; |
| 8 | +import SearchBar from './SearchBar'; |
| 9 | +import Timestamp from './Timestamp'; |
7 | 10 |
|
8 | 11 | class App extends React.Component { |
9 | 12 |
|
10 | 13 | //****** I need the following lines to expose the CONTEXT |
11 | 14 | //to make the context api work we need to define the context type |
12 | | - static childContextTypes={ |
| 15 | + static childContextTypes = { |
13 | 16 | store: PropTypes.object |
14 | 17 | }; |
| 18 | + |
15 | 19 | //Getting the context |
16 | | - getChildContext(){ |
| 20 | + getChildContext() { |
17 | 21 | return { |
18 | | - store:this.props.store}; |
| 22 | + store: this.props.store |
| 23 | + }; |
19 | 24 | } |
| 25 | + |
20 | 26 | //******** |
21 | 27 |
|
22 | | - state= this.props.store.getState(); |
| 28 | + state = this.props.store.getState(); |
| 29 | + |
| 30 | + onStoreChange=()=>{ |
| 31 | + this.setState(this.props.store.getState); |
| 32 | + } |
| 33 | + |
| 34 | + componentDidMount(){ |
| 35 | + //we need to update the component state so the app component state is in sync to the store state |
| 36 | + if(this.subsciptionId) { |
| 37 | + this.subsciptionId = this.props.store.subscribe(this.onStoreChange); |
| 38 | + } |
| 39 | + this.props.store.startClock(); |
| 40 | + } |
| 41 | + |
| 42 | + componentWillUnmount(){ |
| 43 | + this.props.store.unsubscribe(this.subscriptionId); |
| 44 | + this.subscriptionId = null; |
| 45 | + } |
| 46 | + |
| 47 | + //In react we cannot return two elements unless |
| 48 | + //Option 1. is in an array so we need to pass a Key elements |
| 49 | + // render() { |
| 50 | + // return [ |
| 51 | + // <SearchBar key="searchBar"/>, |
| 52 | + // <ArticleList key="articleList" |
| 53 | + // articles={this.state.articles} |
| 54 | + // store={this.props.store} |
| 55 | + // /> |
| 56 | + // ]; |
| 57 | + // } |
| 58 | + |
| 59 | + //Option 2. Wrap those components in a div |
| 60 | + // return ( |
| 61 | + // <div> |
| 62 | + // <SearchBar key="searchBar" doSearch={this.props.store.setSearchTerm}/> |
| 63 | + // <ArticleList key="articleList" articles={articles} |
| 64 | + // store={this.props.store}/> |
| 65 | + // </div> |
| 66 | + // ); |
| 67 | + |
| 68 | + |
| 69 | + // We can just update when the store state changes..this means we need to subscribe |
| 70 | + // with a callback |
| 71 | + // we can make a StateApi an event emmiter (Flux) |
| 72 | + // or manage subscriptions in the same object |
23 | 73 |
|
24 | 74 | render() { |
| 75 | + let { articles , searchTerm}=this.state; |
| 76 | + //making the search case insensitive |
| 77 | + const searchRegex = new RegExp(searchTerm, 'i'); |
| 78 | + if(searchTerm){ |
| 79 | + articles= pickBy(articles, (value) => { |
| 80 | + return value.title.match(searchRegex) || |
| 81 | + value.body.match(searchRegex); |
| 82 | + }); |
| 83 | + } |
25 | 84 | return ( |
26 | | - <ArticleList |
27 | | - articles={this.state.articles} |
28 | | - store={this.props.store} |
29 | | - /> |
| 85 | + <div> |
| 86 | + <Timestamp /> |
| 87 | + <SearchBar key="searchBar" /> |
| 88 | + <ArticleList key="articleList" |
| 89 | + articles={articles} |
| 90 | + /> |
| 91 | + </div> |
30 | 92 | ); |
31 | 93 | } |
32 | 94 | } |
|
0 commit comments