lbry-desktop/src/ui/page/channel/view.jsx

149 lines
4.6 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
2018-11-26 02:21:25 +01:00
import type { Claim } from 'types/claim';
import * as icons from 'constants/icons';
2018-11-26 02:21:25 +01:00
import * as MODALS from 'constants/modal_types';
import React from 'react';
2018-03-26 23:32:43 +02:00
import BusyIndicator from 'component/common/busy-indicator';
2019-02-20 06:20:29 +01:00
import { FormField, Form } from 'component/common/form';
import ReactPaginate from 'react-paginate';
import SubscribeButton from 'component/subscribeButton';
2018-03-26 23:32:43 +02:00
import Page from 'component/page';
import FileList from 'component/fileList';
import HiddenNsfwClaims from 'component/hiddenNsfwClaims';
2018-11-02 01:41:41 +01:00
import Button from 'component/button';
2017-05-04 05:44:08 +02:00
2018-03-26 23:32:43 +02:00
type Props = {
uri: string,
page: number,
totalPages: number,
fetching: boolean,
params: { page: number },
claim: Claim,
claimsInChannel: Array<Claim>,
channelIsMine: boolean,
2018-03-26 23:32:43 +02:00
fetchClaims: (string, number) => void,
navigate: (string, {}) => void,
openModal: (id: string, { uri: string }) => void,
2018-03-26 23:32:43 +02:00
};
class ChannelPage extends React.PureComponent<Props> {
2017-05-13 00:50:51 +02:00
componentDidMount() {
const { uri, page, fetchClaims } = this.props;
2017-09-08 05:15:05 +02:00
fetchClaims(uri, page || 1);
2017-05-13 00:50:51 +02:00
}
2017-05-04 05:44:08 +02:00
componentDidUpdate(prevProps: Props) {
const { page, fetchClaims, uri } = this.props;
2017-07-17 08:06:04 +02:00
if (prevProps.page && prevProps.page && page !== prevProps.page) {
fetchClaims(uri, page);
2017-08-24 23:12:23 +02:00
}
2017-05-13 00:50:51 +02:00
}
2018-03-26 23:32:43 +02:00
changePage(pageNumber: number) {
2017-09-20 16:57:43 +02:00
const { params } = this.props;
2017-07-17 08:06:04 +02:00
const newParams = Object.assign({}, params, { page: pageNumber });
this.props.navigate('/show', newParams);
2017-05-13 00:50:51 +02:00
}
paginate(e: SyntheticKeyboardEvent<*>, totalPages: number) {
// Change page if enter was pressed, and the given page is between
// the first and the last.
const pageFromInput = Number(e.currentTarget.value);
2018-05-31 05:50:12 +02:00
if (
pageFromInput &&
2018-05-31 05:50:12 +02:00
e.keyCode === 13 &&
!Number.isNaN(pageFromInput) &&
pageFromInput > 0 &&
pageFromInput <= totalPages
) {
this.changePage(pageFromInput);
}
}
2017-05-13 00:50:51 +02:00
render() {
2018-11-02 01:41:41 +01:00
const {
uri,
fetching,
claimsInChannel,
claim,
page,
totalPages,
channelIsMine,
openModal,
} = this.props;
const { name, permanent_url: permanentUrl } = claim;
2018-06-21 08:01:19 +02:00
const currentPage = parseInt((page || 1) - 1, 10);
const contentList =
claimsInChannel && claimsInChannel.length ? (
<FileList sortByHeight hideFilter fileInfos={claimsInChannel} />
) : (
2019-03-19 18:11:28 +01:00
!fetching && <span className="empty">{__('No content found.')}</span>
);
2017-05-21 16:42:34 +02:00
2017-06-06 23:19:12 +02:00
return (
2018-03-26 23:32:43 +02:00
<Page notContained>
2019-03-19 18:11:28 +01:00
<header className="channel-info">
<h1 className="media__title media__title--large">
{name}
{fetching && <BusyIndicator />}
</h1>
2019-03-19 18:11:28 +01:00
<div className="channel-info__actions__group">
<SubscribeButton uri={`lbry://${permanentUrl}`} channelName={name} />
<Button
2019-03-19 18:11:28 +01:00
button="alt"
2019-01-22 21:36:28 +01:00
icon={icons.SHARE}
label={__('Share Channel')}
onClick={() =>
openModal(MODALS.SOCIAL_SHARE, { uri, speechShareable: true, isChannel: true })
}
/>
</div>
</header>
2019-03-19 18:11:28 +01:00
<section className="media-group--list">{contentList}</section>
2019-03-05 05:46:57 +01:00
{(!fetching || (claimsInChannel && claimsInChannel.length)) && totalPages > 1 && (
<Form>
2019-03-19 18:11:28 +01:00
<fieldset-group class="fieldset-group--smushed fieldgroup--paginate">
2019-03-05 05:46:57 +01:00
<fieldset-section>
<ReactPaginate
pageCount={totalPages}
pageRangeDisplayed={2}
2019-03-19 18:11:28 +01:00
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"
2019-03-05 05:46:57 +01:00
marginPagesDisplayed={2}
onPageChange={e => this.changePage(e.selected + 1)}
forcePage={currentPage}
initialPage={currentPage}
2019-03-19 18:11:28 +01:00
containerClassName="pagination"
2019-02-20 06:20:29 +01:00
/>
2019-03-05 05:46:57 +01:00
</fieldset-section>
<FormField
2019-03-19 18:11:28 +01:00
className="paginate-channel"
2019-03-05 05:46:57 +01:00
onKeyUp={e => this.paginate(e, totalPages)}
label={__('Go to page:')}
2019-03-19 18:11:28 +01:00
type="text"
name="paginate-file"
2019-03-05 05:46:57 +01:00
/>
</fieldset-group>
</Form>
)}
2019-03-19 18:11:28 +01:00
{!channelIsMine && <HiddenNsfwClaims className="card__content help" uri={uri} />}
2018-03-26 23:32:43 +02:00
</Page>
2017-06-06 23:19:12 +02:00
);
2017-05-13 00:50:51 +02:00
}
2017-05-04 05:44:08 +02:00
}
export default ChannelPage;