From bf3b0554ddffd0534c6c0bd404b8761c26defbc0 Mon Sep 17 00:00:00 2001 From: mgabdev <> Date: Thu, 6 Aug 2020 00:18:56 -0500 Subject: [PATCH] Updated Explore feature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Updated: - Explore feature --- app/javascript/gabsocial/features/explore.js | 257 +++++-------------- 1 file changed, 63 insertions(+), 194 deletions(-) diff --git a/app/javascript/gabsocial/features/explore.js b/app/javascript/gabsocial/features/explore.js index 77e12348..ebffa841 100644 --- a/app/javascript/gabsocial/features/explore.js +++ b/app/javascript/gabsocial/features/explore.js @@ -1,230 +1,99 @@ +import { defineMessages, injectIntl } from 'react-intl' import { withRouter } from 'react-router-dom' -import isObject from 'lodash.isobject' -import queryString from 'query-string' -import { fetchGabTrends } from '../actions/gab_trends' -import { BREAKPOINT_EXTRA_SMALL } from '../constants' -import Block from '../components/block' -import Button from '../components/button' -import ColumnIndicator from '../components/column_indicator' -import Heading from '../components/heading' -import Icon from '../components/icon' -import Pills from '../components/pills' -import Responsive from './ui/util/responsive_component'; -import TabBar from '../components/tab_bar' -import Text from '../components/text' -import TrendsItem from '../components/trends_item' +import throttle from 'lodash.throttle' +import { + expandHomeTimeline, + forceDequeueTimeline, +} from '../actions/timelines' +import StatusList from '../components/status_list' -// const messages = defineMessages({ -// title: { id: 'trends.title', defaultMessage: 'Trending right now' }, -// }) +const messages = defineMessages({ + title: { id: 'column.home', defaultMessage: 'Home' }, + empty: { id: 'empty_timeline.home', defaultMessage: 'Your home timeline is empty. Start following other users to receive their content here.' }, +}) const mapStateToProps = (state) => ({ - isError: state.getIn(['gab_trends', 'partner', 'isError']), - isLoading: state.getIn(['gab_trends', 'partner', 'isLoading']), - items: state.getIn(['gab_trends', 'partner', 'items']), + isPartial: state.getIn(['timelines', 'home', 'isPartial']), }) const mapDispatchToProps = (dispatch) => ({ - onfetchGabTrends: () => dispatch(fetchGabTrends('partner')), + onExpandHomeTimeline(options) { + if (!options) dispatch(forceDequeueTimeline('home')) + dispatch(expandHomeTimeline(options)) + }, }) export default +@injectIntl @withRouter @connect(mapStateToProps, mapDispatchToProps) class Explore extends PureComponent { - static contextTypes = { - router: PropTypes.object.isRequired, - } - static propTypes = { intl: PropTypes.object.isRequired, - isError: PropTypes.bool, - isLoading: PropTypes.bool, - items: PropTypes.object, - onfetchGabTrends: PropTypes.func.isRequired, + isPartial: PropTypes.bool, + onExpandHomeTimeline: PropTypes.func.isRequired, } - state = { - activeDomain: null, + componentDidMount () { + this._checkIfReloadNeeded(false, this.props.isPartial) } - updateOnProps = [ - 'items', - 'isLoading', - 'isError', - ] - - componentDidUpdate(prevProps) { - if (this.props.location !== prevProps.location) { - this.setActiveDomain(this.props.location) + componentDidUpdate (prevProps) { + this._checkIfReloadNeeded(prevProps.isPartial, this.props.isPartial) + + //Check if clicked on "home" button, if so, reload + if (prevProps.location.key !== this.props.location.key && + prevProps.location.pathname === '/home' && + this.props.location.pathname === '/home') { + this.handleReload() } } - componentDidMount() { - this.props.onfetchGabTrends() - this.setActiveDomain(this.props.location) + componentWillUnmount () { + this._stopPolling() } - setActiveDomain(location) { - const { search } = location - const qp = queryString.parse(search) - const domain = qp.domain ? `${qp.domain}`.toLowerCase() : undefined + handleLoadMore = (maxId) => { + this.props.onExpandHomeTimeline({ maxId }) + } - if (!!domain) { - this.setState({ activeDomain: domain }) - } else { - this.setState({ activeDomain: null }) + handleReload = throttle(() => { + this.props.onExpandHomeTimeline() + }, 5000) + + _checkIfReloadNeeded (wasPartial, isPartial) { + const { onExpandHomeTimeline } = this.props + + if (!wasPartial && isPartial) { + this.polling = setInterval(() => { + onExpandHomeTimeline() + }, 3000) + } else if (wasPartial && !isPartial) { + this._stopPolling() + } + } + + _stopPolling () { + if (this.polling) { + clearInterval(this.polling) + this.polling = null } } render () { - const { - intl, - isError, - isLoading, - items, - } = this.props - const { activeDomain } = this.state + const { intl } = this.props - if (isError || !isObject(items)) { - return
error
- } - - let domainExists = !activeDomain - let headline, headlineLink - let todaysTopTitle - let leadHeadlines = [] - let todaysTop = [] - let domainTabs = [] - try { - headline = items.headline.title - headlineLink = items.headline.href - leadHeadlines = items.leadHeadlines - - if (activeDomain) { - const domainBlock = items.trackedDomains.find((d) => `${d.domain}`.toLowerCase() === activeDomain) - if (domainBlock) domainExists = true - - todaysTop = domainBlock.topUrls - todaysTopTitle = domainBlock.title - } else { - todaysTop = items.trending.topUrls - todaysTopTitle = "Today's Top" - } - - const domains = items.trackedDomains - domainTabs = domains.map((block) => ({ - title: block.title, - to: `/explore?domain=${block.domain}`, - onClick: () => {}, - active: activeDomain === `${block.domain}`.toLowerCase(), - })) - domainTabs = [ - { - title: "Today's Top", - to: `/explore`, - onClick: () => {}, - active: !activeDomain, - }, - ...domainTabs, - ] - } catch (error) { - return ( -
- - - -
- ) - } - - if (!domainExists) { - return
error
- } + const emptyMessage = intl.formatMessage(messages.empty) return ( -
- -
- - - - -
- -
-
-
- -
- { - !activeDomain && -
- - - {headline} - - -
- } - - { - !activeDomain && leadHeadlines.length > 0 && -
-
- - - Headlines - -
- { - leadHeadlines.map((lead) => ( - - )) - } -
- } - -
-
- - - {todaysTopTitle} - -
-
- { - todaysTop.map((block, i) => ( - - )) - } -
-
- -
-
-
+ ) } -} +} \ No newline at end of file