|
1 | | -import React, { PropTypes, Component } from 'react' |
2 | | -import classnames from 'classnames' |
| 1 | +import React, { PropTypes } from 'react' |
| 2 | +import { connect } from 'react-redux' |
| 3 | +import FilterLink from './FilterLink' |
| 4 | +import { getCompletedCount, getListedCount } from '../reducers' |
| 5 | +import { clearCompleted } from '../actions' |
3 | 6 | import { SHOW_ALL, SHOW_COMPLETED, SHOW_ACTIVE } from '../constants/TodoFilters' |
4 | 7 |
|
5 | | -const FILTER_TITLES = { |
6 | | - [SHOW_ALL]: 'All', |
7 | | - [SHOW_ACTIVE]: 'Active', |
8 | | - [SHOW_COMPLETED]: 'Completed' |
9 | | -} |
10 | | - |
11 | | -class Footer extends Component { |
12 | | - renderTodoCount() { |
13 | | - const { activeCount } = this.props |
14 | | - const itemWord = activeCount === 1 ? 'item' : 'items' |
15 | | - |
16 | | - return ( |
17 | | - <span className="todo-count"> |
18 | | - <strong>{activeCount || 'No'}</strong> {itemWord} left |
19 | | - </span> |
20 | | - ) |
21 | | - } |
22 | | - |
23 | | - renderFilterLink(filter) { |
24 | | - const title = FILTER_TITLES[filter] |
25 | | - const { filter: selectedFilter, onShow } = this.props |
26 | | - |
27 | | - return ( |
28 | | - <a className={classnames({ selected: filter === selectedFilter })} |
29 | | - style={{ cursor: 'pointer' }} |
30 | | - onClick={() => onShow(filter)}> |
31 | | - {title} |
32 | | - </a> |
33 | | - ) |
34 | | - } |
35 | | - |
36 | | - renderClearButton() { |
37 | | - const { completedCount, onClearCompleted } = this.props |
38 | | - if (completedCount > 0) { |
39 | | - return ( |
40 | | - <button className="clear-completed" |
41 | | - onClick={onClearCompleted} > |
42 | | - Clear completed |
43 | | - </button> |
44 | | - ) |
45 | | - } |
46 | | - } |
47 | | - |
48 | | - render() { |
49 | | - return ( |
50 | | - <footer className="footer"> |
51 | | - {this.renderTodoCount()} |
52 | | - <ul className="filters"> |
53 | | - {[ SHOW_ALL, SHOW_ACTIVE, SHOW_COMPLETED ].map(filter => |
54 | | - <li key={filter}> |
55 | | - {this.renderFilterLink(filter)} |
56 | | - </li> |
57 | | - )} |
58 | | - </ul> |
59 | | - {this.renderClearButton()} |
60 | | - </footer> |
61 | | - ) |
62 | | - } |
63 | | -} |
64 | | - |
| 8 | +const TodoCount = ({ activeCount }) => ( |
| 9 | + <span className="todo-count"> |
| 10 | + <strong>{activeCount || 'No'}</strong> |
| 11 | + {' '} |
| 12 | + {activeCount === 1 ? 'item' : 'items'} left |
| 13 | + </span> |
| 14 | +) |
| 15 | + |
| 16 | +const ClearButton = ({ completedCount, clearCompleted }) => ( |
| 17 | + <button className="clear-completed" |
| 18 | + onClick={clearCompleted} > |
| 19 | + Clear completed |
| 20 | + </button> |
| 21 | +) |
| 22 | + |
| 23 | +const Footer = ({ filter, completedCount, listedCount, clearCompleted }) => ( |
| 24 | + listedCount ? ( |
| 25 | + <footer className="footer"> |
| 26 | + <TodoCount activeCount={listedCount - completedCount} /> |
| 27 | + <ul className="filters"> |
| 28 | + {[ SHOW_ALL, SHOW_ACTIVE, SHOW_COMPLETED ].map(filter => |
| 29 | + <li key={filter}> |
| 30 | + <FilterLink filter={filter} /> |
| 31 | + </li> |
| 32 | + )} |
| 33 | + </ul> |
| 34 | + {completedCount > 0 && |
| 35 | + <ClearButton |
| 36 | + completedCount={completedCount} |
| 37 | + clearCompleted={clearCompleted} |
| 38 | + /> |
| 39 | + } |
| 40 | + </footer> |
| 41 | + ) : ( |
| 42 | + <span /> |
| 43 | + ) |
| 44 | +) |
65 | 45 | Footer.propTypes = { |
66 | | - completedCount: PropTypes.number.isRequired, |
67 | | - activeCount: PropTypes.number.isRequired, |
68 | 46 | filter: PropTypes.string.isRequired, |
69 | | - onClearCompleted: PropTypes.func.isRequired, |
70 | | - onShow: PropTypes.func.isRequired |
| 47 | + listedCount: PropTypes.number.isRequired, |
| 48 | + completedCount: PropTypes.number.isRequired, |
71 | 49 | } |
72 | 50 |
|
73 | | -export default Footer |
| 51 | +const mapStateToProps = (state) => ({ |
| 52 | + filter: state.filter, |
| 53 | + listedCount: getListedCount(state), |
| 54 | + completedCount: getCompletedCount(state), |
| 55 | +}) |
| 56 | + |
| 57 | +export default connect( |
| 58 | + mapStateToProps, |
| 59 | + { clearCompleted } |
| 60 | +)(Footer) |
0 commit comments