bigbuffet-rw/app/soapbox/features/ui/components/subscription-button.tsx

107 lines
3.6 KiB
TypeScript
Raw Normal View History

import React from 'react';
2022-06-13 08:35:54 -07:00
import { defineMessages, useIntl } from 'react-intl';
import {
followAccount,
subscribeAccount,
unsubscribeAccount,
} from 'soapbox/actions/accounts';
import { IconButton } from 'soapbox/components/ui';
import { useAppDispatch, useFeatures } from 'soapbox/hooks';
2022-12-20 07:47:46 -08:00
import toast from 'soapbox/toast';
2022-06-13 08:35:54 -07:00
import type { Account as AccountEntity } from 'soapbox/types/entities';
const messages = defineMessages({
subscribe: { id: 'account.subscribe', defaultMessage: 'Subscribe to notifications from @{name}' },
unsubscribe: { id: 'account.unsubscribe', defaultMessage: 'Unsubscribe to notifications from @{name}' },
subscribeSuccess: { id: 'account.subscribe.success', defaultMessage: 'You have subscribed to this account.' },
unsubscribeSuccess: { id: 'account.unsubscribe.success', defaultMessage: 'You have unsubscribed from this account.' },
subscribeFailure: { id: 'account.subscribe.failure', defaultMessage: 'An error occurred trying to subscribe to this account.' },
unsubscribeFailure: { id: 'account.unsubscribe.failure', defaultMessage: 'An error occurred trying to unsubscribe to this account.' },
2022-06-13 08:35:54 -07:00
});
interface ISubscriptionButton {
account: AccountEntity
}
const SubscriptionButton = ({ account }: ISubscriptionButton) => {
const dispatch = useAppDispatch();
const features = useFeatures();
const intl = useIntl();
2022-06-14 08:58:42 -07:00
const isFollowing = account.relationship?.following;
const isRequested = account.relationship?.requested;
const isSubscribed = features.accountNotifies
? account.relationship?.notifying
: account.relationship?.subscribing;
const title = isSubscribed
? intl.formatMessage(messages.unsubscribe, { name: account.get('username') })
: intl.formatMessage(messages.subscribe, { name: account.get('username') });
2022-06-13 08:35:54 -07:00
const onSubscribeSuccess = () =>
2022-12-20 07:47:46 -08:00
toast.success(intl.formatMessage(messages.subscribeSuccess));
2022-06-13 08:35:54 -07:00
const onSubscribeFailure = () =>
2022-12-20 08:34:53 -08:00
toast.error(intl.formatMessage(messages.subscribeFailure));
2022-06-13 08:35:54 -07:00
const onUnsubscribeSuccess = () =>
2022-12-20 07:47:46 -08:00
toast.success(intl.formatMessage(messages.unsubscribeSuccess));
2022-06-13 08:35:54 -07:00
const onUnsubscribeFailure = () =>
2022-12-20 08:34:53 -08:00
toast.error(intl.formatMessage(messages.unsubscribeFailure));
2022-06-13 08:35:54 -07:00
const onNotifyToggle = () => {
if (account.relationship?.notifying) {
dispatch(followAccount(account.get('id'), { notify: false } as any))
?.then(() => onUnsubscribeSuccess())
.catch(() => onUnsubscribeFailure());
} else {
dispatch(followAccount(account.get('id'), { notify: true } as any))
?.then(() => onSubscribeSuccess())
.catch(() => onSubscribeFailure());
}
};
const onSubscriptionToggle = () => {
if (account.relationship?.subscribing) {
dispatch(unsubscribeAccount(account.get('id')))
?.then(() => onUnsubscribeSuccess())
.catch(() => onUnsubscribeFailure());
} else {
dispatch(subscribeAccount(account.get('id')))
?.then(() => onSubscribeSuccess())
.catch(() => onSubscribeFailure());
}
};
const handleToggle = () => {
if (features.accountNotifies) {
onNotifyToggle();
} else {
onSubscriptionToggle();
}
};
2022-06-14 08:58:42 -07:00
if (!features.accountSubscriptions && !features.accountNotifies) {
2022-06-13 08:35:54 -07:00
return null;
}
2022-06-14 08:58:42 -07:00
if (isRequested || isFollowing) {
2022-06-13 08:35:54 -07:00
return (
<IconButton
src={isSubscribed ? require('@tabler/icons/bell-ringing.svg') : require('@tabler/icons/bell.svg')}
2022-06-13 08:35:54 -07:00
onClick={handleToggle}
title={title}
2022-08-12 08:42:26 -07:00
theme='outlined'
className='px-2'
iconClassName='w-4 h-4'
2022-06-13 08:35:54 -07:00
/>
);
}
return null;
};
export default SubscriptionButton;