2020-07-28 18:01:16 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import Button from 'soapbox/components/button';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import {
|
|
|
|
followAccount,
|
|
|
|
unfollowAccount,
|
|
|
|
blockAccount,
|
|
|
|
unblockAccount,
|
|
|
|
} from 'soapbox/actions/accounts';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' },
|
|
|
|
follow: { id: 'account.follow', defaultMessage: 'Follow' },
|
2020-08-11 10:49:15 -07:00
|
|
|
remote_follow: { id: 'account.remote_follow', defaultMessage: 'Remote follow' },
|
2020-07-28 18:01:16 -07:00
|
|
|
requested: { id: 'account.requested', defaultMessage: 'Awaiting approval. Click to cancel follow request' },
|
2020-08-10 10:23:59 -07:00
|
|
|
requested_small: { id: 'account.requested_small', defaultMessage: 'Awaiting approval' },
|
2020-07-28 18:01:16 -07:00
|
|
|
unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
|
|
|
|
edit_profile: { id: 'account.edit_profile', defaultMessage: 'Edit profile' },
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapStateToProps = state => {
|
|
|
|
const me = state.get('me');
|
|
|
|
return {
|
|
|
|
me,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
onFollow(account) {
|
|
|
|
if (account.getIn(['relationship', 'following']) || account.getIn(['relationship', 'requested'])) {
|
|
|
|
dispatch(unfollowAccount(account.get('id')));
|
|
|
|
} else {
|
|
|
|
dispatch(followAccount(account.get('id')));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onBlock(account) {
|
|
|
|
if (account.getIn(['relationship', 'blocking'])) {
|
|
|
|
dispatch(unblockAccount(account.get('id')));
|
|
|
|
} else {
|
|
|
|
dispatch(blockAccount(account.get('id')));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default @connect(mapStateToProps, mapDispatchToProps)
|
|
|
|
@injectIntl
|
|
|
|
class ActionButton extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
account: ImmutablePropTypes.map.isRequired,
|
|
|
|
onFollow: PropTypes.func.isRequired,
|
|
|
|
onBlock: PropTypes.func.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
2020-08-10 10:23:59 -07:00
|
|
|
small: PropTypes.bool,
|
2020-07-28 18:01:16 -07:00
|
|
|
};
|
|
|
|
|
2020-08-10 10:23:59 -07:00
|
|
|
static defaultProps = {
|
|
|
|
small: false,
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:01:16 -07:00
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener('resize', this.handleResize, { passive: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.handleResize);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFollow = () => {
|
|
|
|
this.props.onFollow(this.props.account);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBlock = () => {
|
|
|
|
this.props.onBlock(this.props.account);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-08-10 10:23:59 -07:00
|
|
|
const { account, intl, me, small } = this.props;
|
2021-01-27 17:38:05 -08:00
|
|
|
const empty = <></>;
|
2020-07-28 18:01:16 -07:00
|
|
|
|
2020-08-11 10:49:15 -07:00
|
|
|
if (!me) {
|
2021-01-27 17:38:05 -08:00
|
|
|
// Remote follow
|
|
|
|
return (<form method='POST' action='/main/ostatus'>
|
|
|
|
<input type='hidden' name='nickname' value={account.get('username')} />
|
|
|
|
<input type='hidden' name='profile' value='' />
|
|
|
|
<Button className='logo-button' text={intl.formatMessage(messages.remote_follow)} click='submit' />
|
|
|
|
</form>);
|
2020-08-11 10:49:15 -07:00
|
|
|
}
|
2020-07-28 18:01:16 -07:00
|
|
|
|
|
|
|
if (me !== account.get('id')) {
|
|
|
|
if (!account.get('relationship')) { // Wait until the relationship is loaded
|
2021-01-27 17:38:05 -08:00
|
|
|
return empty;
|
2020-07-28 18:01:16 -07:00
|
|
|
} else if (account.getIn(['relationship', 'requested'])) {
|
2021-01-27 17:38:05 -08:00
|
|
|
// Awaiting acceptance
|
|
|
|
return <Button className='logo-button' text={small ? intl.formatMessage(messages.requested_small) : intl.formatMessage(messages.requested)} onClick={this.handleFollow} />;
|
2020-07-28 18:01:16 -07:00
|
|
|
} else if (!account.getIn(['relationship', 'blocking'])) {
|
2021-01-27 17:38:05 -08:00
|
|
|
// Follow & Unfollow
|
|
|
|
return (<Button
|
|
|
|
disabled={account.getIn(['relationship', 'blocked_by'])}
|
|
|
|
className={classNames('logo-button', {
|
|
|
|
'button--destructive': account.getIn(['relationship', 'following']),
|
|
|
|
})}
|
|
|
|
text={intl.formatMessage(account.getIn(['relationship', 'following']) ? messages.unfollow : messages.follow)}
|
|
|
|
onClick={this.handleFollow}
|
|
|
|
/>);
|
2020-07-28 18:01:16 -07:00
|
|
|
} else if (account.getIn(['relationship', 'blocking'])) {
|
2021-01-27 17:38:05 -08:00
|
|
|
// Unblock
|
|
|
|
return <Button className='logo-button' text={intl.formatMessage(messages.unblock, { name: account.get('username') })} onClick={this.handleBlock} />;
|
2020-07-28 18:01:16 -07:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-27 17:38:05 -08:00
|
|
|
// Edit profile
|
|
|
|
return <Button className='logo-button' text={intl.formatMessage(messages.edit_profile)} to='/settings/profile' />;
|
2020-07-28 18:01:16 -07:00
|
|
|
}
|
2021-01-27 17:38:05 -08:00
|
|
|
return empty;
|
2020-07-28 18:01:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|