|
| 1 | +import React, { Component, PropTypes } from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import { selectReddit, fetchPostsIfNeeded, invalidateReddit } from '../actions'; |
| 4 | +import Picker from '../components/Picker'; |
| 5 | +import Posts from '../components/Posts'; |
| 6 | + |
| 7 | +class AsyncApp extends Component { |
| 8 | + constructor(props) { |
| 9 | + super(props); |
| 10 | + this.handleChange = this.handleChange.bind(this); |
| 11 | + this.handleRefreshClick = this.handleRefreshClick.bind(this); |
| 12 | + } |
| 13 | + |
| 14 | + componentDidMount() { |
| 15 | + const { dispatch, selectedReddit } = this.props; |
| 16 | + dispatch(fetchPostsIfNeeded(selectedReddit)); |
| 17 | + } |
| 18 | + |
| 19 | + componentWillReceiveProps(nextProps) { |
| 20 | + if (nextProps.selectedReddit !== this.props.selectedReddit) { |
| 21 | + const { dispatch, selectedReddit } = nextProps; |
| 22 | + dispatch(fetchPostsIfNeeded(selectedReddit)); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + handleChange(nextReddit) { |
| 27 | + this.props.dispatch(selectReddit(nextReddit)); |
| 28 | + } |
| 29 | + |
| 30 | + handleRefreshClick(e) { |
| 31 | + e.preventDefault(); |
| 32 | + |
| 33 | + const { dispatch, selectedReddit } = this.props; |
| 34 | + dispatch(invalidateReddit(selectedReddit)); |
| 35 | + dispatch(fetchPostsIfNeeded(selectedReddit)); |
| 36 | + } |
| 37 | + |
| 38 | + render () { |
| 39 | + const { selectedReddit, posts, isFetching, lastUpdated } = this.props; |
| 40 | + return ( |
| 41 | + <div> |
| 42 | + <Picker value={selectedReddit} |
| 43 | + onChange={this.handleChange} |
| 44 | + options={['reactjs', 'frontend']} /> |
| 45 | + <p> |
| 46 | + {lastUpdated && |
| 47 | + <span> |
| 48 | + Last updated at {new Date(lastUpdated).toLocaleTimeString()}. |
| 49 | + {' '} |
| 50 | + </span> |
| 51 | + } |
| 52 | + {!isFetching && |
| 53 | + <a href='#' |
| 54 | + onClick={this.handleRefreshClick}> |
| 55 | + Refresh |
| 56 | + </a> |
| 57 | + } |
| 58 | + </p> |
| 59 | + {isFetching && posts.length === 0 && |
| 60 | + <h2>Loading...</h2> |
| 61 | + } |
| 62 | + {!isFetching && posts.length === 0 && |
| 63 | + <h2>Empty.</h2> |
| 64 | + } |
| 65 | + {posts.length > 0 && |
| 66 | + <div style={{ opacity: isFetching ? 0.5 : 1 }}> |
| 67 | + <Posts posts={posts} /> |
| 68 | + </div> |
| 69 | + } |
| 70 | + </div> |
| 71 | + ); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +AsyncApp.propTypes = { |
| 76 | + selectedReddit: PropTypes.string.isRequired, |
| 77 | + posts: PropTypes.array.isRequired, |
| 78 | + isFetching: PropTypes.bool.isRequired, |
| 79 | + lastUpdated: PropTypes.number, |
| 80 | + dispatch: PropTypes.func.isRequired |
| 81 | +}; |
| 82 | + |
| 83 | +function mapStateToProps(state) { |
| 84 | + const { selectedReddit, postsByReddit } = state; |
| 85 | + const { |
| 86 | + isFetching, |
| 87 | + lastUpdated, |
| 88 | + items: posts |
| 89 | + } = postsByReddit[selectedReddit] || { |
| 90 | + isFetching: true, |
| 91 | + items: [] |
| 92 | + }; |
| 93 | + |
| 94 | + return { |
| 95 | + selectedReddit, |
| 96 | + posts, |
| 97 | + isFetching, |
| 98 | + lastUpdated |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +export default connect(mapStateToProps)(AsyncApp); |
0 commit comments