From 86a40eacbcc71f3e640a5fab3e3b1c1ce32809cf Mon Sep 17 00:00:00 2001 From: Travis Eden Date: Wed, 1 Aug 2018 10:06:43 -0400 Subject: [PATCH] add UserHistoryPage --- src/renderer/component/router/view.jsx | 2 + src/renderer/page/userHistory/index.js | 24 +++++++ src/renderer/page/userHistory/view.jsx | 91 +++++++++++++++++++++++++ src/renderer/redux/actions/content.js | 11 ++- src/renderer/redux/selectors/content.js | 13 ++-- 5 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 src/renderer/page/userHistory/index.js create mode 100644 src/renderer/page/userHistory/view.jsx diff --git a/src/renderer/component/router/view.jsx b/src/renderer/component/router/view.jsx index fa1a0fea3..c6cb09026 100644 --- a/src/renderer/component/router/view.jsx +++ b/src/renderer/component/router/view.jsx @@ -18,6 +18,7 @@ import InvitePage from 'page/invite'; import BackupPage from 'page/backup'; import SubscriptionsPage from 'page/subscriptions'; import SearchPage from 'page/search'; +import UserHistoryPage from 'page/userHistory'; const route = (props, page, routesMap) => { const component = routesMap[page]; @@ -53,6 +54,7 @@ const Router = props => { wallet: , subscriptions: , search: , + user_history: , }); }; diff --git a/src/renderer/page/userHistory/index.js b/src/renderer/page/userHistory/index.js new file mode 100644 index 000000000..a922bb58c --- /dev/null +++ b/src/renderer/page/userHistory/index.js @@ -0,0 +1,24 @@ +import { connect } from 'react-redux'; +import { selectHistoryPageCount, makeSelectHistoryForPage } from 'redux/selectors/content'; +import { doNavigate } from 'redux/actions/navigation'; +import { selectCurrentParams, makeSelectCurrentParam } from 'lbry-redux'; +import UserHistoryPage from './view'; + +const select = state => { + const paramPage = makeSelectCurrentParam('page')(state) || 0; + return { + pageCount: selectHistoryPageCount(state), + page: paramPage, + params: selectCurrentParams(state), + history: makeSelectHistoryForPage(paramPage)(state), + }; +}; + +const perform = dispatch => ({ + navigate: (path, params) => dispatch(doNavigate(path, params)), +}); + +export default connect( + select, + perform +)(UserHistoryPage); diff --git a/src/renderer/page/userHistory/view.jsx b/src/renderer/page/userHistory/view.jsx new file mode 100644 index 000000000..13b9efc50 --- /dev/null +++ b/src/renderer/page/userHistory/view.jsx @@ -0,0 +1,91 @@ +// @flow +import React from 'react'; +import FileCard from 'component/fileCard'; +import Page from 'component/page'; +import Button from 'component/button'; +import { FormField, FormRow } from 'component/common/form'; +import ReactPaginate from 'react-paginate'; + +type HistoryItem = { + uri: string, + lastViewed: number, +}; + +type Props = { + history: Array, + page: number, + pageCount: number, + navigate: string => void, + params: { page: number }, +}; + +class UserHistoryPage extends React.PureComponent { + 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); + } + } + + render() { + const { history, page, pageCount, navigate } = this.props; + + return ( + +
+ {history && history.length ? ( + + {history.map(item => )} + {pageCount > 1 && ( + + this.changePage(e.selected)} + forcePage={page} + initialPage={page} + containerClassName="pagination" + /> + + this.paginate(e)} + prefix={__('Go to page:')} + type="text" + /> + + )} + + ) : ( +

+ {__('You have no saved history. Go')}{' '} +

+
+ ); + } +} +export default UserHistoryPage; diff --git a/src/renderer/redux/actions/content.js b/src/renderer/redux/actions/content.js index e9d98817f..b8073d820 100644 --- a/src/renderer/redux/actions/content.js +++ b/src/renderer/redux/actions/content.js @@ -523,8 +523,17 @@ export function doClearContentHistoryUri(uri: string) { }; } -export function doSetContentHistoryAll() { +export function doClearContentHistoryAll() { return dispatch => { dispatch({ type: ACTIONS.CLEAR_CONTENT_HISTORY_ALL }); }; } + +export function doSetHistoryPage(page) { + return dispatch => { + dispatch({ + type: ACTIONS.SET_CONTENT_HISTORY_PAGE, + data: { page }, + }); + }; +} diff --git a/src/renderer/redux/selectors/content.js b/src/renderer/redux/selectors/content.js index 0f77fc811..8c8c11fbe 100644 --- a/src/renderer/redux/selectors/content.js +++ b/src/renderer/redux/selectors/content.js @@ -42,10 +42,15 @@ export const makeSelectContentPositionForUri = uri => return state.positions[id] ? state.positions[id][outpoint] : null; }); -export const makeSelectHistoryForPage = (page = 1) => - createSelector(selectState, state => - state.history.slice((page - 1) * HISTORY_ITEMS_PER_PAGE, HISTORY_ITEMS_PER_PAGE) - ); +export const selectHistoryPageCount = createSelector(selectState, state => + Math.ceil(state.history.length / HISTORY_ITEMS_PER_PAGE) +); + +export const makeSelectHistoryForPage = page => + createSelector(selectState, state => { + const left = page * HISTORY_ITEMS_PER_PAGE; + return state.history.slice(left, left + HISTORY_ITEMS_PER_PAGE); + }); export const makeSelectHistoryForUri = uri => createSelector(selectState, state => state.history.find(i => i.uri === uri));