2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-17 21:39:25 +01:00
|
|
|
import { connect } from 'react-redux'
|
2020-02-19 23:57:07 +00:00
|
|
|
import { injectIntl, defineMessages } from 'react-intl'
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component'
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes'
|
2020-04-04 00:18:26 +01:00
|
|
|
import { fetchGabTrends } from '../../actions/gab_trends'
|
2020-02-19 23:57:07 +00:00
|
|
|
import PanelLayout from './panel_layout'
|
2020-04-28 06:33:58 +01:00
|
|
|
import ScrollableList from '../scrollable_list'
|
2020-04-29 23:32:49 +01:00
|
|
|
import TrendsItem from '../trends_item'
|
2020-07-28 21:11:51 +01:00
|
|
|
import TrendsItemPlaceholder from '../placeholder/trends_item_placeholder'
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
class TrendsPanel extends ImmutablePureComponent {
|
|
|
|
|
2020-04-29 23:32:49 +01:00
|
|
|
updateOnProps = [
|
2020-06-09 03:10:51 +01:00
|
|
|
'items',
|
2020-08-07 05:05:46 +01:00
|
|
|
'isLazy',
|
2020-06-09 03:10:51 +01:00
|
|
|
'isLoading',
|
|
|
|
'isError',
|
2020-04-29 23:32:49 +01:00
|
|
|
]
|
|
|
|
|
2020-08-07 05:05:46 +01:00
|
|
|
state = {
|
|
|
|
fetched: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDerivedStateFromProps(nextProps, prevState) {
|
|
|
|
if (nextProps.shouldLoad && !prevState.fetched) {
|
|
|
|
return { fetched: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
if (!prevState.fetched && this.state.fetched) {
|
|
|
|
this.props.onfetchGabTrends()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
componentDidMount() {
|
2020-08-07 05:05:46 +01:00
|
|
|
if (!this.props.isLazy) {
|
|
|
|
this.props.onfetchGabTrends()
|
|
|
|
this.setState({ fetched: true })
|
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-06-09 03:10:51 +01:00
|
|
|
const {
|
|
|
|
intl,
|
|
|
|
isError,
|
|
|
|
isLoading,
|
|
|
|
items,
|
|
|
|
} = this.props
|
2020-08-07 05:05:46 +01:00
|
|
|
const { fetched } = this.state
|
|
|
|
|
|
|
|
const count = !!items ? items.count() : 0
|
|
|
|
if (isError || (count === 0 && fetched)) return null
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
return (
|
2020-02-21 00:57:29 +00:00
|
|
|
<PanelLayout
|
|
|
|
noPadding
|
|
|
|
title={intl.formatMessage(messages.title)}
|
2020-08-07 05:05:46 +01:00
|
|
|
headerButtonTitle={intl.formatMessage(messages.readMore)}
|
|
|
|
headerButtonTo='/news'
|
|
|
|
footerButtonTitle={intl.formatMessage(messages.readMore)}
|
|
|
|
footerButtonTo='/news'
|
2020-02-21 00:57:29 +00:00
|
|
|
>
|
2020-04-29 23:32:49 +01:00
|
|
|
<ScrollableList
|
2020-07-28 21:11:51 +01:00
|
|
|
showLoading={isLoading}
|
|
|
|
placeholderComponent={TrendsItemPlaceholder}
|
|
|
|
placeholderCount={8}
|
2020-04-29 23:32:49 +01:00
|
|
|
scrollKey='trending-items'
|
|
|
|
>
|
2020-04-04 00:18:26 +01:00
|
|
|
{
|
2020-06-09 03:10:51 +01:00
|
|
|
items.slice(0, 8).map((trend, i) => (
|
2020-04-29 23:32:49 +01:00
|
|
|
<TrendsItem
|
|
|
|
key={`gab-trend-${i}`}
|
|
|
|
index={i + 1}
|
|
|
|
isLast={i === 7}
|
2020-07-02 03:39:43 +01:00
|
|
|
title={trend.get('title')}
|
|
|
|
description={trend.get('description')}
|
|
|
|
url={trend.get('url')}
|
|
|
|
author={trend.getIn(['author', 'name'], '')}
|
|
|
|
date={trend.get('date_published')}
|
2020-04-29 23:32:49 +01:00
|
|
|
/>
|
|
|
|
))
|
2020-04-04 00:18:26 +01:00
|
|
|
}
|
2020-04-29 23:32:49 +01:00
|
|
|
</ScrollableList>
|
2019-08-07 06:02:36 +01:00
|
|
|
</PanelLayout>
|
2020-02-19 23:57:07 +00:00
|
|
|
)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
2020-08-19 01:22:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
title: { id: 'trends.title', defaultMessage: 'Trending right now' },
|
|
|
|
readMore: { id: 'status.read_more', defaultMessage: 'Read more' },
|
|
|
|
})
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => ({
|
|
|
|
isError: state.getIn(['gab_trends', 'feed', 'isError']),
|
|
|
|
isLoading: state.getIn(['gab_trends', 'feed', 'isLoading']),
|
|
|
|
items: state.getIn(['gab_trends', 'feed', 'items']),
|
|
|
|
})
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
onfetchGabTrends: () => dispatch(fetchGabTrends('feed')),
|
|
|
|
})
|
|
|
|
|
|
|
|
TrendsPanel.propTypes = {
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
isError: PropTypes.bool,
|
|
|
|
isLazy: PropTypes.bool,
|
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
items: ImmutablePropTypes.list.isRequired,
|
|
|
|
onfetchGabTrends: PropTypes.func.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(TrendsPanel))
|