import React from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' 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' class News extends React.PureComponent { static contextTypes = { router: PropTypes.object.isRequired, } state = { activeDomain: null, } componentDidUpdate(prevProps) { if (this.props.location !== prevProps.location) { this.setActiveDomain(this.props.location) } } componentDidMount() { this.props.onfetchGabTrends() this.setActiveDomain(this.props.location) } setActiveDomain(location) { const { search } = location const qp = queryString.parse(search) const domain = qp.domain ? `${qp.domain}`.toLowerCase() : undefined if (!!domain) { this.setState({ activeDomain: domain }) } else { this.setState({ activeDomain: null }) } } render() { const { intl, isError, isLoading, items, } = this.props const { activeDomain } = this.state 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: `/news?domain=${block.domain}`, onClick: () => { }, active: activeDomain === `${block.domain}`.toLowerCase(), })) domainTabs = [ { title: "Today's Top", to: `/news`, onClick: () => { }, active: !activeDomain, }, ...domainTabs, ] } catch (error) { return (
) } if (!domainExists) { return
error
} return (
{ !activeDomain &&
{headline}
} { !activeDomain && leadHeadlines.length > 0 &&
Headlines
{ leadHeadlines.map((lead) => ( )) }
}
{todaysTopTitle}
{ todaysTop.map((block, i) => ( )) }
) } } const mapStateToProps = (state) => ({ isError: state.getIn(['gab_trends', 'partner', 'isError']), isLoading: state.getIn(['gab_trends', 'partner', 'isLoading']), items: state.getIn(['gab_trends', 'partner', 'items']), }) const mapDispatchToProps = (dispatch) => ({ onfetchGabTrends: () => dispatch(fetchGabTrends('partner')), }) News.propTypes = { intl: PropTypes.object.isRequired, isError: PropTypes.bool, isLoading: PropTypes.bool, items: PropTypes.object, onfetchGabTrends: PropTypes.func.isRequired, } export default withRouter(connect(mapStateToProps, mapDispatchToProps)(News))