The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

112 lines
3.5 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import PropTypes from 'prop-types';
  5. import { fetchAccount } from '../../actions/accounts';
  6. import { refreshAccountMediaTimeline, expandAccountMediaTimeline } from '../../actions/timelines';
  7. import LoadingIndicator from '../../components/loading_indicator';
  8. import Column from '../ui/components/column';
  9. import ColumnBackButton from '../../components/column_back_button';
  10. import ImmutablePureComponent from 'react-immutable-pure-component';
  11. import { getAccountGallery } from '../../selectors';
  12. import MediaItem from './components/media_item';
  13. import HeaderContainer from '../account_timeline/containers/header_container';
  14. import { FormattedMessage } from 'react-intl';
  15. import { ScrollContainer } from 'react-router-scroll-4';
  16. import LoadMore from '../../components/load_more';
  17. const mapStateToProps = (state, props) => ({
  18. medias: getAccountGallery(state, props.params.accountId),
  19. isLoading: state.getIn(['timelines', `account:${props.params.accountId}:media`, 'isLoading']),
  20. hasMore: !!state.getIn(['timelines', `account:${props.params.accountId}:media`, 'next']),
  21. });
  22. @connect(mapStateToProps)
  23. export default class AccountGallery extends ImmutablePureComponent {
  24. static propTypes = {
  25. params: PropTypes.object.isRequired,
  26. dispatch: PropTypes.func.isRequired,
  27. medias: ImmutablePropTypes.list.isRequired,
  28. isLoading: PropTypes.bool,
  29. hasMore: PropTypes.bool,
  30. };
  31. componentDidMount () {
  32. this.props.dispatch(fetchAccount(this.props.params.accountId));
  33. this.props.dispatch(refreshAccountMediaTimeline(this.props.params.accountId));
  34. }
  35. componentWillReceiveProps (nextProps) {
  36. if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) {
  37. this.props.dispatch(fetchAccount(nextProps.params.accountId));
  38. this.props.dispatch(refreshAccountMediaTimeline(this.props.params.accountId));
  39. }
  40. }
  41. handleScrollToBottom = () => {
  42. if (this.props.hasMore) {
  43. this.props.dispatch(expandAccountMediaTimeline(this.props.params.accountId));
  44. }
  45. }
  46. handleScroll = (e) => {
  47. const { scrollTop, scrollHeight, clientHeight } = e.target;
  48. const offset = scrollHeight - scrollTop - clientHeight;
  49. if (150 > offset && !this.props.isLoading) {
  50. this.handleScrollToBottom();
  51. }
  52. }
  53. handleLoadMore = (e) => {
  54. e.preventDefault();
  55. this.handleScrollToBottom();
  56. }
  57. render () {
  58. const { medias, isLoading, hasMore } = this.props;
  59. let loadMore = null;
  60. if (!medias && isLoading) {
  61. return (
  62. <Column>
  63. <LoadingIndicator />
  64. </Column>
  65. );
  66. }
  67. if (!isLoading && medias.size > 0 && hasMore) {
  68. loadMore = <LoadMore onClick={this.handleLoadMore} />;
  69. }
  70. return (
  71. <Column>
  72. <ColumnBackButton />
  73. <ScrollContainer scrollKey='account_gallery'>
  74. <div className='scrollable' onScroll={this.handleScroll}>
  75. <HeaderContainer accountId={this.props.params.accountId} />
  76. <div className='account-section-headline'>
  77. <FormattedMessage id='account.media' defaultMessage='Media' />
  78. </div>
  79. <div className='account-gallery__container'>
  80. {medias.map(media =>
  81. <MediaItem
  82. key={media.get('id')}
  83. media={media}
  84. />
  85. )}
  86. {loadMore}
  87. </div>
  88. </div>
  89. </ScrollContainer>
  90. </Column>
  91. );
  92. }
  93. }