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.
 
 
 
 

139 lines
4.1 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 { 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 { ScrollContainer } from 'react-router-scroll-4';
  15. import LoadMore from '../../components/load_more';
  16. const mapStateToProps = (state, props) => ({
  17. medias: getAccountGallery(state, props.params.accountId),
  18. isLoading: state.getIn(['timelines', `account:${props.params.accountId}:media`, 'isLoading']),
  19. hasMore: state.getIn(['timelines', `account:${props.params.accountId}:media`, 'hasMore']),
  20. });
  21. class LoadMoreMedia extends ImmutablePureComponent {
  22. static propTypes = {
  23. shouldUpdateScroll: PropTypes.func,
  24. maxId: PropTypes.string,
  25. onLoadMore: PropTypes.func.isRequired,
  26. };
  27. handleLoadMore = () => {
  28. this.props.onLoadMore(this.props.maxId);
  29. }
  30. render () {
  31. return (
  32. <LoadMore
  33. disabled={this.props.disabled}
  34. onLoadMore={this.handleLoadMore}
  35. />
  36. );
  37. }
  38. }
  39. export default @connect(mapStateToProps)
  40. class AccountGallery extends ImmutablePureComponent {
  41. static propTypes = {
  42. params: PropTypes.object.isRequired,
  43. dispatch: PropTypes.func.isRequired,
  44. medias: ImmutablePropTypes.list.isRequired,
  45. isLoading: PropTypes.bool,
  46. hasMore: PropTypes.bool,
  47. };
  48. componentDidMount () {
  49. this.props.dispatch(fetchAccount(this.props.params.accountId));
  50. this.props.dispatch(expandAccountMediaTimeline(this.props.params.accountId));
  51. }
  52. componentWillReceiveProps (nextProps) {
  53. if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) {
  54. this.props.dispatch(fetchAccount(nextProps.params.accountId));
  55. this.props.dispatch(expandAccountMediaTimeline(this.props.params.accountId));
  56. }
  57. }
  58. handleScrollToBottom = () => {
  59. if (this.props.hasMore) {
  60. this.handleLoadMore(this.props.medias.last().getIn(['status', 'id']));
  61. }
  62. }
  63. handleScroll = (e) => {
  64. const { scrollTop, scrollHeight, clientHeight } = e.target;
  65. const offset = scrollHeight - scrollTop - clientHeight;
  66. if (150 > offset && !this.props.isLoading) {
  67. this.handleScrollToBottom();
  68. }
  69. }
  70. handleLoadMore = maxId => {
  71. this.props.dispatch(expandAccountMediaTimeline(this.props.params.accountId, { maxId }));
  72. };
  73. handleLoadOlder = (e) => {
  74. e.preventDefault();
  75. this.handleScrollToBottom();
  76. }
  77. render () {
  78. const { medias, shouldUpdateScroll, isLoading, hasMore } = this.props;
  79. let loadOlder = null;
  80. if (!medias && isLoading) {
  81. return (
  82. <Column>
  83. <LoadingIndicator />
  84. </Column>
  85. );
  86. }
  87. if (!isLoading && medias.size > 0 && hasMore) {
  88. loadOlder = <LoadMore onClick={this.handleLoadOlder} />;
  89. }
  90. return (
  91. <Column>
  92. <ColumnBackButton />
  93. <ScrollContainer scrollKey='account_gallery' shouldUpdateScroll={shouldUpdateScroll}>
  94. <div className='scrollable' onScroll={this.handleScroll}>
  95. <HeaderContainer accountId={this.props.params.accountId} />
  96. <div className='account-gallery__container'>
  97. {medias.map((media, index) => media === null ? (
  98. <LoadMoreMedia
  99. key={'more:' + medias.getIn(index + 1, 'id')}
  100. maxId={index > 0 ? medias.getIn(index - 1, 'id') : null}
  101. />
  102. ) : (
  103. <MediaItem
  104. key={media.get('id')}
  105. media={media}
  106. />
  107. ))}
  108. {loadOlder}
  109. </div>
  110. </div>
  111. </ScrollContainer>
  112. </Column>
  113. );
  114. }
  115. }