lbry-desktop/src/renderer/component/userHistory/view.jsx

166 lines
4.5 KiB
React
Raw Normal View History

2018-08-30 07:11:05 +02:00
// @flow
2018-09-04 19:18:11 +02:00
import * as React from 'react';
2018-08-30 07:11:05 +02:00
import Button from 'component/button';
import { FormField, FormRow } from 'component/common/form';
import ReactPaginate from 'react-paginate';
import UserHistoryItem from 'component/userHistoryItem';
type HistoryItem = {
uri: string,
lastViewed: number,
};
type Props = {
history: Array<HistoryItem>,
page: number,
pageCount: number,
2018-09-04 19:18:11 +02:00
navigate: (string, {}) => void,
clearHistoryUri: string => void,
2018-08-30 07:11:05 +02:00
params: { page: number },
};
2018-09-04 19:18:11 +02:00
type State = {
itemsSelected: {},
};
class UserHistoryPage extends React.PureComponent<Props, State> {
2018-08-30 07:11:05 +02:00
constructor() {
super();
2018-08-31 05:45:49 +02:00
2018-08-30 07:11:05 +02:00
this.state = {
itemsSelected: {},
2018-08-31 05:45:49 +02:00
};
2018-09-04 19:18:11 +02:00
(this: any).selectAll = this.selectAll.bind(this);
(this: any).unselectAll = this.unselectAll.bind(this);
(this: any).removeSelected = this.removeSelected.bind(this);
}
onSelect(uri: string) {
const { itemsSelected } = this.state;
const newItemsSelected = { ...itemsSelected };
if (itemsSelected[uri]) {
delete newItemsSelected[uri];
} else {
newItemsSelected[uri] = true;
}
this.setState({
itemsSelected: { ...newItemsSelected },
});
2018-08-30 07:11:05 +02:00
}
2018-08-31 05:45:49 +02:00
2018-08-30 07:11:05 +02:00
changePage(pageNumber: number) {
const { params } = this.props;
const newParams = { ...params, page: pageNumber };
this.props.navigate('/user_history', newParams);
}
paginate(e: SyntheticKeyboardEvent<*>) {
const pageFromInput = Number(e.currentTarget.value);
if (
pageFromInput &&
e.keyCode === 13 &&
!Number.isNaN(pageFromInput) &&
pageFromInput > 0 &&
pageFromInput <= this.props.pageCount
) {
this.changePage(pageFromInput);
}
}
2018-08-31 05:45:49 +02:00
2018-08-30 07:11:05 +02:00
selectAll() {
const { history } = this.props;
const newSelectedState = {};
2018-08-31 05:45:49 +02:00
history.forEach(({ uri }) => (newSelectedState[uri] = true));
2018-08-30 07:11:05 +02:00
this.setState({ itemsSelected: newSelectedState });
}
2018-08-31 05:45:49 +02:00
2018-08-30 07:11:05 +02:00
unselectAll() {
this.setState({
2018-08-31 05:45:49 +02:00
itemsSelected: {},
});
2018-08-30 07:11:05 +02:00
}
2018-08-31 05:45:49 +02:00
2018-08-30 07:11:05 +02:00
removeSelected() {
const { clearHistoryUri } = this.props;
const { itemsSelected } = this.state;
2018-08-31 05:45:49 +02:00
2018-08-30 07:11:05 +02:00
Object.keys(itemsSelected).forEach(uri => clearHistoryUri(uri));
2018-08-31 05:45:49 +02:00
this.setState({
itemsSelected: {},
});
2018-08-30 07:11:05 +02:00
}
render() {
2018-09-04 19:18:11 +02:00
const { history, page, pageCount } = this.props;
2018-08-30 07:11:05 +02:00
const { itemsSelected } = this.state;
2018-08-31 05:45:49 +02:00
2018-08-30 07:11:05 +02:00
const allSelected = Object.keys(itemsSelected).length === history.length;
const selectHandler = allSelected ? this.unselectAll : this.selectAll;
return (
<React.Fragment>
2018-08-31 05:45:49 +02:00
<div className="card__actions card__actions--between">
2018-09-04 19:18:11 +02:00
{Object.keys(itemsSelected).length ? (
2018-08-31 05:45:49 +02:00
<Button button="link" label={__('Delete')} onClick={this.removeSelected} />
) : (
2018-09-04 19:18:11 +02:00
<span>
{/* Using an empty span so spacing stays the same if the button isn't rendered */}
</span>
2018-08-31 05:45:49 +02:00
)}
2018-08-30 07:11:05 +02:00
<Button
button="link"
2018-08-31 05:45:49 +02:00
label={allSelected ? __('Cancel') : __('Select All')}
onClick={selectHandler}
/>
2018-08-30 07:11:05 +02:00
</div>
{!!history.length && (
2018-10-04 06:11:08 +02:00
<section className="item-list">
{history.map(item => (
<UserHistoryItem
key={item.uri}
uri={item.uri}
lastViewed={item.lastViewed}
selected={!!itemsSelected[item.uri]}
onSelect={() => {
this.onSelect(item.uri);
}}
/>
))}
</section>
2018-08-30 07:11:05 +02:00
)}
2018-08-31 05:45:49 +02:00
{pageCount > 1 && (
2018-09-04 19:18:11 +02:00
<FormRow padded verticallyCentered centered>
2018-08-31 05:45:49 +02:00
<ReactPaginate
pageCount={pageCount}
pageRangeDisplayed={2}
previousLabel=""
nextLabel=""
activeClassName="pagination__item--selected"
pageClassName="pagination__item"
previousClassName="pagination__item pagination__item--previous"
nextClassName="pagination__item pagination__item--next"
breakClassName="pagination__item pagination__item--break"
marginPagesDisplayed={2}
onPageChange={e => this.changePage(e.selected)}
forcePage={page}
initialPage={page}
containerClassName="pagination"
/>
<FormField
className="paginate-channel"
onKeyUp={e => this.paginate(e)}
prefix={__('Go to page:')}
type="text"
/>
</FormRow>
)}
2018-08-30 07:11:05 +02:00
</React.Fragment>
);
}
}
export default UserHistoryPage;