2022-01-10 14:17:52 -08:00
|
|
|
import classNames from 'classnames';
|
2021-12-30 07:13:45 -08:00
|
|
|
import PropTypes from 'prop-types';
|
2021-06-26 10:27:53 -07:00
|
|
|
import React from 'react';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2022-01-10 14:17:52 -08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import { connect } from 'react-redux';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2021-06-26 10:27:53 -07:00
|
|
|
import {
|
2021-12-30 07:13:45 -08:00
|
|
|
followAccount,
|
2021-06-26 10:27:53 -07:00
|
|
|
subscribeAccount,
|
|
|
|
unsubscribeAccount,
|
|
|
|
} from 'soapbox/actions/accounts';
|
2022-01-10 14:17:52 -08:00
|
|
|
import Icon from 'soapbox/components/icon';
|
2022-03-21 11:09:01 -07:00
|
|
|
import { Button } from 'soapbox/components/ui';
|
2021-06-26 10:27:53 -07:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
2021-06-27 09:52:12 -07:00
|
|
|
subscribe: { id: 'account.subscribe', defaultMessage: 'Subscribe to notifications from @{name}' },
|
|
|
|
unsubscribe: { id: 'account.unsubscribe', defaultMessage: 'Unsubscribe to notifications from @{name}' },
|
2021-06-27 10:46:00 -07:00
|
|
|
subscribed: { id: 'account.subscribed', defaultMessage: 'Subscribed' },
|
2021-06-26 10:27:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
const mapStateToProps = state => {
|
|
|
|
const me = state.get('me');
|
|
|
|
return {
|
|
|
|
me,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
onSubscriptionToggle(account) {
|
2022-06-04 00:22:36 -07:00
|
|
|
if (account.relationship?.subscribing) {
|
2021-06-26 10:27:53 -07:00
|
|
|
dispatch(unsubscribeAccount(account.get('id')));
|
|
|
|
} else {
|
|
|
|
dispatch(subscribeAccount(account.get('id')));
|
|
|
|
}
|
|
|
|
},
|
2021-12-30 07:13:45 -08:00
|
|
|
onNotifyToggle(account) {
|
2022-06-04 00:22:36 -07:00
|
|
|
if (account.relationship?.notifying) {
|
2021-12-30 07:13:45 -08:00
|
|
|
dispatch(followAccount(account.get('id'), { notify: false }));
|
|
|
|
} else {
|
|
|
|
dispatch(followAccount(account.get('id'), { notify: true }));
|
|
|
|
}
|
|
|
|
},
|
2021-06-26 10:27:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
export default @connect(mapStateToProps, mapDispatchToProps)
|
|
|
|
@injectIntl
|
|
|
|
class SubscriptionButton extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
2022-03-23 10:14:42 -07:00
|
|
|
account: ImmutablePropTypes.record,
|
2021-12-30 07:13:45 -08:00
|
|
|
features: PropTypes.object.isRequired,
|
2021-06-26 10:27:53 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
handleSubscriptionToggle = () => {
|
2021-12-30 07:13:45 -08:00
|
|
|
if (this.props.features.accountNotifies) this.props.onNotifyToggle(this.props.account);
|
|
|
|
else this.props.onSubscriptionToggle(this.props.account);
|
2021-06-26 10:27:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2021-12-30 07:13:45 -08:00
|
|
|
const { account, intl, features } = this.props;
|
2022-06-04 00:22:36 -07:00
|
|
|
const subscribing = features.accountNotifies ? account.relationship?.notifying : account.relationship?.subscribing;
|
|
|
|
const following = account.relationship?.following;
|
|
|
|
const requested = account.relationship?.requested;
|
2021-06-26 10:27:53 -07:00
|
|
|
|
2021-06-27 10:46:00 -07:00
|
|
|
if (requested || following) {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
className={classNames('subscription-button', subscribing && 'button-active')}
|
|
|
|
title={intl.formatMessage(subscribing ? messages.unsubscribe : messages.subscribe, { name: account.get('username') })}
|
|
|
|
onClick={this.handleSubscriptionToggle}
|
|
|
|
>
|
2021-09-26 22:00:03 -07:00
|
|
|
<Icon src={subscribing ? require('@tabler/icons/icons/bell-ringing.svg') : require('@tabler/icons/icons/bell.svg')} />
|
2021-06-27 10:46:00 -07:00
|
|
|
{subscribing && intl.formatMessage(messages.subscribed)}
|
|
|
|
</Button>
|
|
|
|
);
|
2021-06-26 10:27:53 -07:00
|
|
|
}
|
|
|
|
|
2021-06-27 10:46:00 -07:00
|
|
|
return null;
|
2021-06-26 10:27:53 -07:00
|
|
|
}
|
|
|
|
|
2021-06-27 09:35:53 -07:00
|
|
|
}
|