gab-social/app/javascript/gabsocial/components/panel/user_suggestions_panel.js

128 lines
3.5 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
2020-04-23 07:13:29 +01:00
import { defineMessages, injectIntl } from 'react-intl'
import {
fetchRelatedSuggestions,
fetchPopularSuggestions,
} from '../../actions/suggestions'
2020-04-23 07:13:29 +01:00
import ImmutablePureComponent from 'react-immutable-pure-component'
import ImmutablePropTypes from 'react-immutable-proptypes'
import Account from '../account'
import AccountPlaceholder from '../placeholder/account_placeholder'
2020-04-23 07:13:29 +01:00
import PanelLayout from './panel_layout'
class UserSuggestionsPanel extends ImmutablePureComponent {
state = {
fetched: !this.props.isLazy,
2020-04-23 07:13:29 +01:00
}
updateOnProps = [
'suggestions',
'isLazy',
'shouldLoad',
2020-04-23 07:13:29 +01:00
]
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.shouldLoad && !prevState.fetched) {
return { fetched: true }
}
return null
}
componentDidUpdate(prevProps, prevState) {
if (!prevState.fetched && this.state.fetched) {
this.handleFetch()
}
}
2020-05-10 04:26:58 +01:00
componentDidMount() {
if (!this.props.isLazy) {
this.handleFetch()
}
}
handleFetch = () => {
if (this.props.suggestionType === 'verified') {
this.props.fetchPopularSuggestions()
} else {
this.props.fetchRelatedSuggestions()
}
}
render() {
const {
intl,
isLoading,
suggestions,
suggestionType,
} = this.props
2020-04-23 07:13:29 +01:00
2020-04-28 06:33:58 +01:00
if (suggestions.isEmpty()) return null
const Child = isLoading ? AccountPlaceholder : Account
const arr = isLoading ? Array.apply(null, { length: 6 }) : suggestions
const title = suggestionType === 'verified' ? intl.formatMessage(messages.verifiedTitle) : intl.formatMessage(messages.relatedTitle)
return (
2020-02-21 00:57:29 +00:00
<PanelLayout
2020-04-28 06:33:58 +01:00
noPadding
title={title}
footerButtonTitle={intl.formatMessage(messages.show_more)}
footerButtonTo='/suggestions'
2020-02-21 00:57:29 +00:00
>
<div className={_s.d}>
2020-04-23 07:13:29 +01:00
{
arr.map((accountId) => (
<Child
2020-04-28 06:33:58 +01:00
compact
2020-04-23 07:13:29 +01:00
key={accountId}
id={accountId}
isSmall={isLoading ? true : undefined}
2020-04-23 07:13:29 +01:00
/>
))
}
</div>
</PanelLayout>
2020-04-23 07:13:29 +01:00
)
}
}
const messages = defineMessages({
dismissSuggestion: { id: 'suggestions.dismiss', defaultMessage: 'Dismiss suggestion' },
relatedTitle: { id: 'who_to_follow.title', defaultMessage: 'Who to Follow' },
verifiedTitle: { id: 'who_to_follow.verified_title', defaultMessage: 'Verified Accounts to Follow' },
show_more: { id: 'who_to_follow.more', defaultMessage: 'Show more' },
})
const mapStateToProps = (state, { suggestionType = 'related' }) => ({
suggestions: state.getIn(['suggestions', suggestionType, 'items']),
isLoading: state.getIn(['suggestions', suggestionType, 'isLoading']),
})
const mapDispatchToProps = (dispatch) => ({
fetchRelatedSuggestions: () => dispatch(fetchRelatedSuggestions()),
fetchPopularSuggestions: () => dispatch(fetchPopularSuggestions()),
})
UserSuggestionsPanel.propTypes = {
suggestionType: PropTypes.oneOf([
'related',
'verified'
]),
fetchRelatedSuggestions: PropTypes.func.isRequired,
fetchPopularSuggestions: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
suggestions: ImmutablePropTypes.list.isRequired,
isLoading: PropTypes.bool.isRequired,
isLazy: PropTypes.bool,
}
UserSuggestionsPanel.defaultProps = {
suggestionType: 'related',
}
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(UserSuggestionsPanel))