2020-09-10 18:45:02 +03:00
|
|
|
import React, { Component, MouseEventHandler } from 'react';
|
2018-12-22 17:20:04 +03:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
2020-09-10 18:45:02 +03:00
|
|
|
import {
|
|
|
|
SHOW_ALL,
|
|
|
|
SHOW_MARKED,
|
|
|
|
SHOW_UNMARKED,
|
|
|
|
TodoFilter,
|
|
|
|
} from '../constants/TodoFilters';
|
2018-12-22 17:20:04 +03:00
|
|
|
|
|
|
|
const FILTER_TITLES = {
|
|
|
|
[SHOW_ALL]: 'All',
|
|
|
|
[SHOW_UNMARKED]: 'Active',
|
2020-08-08 23:26:39 +03:00
|
|
|
[SHOW_MARKED]: 'Completed',
|
2018-12-22 17:20:04 +03:00
|
|
|
};
|
|
|
|
|
2020-09-10 18:45:02 +03:00
|
|
|
interface Props {
|
|
|
|
markedCount: number;
|
|
|
|
unmarkedCount: number;
|
|
|
|
filter: TodoFilter;
|
|
|
|
onClearMarked: MouseEventHandler<HTMLButtonElement>;
|
|
|
|
onShow: (filter: TodoFilter) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Footer extends Component<Props> {
|
2018-12-22 17:20:04 +03:00
|
|
|
static propTypes = {
|
|
|
|
markedCount: PropTypes.number.isRequired,
|
|
|
|
unmarkedCount: PropTypes.number.isRequired,
|
|
|
|
filter: PropTypes.string.isRequired,
|
|
|
|
onClearMarked: PropTypes.func.isRequired,
|
2020-08-08 23:26:39 +03:00
|
|
|
onShow: PropTypes.func.isRequired,
|
2018-12-22 17:20:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2019-01-10 20:23:33 +03:00
|
|
|
<footer className="footer">
|
2018-12-22 17:20:04 +03:00
|
|
|
{this.renderTodoCount()}
|
2019-01-10 20:23:33 +03:00
|
|
|
<ul className="filters">
|
2020-09-10 18:45:02 +03:00
|
|
|
{([SHOW_ALL, SHOW_UNMARKED, SHOW_MARKED] as const).map((filter) => (
|
2018-12-22 17:20:04 +03:00
|
|
|
<li key={filter}>{this.renderFilterLink(filter)}</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
{this.renderClearButton()}
|
|
|
|
</footer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTodoCount() {
|
|
|
|
const { unmarkedCount } = this.props;
|
|
|
|
const itemWord = unmarkedCount === 1 ? 'item' : 'items';
|
|
|
|
|
|
|
|
return (
|
2019-01-10 20:23:33 +03:00
|
|
|
<span className="todo-count">
|
2018-12-22 17:20:04 +03:00
|
|
|
<strong>{unmarkedCount || 'No'}</strong> {itemWord} left
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-10 18:45:02 +03:00
|
|
|
renderFilterLink(filter: TodoFilter) {
|
2018-12-22 17:20:04 +03:00
|
|
|
const title = FILTER_TITLES[filter];
|
|
|
|
const { filter: selectedFilter, onShow } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<a
|
|
|
|
className={classnames({ selected: filter === selectedFilter })}
|
|
|
|
style={{ cursor: 'hand' }}
|
|
|
|
onClick={() => onShow(filter)}
|
|
|
|
>
|
|
|
|
{title}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderClearButton() {
|
|
|
|
const { markedCount, onClearMarked } = this.props;
|
|
|
|
if (markedCount > 0) {
|
|
|
|
return (
|
2019-01-10 20:23:33 +03:00
|
|
|
<button className="clear-completed" onClick={onClearMarked}>
|
2018-12-22 17:20:04 +03:00
|
|
|
Clear completed
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|