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-02-13 17:27:20 +01:00
|
|
|
|
import { Form, FormField } from 'component/common/form';
|
2018-08-30 07:11:05 +02:00
|
|
|
|
import ReactPaginate from 'react-paginate';
|
|
|
|
|
import UserHistoryItem from 'component/userHistoryItem';
|
2019-03-29 15:23:32 +01:00
|
|
|
|
import { navigate } from '@reach/router';
|
2018-08-30 07:11:05 +02:00
|
|
|
|
|
|
|
|
|
type HistoryItem = {
|
|
|
|
|
uri: string,
|
|
|
|
|
lastViewed: number,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
history: Array<HistoryItem>,
|
|
|
|
|
page: number,
|
|
|
|
|
pageCount: number,
|
2018-09-04 19:18:11 +02:00
|
|
|
|
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) {
|
2019-03-29 15:23:32 +01:00
|
|
|
|
console.log('new', pageNumber);
|
|
|
|
|
navigate(`/$/user_history?page=${pageNumber}`);
|
2018-08-30 07:11:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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() {
|
2019-03-28 17:53:13 +01:00
|
|
|
|
const { history = [], page, pageCount } = this.props;
|
2019-03-29 15:23:32 +01:00
|
|
|
|
console.log('this.props', this.props);
|
2018-08-30 07:11:05 +02:00
|
|
|
|
const { itemsSelected } = this.state;
|
|
|
|
|
const allSelected = Object.keys(itemsSelected).length === history.length;
|
|
|
|
|
const selectHandler = allSelected ? this.unselectAll : this.selectAll;
|
2018-10-05 05:47:54 +02:00
|
|
|
|
|
|
|
|
|
return history.length ? (
|
2018-08-30 07:11:05 +02:00
|
|
|
|
<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 && (
|
2019-02-20 06:20:29 +01:00
|
|
|
|
<section className="card__content item-list">
|
2018-10-04 06:11:08 +02:00
|
|
|
|
{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
|
|
|
|
)}
|
2019-02-20 06:20:29 +01:00
|
|
|
|
{pageCount > 1 && (
|
2019-02-13 17:27:20 +01:00
|
|
|
|
<Form>
|
|
|
|
|
<fieldset-group class="fieldset-group--smushed fieldgroup--paginate">
|
|
|
|
|
<fieldset-section>
|
|
|
|
|
<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}
|
2019-03-29 15:23:32 +01:00
|
|
|
|
disableInitialCallback
|
2019-02-13 17:27:20 +01:00
|
|
|
|
containerClassName="pagination"
|
|
|
|
|
/>
|
|
|
|
|
</fieldset-section>
|
|
|
|
|
<FormField
|
2019-02-20 06:20:29 +01:00
|
|
|
|
type="text"
|
|
|
|
|
name="paginate-input"
|
|
|
|
|
label={__('Go to page:')}
|
2019-02-13 17:27:20 +01:00
|
|
|
|
className="paginate-channel"
|
|
|
|
|
onKeyUp={e => this.paginate(e)}
|
|
|
|
|
/>
|
|
|
|
|
</fieldset-group>
|
|
|
|
|
</Form>
|
2018-08-31 05:45:49 +02:00
|
|
|
|
)}
|
2018-08-30 07:11:05 +02:00
|
|
|
|
</React.Fragment>
|
2018-10-05 05:47:54 +02:00
|
|
|
|
) : (
|
|
|
|
|
<div className="page__empty">
|
2018-12-19 06:44:53 +01:00
|
|
|
|
<section className="card card--section">
|
|
|
|
|
<header className="card__header">
|
|
|
|
|
<h2 className="card__title">
|
|
|
|
|
{__(
|
|
|
|
|
"You don't have anything saved in history yet, go check out some content on LBRY!"
|
|
|
|
|
)}
|
|
|
|
|
</h2>
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
<div className="card__content">
|
|
|
|
|
<div className="card__actions card__actions--center">
|
2019-03-28 17:53:13 +01:00
|
|
|
|
<Button button="primary" navigate="/" label={__('Explore new content')} />
|
2018-12-19 06:44:53 +01:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
2018-10-05 05:47:54 +02:00
|
|
|
|
</div>
|
2018-08-30 07:11:05 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default UserHistoryPage;
|