lbry-desktop/ui/component/navigationHistory/view.jsx

122 lines
3.3 KiB
React
Raw Permalink 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';
2019-04-01 01:04:01 +02:00
import NavigationHistoryItem from 'component/navigationHistoryItem';
2019-05-07 04:35:04 +02:00
import Paginate from 'component/common/paginate';
2018-08-30 07:11:05 +02:00
type HistoryItem = {
uri: string,
lastViewed: number,
};
type Props = {
2019-04-04 23:05:23 +02:00
historyItems: Array<HistoryItem>,
2018-08-30 07:11:05 +02:00
pageCount: number,
2018-09-04 19:18:11 +02:00
clearHistoryUri: string => void,
2018-08-30 07:11:05 +02:00
};
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
selectAll() {
2019-04-04 23:05:23 +02:00
const { historyItems } = this.props;
2018-08-30 07:11:05 +02:00
const newSelectedState = {};
2019-04-04 23:05:23 +02:00
historyItems.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() {
2019-05-07 04:35:04 +02:00
const { historyItems = [], pageCount } = this.props;
2018-08-30 07:11:05 +02:00
const { itemsSelected } = this.state;
2019-05-07 04:35:04 +02:00
const allSelected = Object.keys(itemsSelected).length === historyItems.length;
2018-08-30 07:11:05 +02:00
const selectHandler = allSelected ? this.unselectAll : this.selectAll;
2019-05-07 04:35:04 +02:00
return historyItems.length ? (
2018-08-30 07:11:05 +02:00
<React.Fragment>
2019-07-21 23:31:22 +02:00
<div className="card__actions">
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} />
) : (
2019-05-07 04:35:04 +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
)}
2019-05-07 04:35:04 +02:00
<Button button="link" label={allSelected ? __('Cancel') : __('Select All')} onClick={selectHandler} />
2018-08-30 07:11:05 +02:00
</div>
2019-04-04 23:05:23 +02:00
{!!historyItems.length && (
2019-07-21 23:31:22 +02:00
<section className="card item-list">
2019-04-04 23:05:23 +02:00
{historyItems.map(item => (
2019-04-01 01:04:01 +02:00
<NavigationHistoryItem
2018-10-04 06:11:08 +02:00
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
)}
2019-05-07 04:35:04 +02:00
<Paginate totalPages={pageCount} />
2018-08-30 07:11:05 +02:00
</React.Fragment>
) : (
<div className="main--empty">
<section className="card card--section">
2019-11-22 22:13:00 +01:00
<h2 className="card__title card__title--deprecated">
{__('Your history is empty, what are you doing here?')}
</h2>
2019-07-21 23:31:22 +02:00
<div className="card__actions card__actions--center">
<Button button="primary" navigate="/" label={__('Explore new content')} />
</div>
</section>
</div>
2018-08-30 07:11:05 +02:00
);
}
}
2019-05-07 04:35:04 +02:00
export default UserHistoryPage;