2020-03-27 13:59:38 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2021-09-09 11:05:54 -07:00
|
|
|
import { fetchFavouritedStatuses, expandFavouritedStatuses, fetchUserFavouritedStatuses, expandUserFavouritedStatuses } from '../../actions/favourites';
|
2020-03-27 13:59:38 -07:00
|
|
|
import Column from '../ui/components/column';
|
|
|
|
import StatusList from '../../components/status_list';
|
|
|
|
import { injectIntl, FormattedMessage } from 'react-intl';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import { debounce } from 'lodash';
|
2020-05-28 15:52:07 -07:00
|
|
|
import MissingIndicator from 'soapbox/components/missing_indicator';
|
2021-09-09 11:05:54 -07:00
|
|
|
import { fetchAccount, fetchAccountByUsername } from '../../actions/accounts';
|
|
|
|
import LoadingIndicator from '../../components/loading_indicator';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-07-02 13:06:27 -07:00
|
|
|
const mapStateToProps = (state, { params }) => {
|
|
|
|
const username = params.username || '';
|
2020-04-17 15:14:04 -07:00
|
|
|
const me = state.get('me');
|
|
|
|
const meUsername = state.getIn(['accounts', me, 'username']);
|
2021-09-09 11:05:54 -07:00
|
|
|
|
|
|
|
const isMyAccount = (username.toLowerCase() === meUsername.toLowerCase());
|
|
|
|
|
|
|
|
if (isMyAccount) {
|
|
|
|
return {
|
|
|
|
isMyAccount,
|
|
|
|
statusIds: state.getIn(['status_lists', 'favourites', 'items']),
|
|
|
|
isLoading: state.getIn(['status_lists', 'favourites', 'isLoading'], true),
|
|
|
|
hasMore: !!state.getIn(['status_lists', 'favourites', 'next']),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const accounts = state.getIn(['accounts']);
|
|
|
|
const accountFetchError = (state.getIn(['accounts', -1, 'username'], '').toLowerCase() === username.toLowerCase());
|
|
|
|
|
|
|
|
let accountId = -1;
|
|
|
|
if (accountFetchError) {
|
|
|
|
accountId = null;
|
|
|
|
} else {
|
|
|
|
const account = accounts.find(acct => username.toLowerCase() === acct.getIn(['acct'], '').toLowerCase());
|
|
|
|
accountId = account ? account.getIn(['id'], null) : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isBlocked = state.getIn(['relationships', accountId, 'blocked_by'], false);
|
|
|
|
const unavailable = (me === accountId) ? false : isBlocked;
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
return {
|
2021-09-09 11:05:54 -07:00
|
|
|
isMyAccount,
|
|
|
|
accountId,
|
|
|
|
unavailable,
|
|
|
|
username,
|
|
|
|
isAccount: !!state.getIn(['accounts', accountId]),
|
|
|
|
statusIds: state.getIn(['status_lists', `favourites:${accountId}`, 'items'], []),
|
|
|
|
isLoading: state.getIn(['status_lists', `favourites:${accountId}`, 'isLoading'], true),
|
|
|
|
hasMore: !!state.getIn(['status_lists', `favourites:${accountId}`, 'next']),
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default @connect(mapStateToProps)
|
|
|
|
@injectIntl
|
|
|
|
class Favourites extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
2021-07-08 14:12:04 -07:00
|
|
|
statusIds: ImmutablePropTypes.orderedSet.isRequired,
|
2020-03-27 13:59:38 -07:00
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
hasMore: PropTypes.bool,
|
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
isMyAccount: PropTypes.bool.isRequired,
|
|
|
|
};
|
|
|
|
|
2020-06-17 18:42:30 -07:00
|
|
|
componentDidMount() {
|
2021-09-09 11:05:54 -07:00
|
|
|
const { accountId, isMyAccount, username } = this.props;
|
|
|
|
|
|
|
|
if (isMyAccount)
|
|
|
|
this.props.dispatch(fetchFavouritedStatuses());
|
|
|
|
else {
|
|
|
|
if (accountId && accountId !== -1) {
|
|
|
|
this.props.dispatch(fetchAccount(accountId));
|
|
|
|
this.props.dispatch(fetchUserFavouritedStatuses(accountId));
|
|
|
|
} else {
|
|
|
|
this.props.dispatch(fetchAccountByUsername(username));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const { accountId, isMyAccount } = this.props;
|
|
|
|
|
|
|
|
if (!isMyAccount && accountId && accountId !== -1 && (accountId !== prevProps.accountId && accountId)) {
|
|
|
|
this.props.dispatch(fetchAccount(accountId));
|
|
|
|
this.props.dispatch(fetchUserFavouritedStatuses(accountId));
|
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
handleLoadMore = debounce(() => {
|
2021-09-09 11:05:54 -07:00
|
|
|
const { accountId, isMyAccount } = this.props;
|
|
|
|
|
|
|
|
if (isMyAccount) {
|
|
|
|
this.props.dispatch(expandFavouritedStatuses());
|
|
|
|
} else {
|
|
|
|
this.props.dispatch(expandUserFavouritedStatuses(accountId));
|
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
}, 300, { leading: true })
|
|
|
|
|
2020-04-14 14:47:35 -07:00
|
|
|
render() {
|
2021-09-09 11:05:54 -07:00
|
|
|
const { statusIds, isLoading, hasMore, isMyAccount, isAccount, accountId, unavailable } = this.props;
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-09-09 11:05:54 -07:00
|
|
|
if (!isMyAccount && !isAccount && accountId !== -1) {
|
2020-03-27 13:59:38 -07:00
|
|
|
return (
|
|
|
|
<Column>
|
|
|
|
<MissingIndicator />
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-09 11:05:54 -07:00
|
|
|
if (accountId === -1) {
|
|
|
|
return (
|
|
|
|
<Column>
|
|
|
|
<LoadingIndicator />
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unavailable) {
|
|
|
|
return (
|
|
|
|
<Column>
|
|
|
|
<div className='empty-column-indicator'>
|
|
|
|
<FormattedMessage id='empty_column.account_unavailable' defaultMessage='Profile unavailable' />
|
|
|
|
</div>
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const emptyMessage = isMyAccount
|
|
|
|
? <FormattedMessage id='empty_column.favourited_statuses' defaultMessage="You don't have any liked posts yet. When you like one, it will show up here." />
|
|
|
|
: <FormattedMessage id='empty_column.account_favourited_statuses' defaultMessage="This user doesn't have any liked posts yet." />;
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Column>
|
|
|
|
<StatusList
|
|
|
|
statusIds={statusIds}
|
|
|
|
scrollKey='favourited_statuses'
|
|
|
|
hasMore={hasMore}
|
|
|
|
isLoading={isLoading}
|
|
|
|
onLoadMore={this.handleLoadMore}
|
|
|
|
emptyMessage={emptyMessage}
|
|
|
|
/>
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|