SubNavButtons.jsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import AppContainer from '~/client/services/AppContainer';
  4. import NavigationContainer from '~/client/services/NavigationContainer';
  5. import PageContainer from '~/client/services/PageContainer';
  6. import { withUnstatedContainers } from '../UnstatedUtils';
  7. import BookmarkButton from '../BookmarkButton';
  8. import LikeButtons from '../LikeButtons';
  9. import PageManagement from '../Page/PageManagement';
  10. import { toastError } from '~/client/util/apiNotification';
  11. const SubnavButtons = (props) => {
  12. const {
  13. appContainer, navigationContainer, pageContainer, isCompactMode,
  14. } = props;
  15. /* eslint-enable react/prop-types */
  16. /* eslint-disable react/prop-types */
  17. const PageReactionButtons = ({ pageContainer }) => {
  18. const {
  19. state: { likers, sumOfLikers, isLiked },
  20. } = pageContainer;
  21. const toggleLike = () => {
  22. const { isGuestUser } = appContainer;
  23. if (isGuestUser) {
  24. return;
  25. }
  26. try {
  27. pageContainer.toggleLike();
  28. }
  29. catch (err) {
  30. toastError(err);
  31. }
  32. };
  33. return (
  34. <>
  35. {pageContainer.isAbleToShowLikeButtons && (
  36. <span>
  37. <LikeButtons onClickInvoked={toggleLike} likers={likers} sumOfLikers={sumOfLikers} isLiked={isLiked} />
  38. </span>
  39. )}
  40. <span>
  41. <BookmarkButton />
  42. </span>
  43. </>
  44. );
  45. };
  46. /* eslint-enable react/prop-types */
  47. const { editorMode } = navigationContainer.state;
  48. const isViewMode = editorMode === 'view';
  49. return (
  50. <>
  51. {isViewMode && (
  52. <>
  53. {pageContainer.isAbleToShowPageReactionButtons && <PageReactionButtons appContainer={appContainer} pageContainer={pageContainer} />}
  54. {pageContainer.isAbleToShowPageManagement && <PageManagement isCompactMode={isCompactMode} />}
  55. </>
  56. )}
  57. </>
  58. );
  59. };
  60. /**
  61. * Wrapper component for using unstated
  62. */
  63. const SubnavButtonsWrapper = withUnstatedContainers(SubnavButtons, [AppContainer, NavigationContainer, PageContainer]);
  64. SubnavButtons.propTypes = {
  65. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  66. navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
  67. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  68. isCompactMode: PropTypes.bool,
  69. };
  70. export default SubnavButtonsWrapper;