2017-05-20 02:28:25 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-06-11 18:07:35 +03:00
|
|
|
import { fetchAccount } from '../../actions/accounts';
|
|
|
|
import { refreshAccountMediaTimeline, expandAccountMediaTimeline } from '../../actions/timelines';
|
2017-05-20 02:28:25 +03:00
|
|
|
import LoadingIndicator from '../../components/loading_indicator';
|
|
|
|
import Column from '../ui/components/column';
|
|
|
|
import ColumnBackButton from '../../components/column_back_button';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import { getAccountGallery } from '../../selectors';
|
|
|
|
import MediaItem from './components/media_item';
|
|
|
|
import HeaderContainer from '../account_timeline/containers/header_container';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
|
|
|
import { ScrollContainer } from 'react-router-scroll';
|
2017-05-25 06:22:46 +03:00
|
|
|
import LoadMore from '../../components/load_more';
|
2017-05-20 02:28:25 +03:00
|
|
|
|
|
|
|
const mapStateToProps = (state, props) => ({
|
|
|
|
medias: getAccountGallery(state, Number(props.params.accountId)),
|
2017-06-11 18:07:35 +03:00
|
|
|
isLoading: state.getIn(['timelines', `account:${Number(props.params.accountId)}:media`, 'isLoading']),
|
|
|
|
hasMore: !!state.getIn(['timelines', `account:${Number(props.params.accountId)}:media`, 'next']),
|
2017-05-20 02:28:25 +03:00
|
|
|
autoPlayGif: state.getIn(['meta', 'auto_play_gif']),
|
|
|
|
});
|
|
|
|
|
2017-06-23 20:36:54 +03:00
|
|
|
@connect(mapStateToProps)
|
|
|
|
export default class AccountGallery extends ImmutablePureComponent {
|
2017-05-20 02:28:25 +03:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
params: PropTypes.object.isRequired,
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
|
|
|
medias: ImmutablePropTypes.list.isRequired,
|
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
hasMore: PropTypes.bool,
|
|
|
|
autoPlayGif: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
this.props.dispatch(fetchAccount(Number(this.props.params.accountId)));
|
2017-06-11 18:07:35 +03:00
|
|
|
this.props.dispatch(refreshAccountMediaTimeline(Number(this.props.params.accountId)));
|
2017-05-20 02:28:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) {
|
|
|
|
this.props.dispatch(fetchAccount(Number(nextProps.params.accountId)));
|
2017-06-11 18:07:35 +03:00
|
|
|
this.props.dispatch(refreshAccountMediaTimeline(Number(this.props.params.accountId)));
|
2017-05-20 02:28:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 06:22:46 +03:00
|
|
|
handleScrollToBottom = () => {
|
|
|
|
if (this.props.hasMore) {
|
|
|
|
this.props.dispatch(expandAccountMediaTimeline(Number(this.props.params.accountId)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:28:25 +03:00
|
|
|
handleScroll = (e) => {
|
|
|
|
const { scrollTop, scrollHeight, clientHeight } = e.target;
|
2017-05-25 06:22:46 +03:00
|
|
|
const offset = scrollHeight - scrollTop - clientHeight;
|
2017-05-20 02:28:25 +03:00
|
|
|
|
2017-05-25 06:22:46 +03:00
|
|
|
if (150 > offset && !this.props.isLoading) {
|
|
|
|
this.handleScrollToBottom();
|
2017-05-20 02:28:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 06:22:46 +03:00
|
|
|
handleLoadMore = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.handleScrollToBottom();
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:28:25 +03:00
|
|
|
render () {
|
2017-05-25 06:22:46 +03:00
|
|
|
const { medias, autoPlayGif, isLoading, hasMore } = this.props;
|
|
|
|
|
|
|
|
let loadMore = null;
|
2017-05-20 02:28:25 +03:00
|
|
|
|
|
|
|
if (!medias && isLoading) {
|
|
|
|
return (
|
|
|
|
<Column>
|
|
|
|
<LoadingIndicator />
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-25 06:22:46 +03:00
|
|
|
if (!isLoading && medias.size > 0 && hasMore) {
|
|
|
|
loadMore = <LoadMore onClick={this.handleLoadMore} />;
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:28:25 +03:00
|
|
|
return (
|
|
|
|
<Column>
|
|
|
|
<ColumnBackButton />
|
|
|
|
|
|
|
|
<ScrollContainer scrollKey='account_gallery'>
|
|
|
|
<div className='scrollable' onScroll={this.handleScroll}>
|
|
|
|
<HeaderContainer accountId={this.props.params.accountId} />
|
|
|
|
|
|
|
|
<div className='account-section-headline'>
|
|
|
|
<FormattedMessage id='account.media' defaultMessage='Media' />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='account-gallery__container'>
|
|
|
|
{medias.map(media =>
|
|
|
|
<MediaItem
|
|
|
|
key={media.get('id')}
|
|
|
|
media={media}
|
|
|
|
autoPlayGif={autoPlayGif}
|
|
|
|
/>
|
|
|
|
)}
|
2017-05-25 06:22:46 +03:00
|
|
|
{loadMore}
|
2017-05-20 02:28:25 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ScrollContainer>
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|