-import api from '../api';
+import api, { getLinks } from '../api';
import { importFetchedStatuses } from './importer';
export const TRENDS_TAGS_FETCH_REQUEST = 'TRENDS_TAGS_FETCH_REQUEST';
export const TRENDS_STATUSES_FETCH_SUCCESS = 'TRENDS_STATUSES_FETCH_SUCCESS';
export const TRENDS_STATUSES_FETCH_FAIL = 'TRENDS_STATUSES_FETCH_FAIL';
+export const TRENDS_STATUSES_EXPAND_REQUEST = 'TRENDS_STATUSES_EXPAND_REQUEST';
+export const TRENDS_STATUSES_EXPAND_SUCCESS = 'TRENDS_STATUSES_EXPAND_SUCCESS';
+export const TRENDS_STATUSES_EXPAND_FAIL = 'TRENDS_STATUSES_EXPAND_FAIL';
+
export const fetchTrendingHashtags = () => (dispatch, getState) => {
dispatch(fetchTrendingHashtagsRequest());
});
export const fetchTrendingStatuses = () => (dispatch, getState) => {
+ if (getState().getIn(['status_lists', 'trending', 'isLoading'])) {
+ return;
+ }
+
dispatch(fetchTrendingStatusesRequest());
- api(getState).get('/api/v1/trends/statuses').then(({ data }) => {
- dispatch(importFetchedStatuses(data));
- dispatch(fetchTrendingStatusesSuccess(data));
+ api(getState).get('/api/v1/trends/statuses').then(response => {
+ const next = getLinks(response).refs.find(link => link.rel === 'next');
+ dispatch(importFetchedStatuses(response.data));
+ dispatch(fetchTrendingStatusesSuccess(response.data, next ? next.uri : null));
}).catch(err => dispatch(fetchTrendingStatusesFail(err)));
};
skipLoading: true,
});
-export const fetchTrendingStatusesSuccess = statuses => ({
+export const fetchTrendingStatusesSuccess = (statuses, next) => ({
type: TRENDS_STATUSES_FETCH_SUCCESS,
statuses,
+ next,
skipLoading: true,
});
skipLoading: true,
skipAlert: true,
});
+
+
+export const expandTrendingStatuses = () => (dispatch, getState) => {
+ const url = getState().getIn(['status_lists', 'trending', 'next'], null);
+
+ if (url === null || getState().getIn(['status_lists', 'trending', 'isLoading'])) {
+ return;
+ }
+
+ dispatch(expandTrendingStatusesRequest());
+
+ api(getState).get(url).then(response => {
+ const next = getLinks(response).refs.find(link => link.rel === 'next');
+ dispatch(importFetchedStatuses(response.data));
+ dispatch(expandTrendingStatusesSuccess(response.data, next ? next.uri : null));
+ }).catch(error => {
+ dispatch(expandTrendingStatusesFail(error));
+ });
+};
+
+export const expandTrendingStatusesRequest = () => ({
+ type: TRENDS_STATUSES_EXPAND_REQUEST,
+});
+
+export const expandTrendingStatusesSuccess = (statuses, next) => ({
+ type: TRENDS_STATUSES_EXPAND_SUCCESS,
+ statuses,
+ next,
+});
+
+export const expandTrendingStatusesFail = error => ({
+ type: TRENDS_STATUSES_EXPAND_FAIL,
+ error,
+});
import StatusList from 'mastodon/components/status_list';
import { FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';
-import { fetchTrendingStatuses } from 'mastodon/actions/trends';
+import { fetchTrendingStatuses, expandTrendingStatuses } from 'mastodon/actions/trends';
+import { debounce } from 'lodash';
const mapStateToProps = state => ({
statusIds: state.getIn(['status_lists', 'trending', 'items']),
isLoading: state.getIn(['status_lists', 'trending', 'isLoading'], true),
+ hasMore: !!state.getIn(['status_lists', 'trending', 'next']),
});
export default @connect(mapStateToProps)
static propTypes = {
statusIds: ImmutablePropTypes.list,
isLoading: PropTypes.bool,
+ hasMore: PropTypes.bool,
multiColumn: PropTypes.bool,
dispatch: PropTypes.func.isRequired,
};
dispatch(fetchTrendingStatuses());
}
+ handleLoadMore = debounce(() => {
+ const { dispatch } = this.props;
+ dispatch(expandTrendingStatuses());
+ }, 300, { leading: true })
+
render () {
- const { isLoading, statusIds, multiColumn } = this.props;
+ const { isLoading, hasMore, statusIds, multiColumn } = this.props;
const emptyMessage = <FormattedMessage id='empty_column.explore_statuses' defaultMessage='Nothing is trending right now. Check back later!' />;
trackScroll
statusIds={statusIds}
scrollKey='explore-statuses'
- hasMore={false}
+ hasMore={hasMore}
isLoading={isLoading}
+ onLoadMore={this.handleLoadMore}
emptyMessage={emptyMessage}
bindToDocument={!multiColumn}
withCounters
TRENDS_STATUSES_FETCH_REQUEST,
TRENDS_STATUSES_FETCH_SUCCESS,
TRENDS_STATUSES_FETCH_FAIL,
+ TRENDS_STATUSES_EXPAND_REQUEST,
+ TRENDS_STATUSES_EXPAND_SUCCESS,
+ TRENDS_STATUSES_EXPAND_FAIL,
} from '../actions/trends';
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
import {
case BOOKMARKED_STATUSES_EXPAND_SUCCESS:
return appendToList(state, 'bookmarks', action.statuses, action.next);
case TRENDS_STATUSES_FETCH_REQUEST:
+ case TRENDS_STATUSES_EXPAND_REQUEST:
return state.setIn(['trending', 'isLoading'], true);
case TRENDS_STATUSES_FETCH_FAIL:
+ case TRENDS_STATUSES_EXPAND_FAIL:
return state.setIn(['trending', 'isLoading'], false);
case TRENDS_STATUSES_FETCH_SUCCESS:
return normalizeList(state, 'trending', action.statuses, action.next);
+ case TRENDS_STATUSES_EXPAND_SUCCESS:
+ return appendToList(state, 'trending', action.statuses, action.next);
case FAVOURITE_SUCCESS:
return prependOneToList(state, 'favourites', action.status);
case UNFAVOURITE_SUCCESS: