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.

102 lines
3.1 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import { fetchFavouritedStatuses, expandFavouritedStatuses } from '../../actions/favourites';
  6. import Column from '../ui/components/column';
  7. import ColumnHeader from '../../components/column_header';
  8. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  9. import StatusList from '../../components/status_list';
  10. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  11. import ImmutablePureComponent from 'react-immutable-pure-component';
  12. import { debounce } from 'lodash';
  13. const messages = defineMessages({
  14. heading: { id: 'column.favourites', defaultMessage: 'Favourites' },
  15. });
  16. const mapStateToProps = state => ({
  17. statusIds: state.getIn(['status_lists', 'favourites', 'items']),
  18. isLoading: state.getIn(['status_lists', 'favourites', 'isLoading'], true),
  19. hasMore: !!state.getIn(['status_lists', 'favourites', 'next']),
  20. });
  21. export default @connect(mapStateToProps)
  22. @injectIntl
  23. class Favourites extends ImmutablePureComponent {
  24. static propTypes = {
  25. dispatch: PropTypes.func.isRequired,
  26. statusIds: ImmutablePropTypes.list.isRequired,
  27. intl: PropTypes.object.isRequired,
  28. columnId: PropTypes.string,
  29. multiColumn: PropTypes.bool,
  30. hasMore: PropTypes.bool,
  31. isLoading: PropTypes.bool,
  32. };
  33. componentWillMount () {
  34. this.props.dispatch(fetchFavouritedStatuses());
  35. }
  36. handlePin = () => {
  37. const { columnId, dispatch } = this.props;
  38. if (columnId) {
  39. dispatch(removeColumn(columnId));
  40. } else {
  41. dispatch(addColumn('FAVOURITES', {}));
  42. }
  43. }
  44. handleMove = (dir) => {
  45. const { columnId, dispatch } = this.props;
  46. dispatch(moveColumn(columnId, dir));
  47. }
  48. handleHeaderClick = () => {
  49. this.column.scrollTop();
  50. }
  51. setRef = c => {
  52. this.column = c;
  53. }
  54. handleLoadMore = debounce(() => {
  55. this.props.dispatch(expandFavouritedStatuses());
  56. }, 300, { leading: true })
  57. render () {
  58. const { intl, statusIds, columnId, multiColumn, hasMore, isLoading } = this.props;
  59. const pinned = !!columnId;
  60. const emptyMessage = <FormattedMessage id='empty_column.favourited_statuses' defaultMessage="You don't have any favourite toots yet. When you favourite one, it will show up here." />;
  61. return (
  62. <Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.heading)}>
  63. <ColumnHeader
  64. icon='star'
  65. title={intl.formatMessage(messages.heading)}
  66. onPin={this.handlePin}
  67. onMove={this.handleMove}
  68. onClick={this.handleHeaderClick}
  69. pinned={pinned}
  70. multiColumn={multiColumn}
  71. showBackButton
  72. />
  73. <StatusList
  74. trackScroll={!pinned}
  75. statusIds={statusIds}
  76. scrollKey={`favourited_statuses-${columnId}`}
  77. hasMore={hasMore}
  78. isLoading={isLoading}
  79. onLoadMore={this.handleLoadMore}
  80. emptyMessage={emptyMessage}
  81. bindToDocument={!multiColumn}
  82. />
  83. </Column>
  84. );
  85. }
  86. }