Added isLazy functionality to TrendsPanel

• Added:
- isLazy functionality to TrendsPanel
This commit is contained in:
mgabdev 2020-08-06 23:05:46 -05:00
parent 7c86ceca42
commit 77ebf94a7b

View File

@ -9,6 +9,7 @@ import TrendsItemPlaceholder from '../placeholder/trends_item_placeholder'
const messages = defineMessages({ const messages = defineMessages({
title: { id: 'trends.title', defaultMessage: 'Trending right now' }, title: { id: 'trends.title', defaultMessage: 'Trending right now' },
readMore: { id: 'status.read_more', defaultMessage: 'Read more' },
}) })
const mapStateToProps = (state) => ({ const mapStateToProps = (state) => ({
@ -29,6 +30,7 @@ class TrendsPanel extends ImmutablePureComponent {
static propTypes = { static propTypes = {
intl: PropTypes.object.isRequired, intl: PropTypes.object.isRequired,
isError: PropTypes.bool, isError: PropTypes.bool,
isLazy: PropTypes.bool,
isLoading: PropTypes.bool, isLoading: PropTypes.bool,
items: ImmutablePropTypes.list.isRequired, items: ImmutablePropTypes.list.isRequired,
onfetchGabTrends: PropTypes.func.isRequired, onfetchGabTrends: PropTypes.func.isRequired,
@ -36,12 +38,34 @@ class TrendsPanel extends ImmutablePureComponent {
updateOnProps = [ updateOnProps = [
'items', 'items',
'isLazy',
'isLoading', 'isLoading',
'isError', 'isError',
] ]
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()
}
}
componentDidMount() { componentDidMount() {
this.props.onfetchGabTrends() if (!this.props.isLazy) {
this.props.onfetchGabTrends()
this.setState({ fetched: true })
}
} }
render() { render() {
@ -51,13 +75,19 @@ class TrendsPanel extends ImmutablePureComponent {
isLoading, isLoading,
items, items,
} = this.props } = this.props
const { fetched } = this.state
if (isError) return null
const count = !!items ? items.count() : 0
if (isError || (count === 0 && fetched)) return null
return ( return (
<PanelLayout <PanelLayout
noPadding noPadding
title={intl.formatMessage(messages.title)} title={intl.formatMessage(messages.title)}
headerButtonTitle={intl.formatMessage(messages.readMore)}
headerButtonTo='/news'
footerButtonTitle={intl.formatMessage(messages.readMore)}
footerButtonTo='/news'
> >
<ScrollableList <ScrollableList
showLoading={isLoading} showLoading={isLoading}