SubscribeButton.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React, { useState, FC } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { UncontrolledTooltip } from 'reactstrap';
  4. import { withUnstatedContainers } from './UnstatedUtils';
  5. import { toastError } from '~/client/util/apiNotification';
  6. import AppContainer from '~/client/services/AppContainer';
  7. import PageContainer from '~/client/services/PageContainer';
  8. type Props = {
  9. appContainer: AppContainer,
  10. pageContainer: PageContainer,
  11. };
  12. const SubscruibeButton: FC<Props> = (props: Props) => {
  13. const { t } = useTranslation();
  14. const [isWatching, setIsWatching] = useState(true);
  15. const { appContainer, pageContainer } = props;
  16. const handleClick = async() => {
  17. try {
  18. const res = await appContainer.apiv3Put('page/subscribe', { pageId: pageContainer.state.pageId, status: !isWatching });
  19. if (res) {
  20. const { subscription } = res.data;
  21. setIsWatching(subscription.status === 'WATCH');
  22. }
  23. }
  24. catch (err) {
  25. toastError(err);
  26. }
  27. };
  28. return (
  29. <>
  30. <button
  31. type="button"
  32. id="subscribe-button"
  33. onClick={handleClick}
  34. className={`btn btn-watch border-0 ${isWatching ? 'active' : ''} ${appContainer.isGuestUser ? 'disabled' : ''}`}
  35. >
  36. {isWatching && (
  37. <i className="fa fa-eye"></i>
  38. )}
  39. {!isWatching && (
  40. <i className="fa fa-eye-slash"></i>
  41. )}
  42. </button>
  43. {appContainer.isGuestUser && (
  44. <UncontrolledTooltip placement="top" target="subscribe-button" fade={false}>
  45. {t('Not available for guest')}
  46. </UncontrolledTooltip>
  47. )}
  48. </>
  49. );
  50. };
  51. /**
  52. * Wrapper component for using unstated
  53. */
  54. const SubscruibeButtonWrapper = withUnstatedContainers(SubscruibeButton, [AppContainer, PageContainer]);
  55. export default SubscruibeButtonWrapper;