SearchResultSubNavButton.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, {
  2. FC, useState, useEffect,
  3. } from 'react';
  4. import AppContainer from '../../client/services/AppContainer';
  5. import { withUnstatedContainers } from '../UnstatedUtils';
  6. import BookmarkButton from '../BookmarkButton';
  7. import LikeButtons from '../LikeButtons';
  8. import PageManagement from '../Page/PageManagement';
  9. import { apiv3Get, apiv3Put } from '../../client/util/apiv3-client'; // '~/client/util/apiv3-client';
  10. import { toastError } from '../../client/util/apiNotification';
  11. type PageReactionButtonsProps = {
  12. appContainer: AppContainer,
  13. pageId: string,
  14. }
  15. const PageReactionButtons : React.FC<PageReactionButtonsProps> = (props: PageReactionButtonsProps) => {
  16. const { appContainer, pageId } = props;
  17. const LikeButtonsTypeAny : any = LikeButtons;
  18. const BookMarkButtonTypeAny: any = BookmarkButton;
  19. const [sumOflikers, setSumOfLikers] = useState(0);
  20. const [likers, setLikers] = useState<string[]>([]);
  21. const [isLiked, setIsLiked] = useState(true);
  22. // component did mount
  23. useEffect(() => {
  24. const f = async() => {
  25. const {
  26. data: { likerIds, sumOfLikers, isLiked },
  27. } = await apiv3Get('/page/info', { _id: pageId });
  28. setSumOfLikers(sumOfLikers);
  29. setLikers(likerIds);
  30. setIsLiked(isLiked);
  31. };
  32. f();
  33. }, []);
  34. const toggleLike = async() => {
  35. const { isGuestUser } = appContainer;
  36. if (isGuestUser) {
  37. return;
  38. }
  39. try {
  40. await apiv3Put('/page/likes', { pageId, bool: isLiked });
  41. }
  42. catch (err) {
  43. toastError(err);
  44. }
  45. setSumOfLikers(sumOflikers => (isLiked ? sumOflikers - 1 : sumOflikers + 1));
  46. setLikers(likerIds => (isLiked
  47. ? likerIds.filter(id => id !== appContainer.currentUserId)
  48. : [...likerIds, appContainer.currentUserId]));
  49. setIsLiked(isLiked => !isLiked);
  50. };
  51. return (
  52. <>
  53. <span>
  54. <LikeButtonsTypeAny onClickInvoked={toggleLike} likers={likers} sumOfLikers={sumOflikers} isLiked={isLiked}></LikeButtonsTypeAny>
  55. </span>
  56. <span>
  57. {/* <BookmarkButton></BookmarkButton> */}
  58. </span>
  59. </>
  60. );
  61. };
  62. type Props = {
  63. appContainer: AppContainer,
  64. isCompactMode: boolean,
  65. pageId: string,
  66. }
  67. const SearchResultSubNavButton : FC<Props> = (props: Props) => {
  68. const {
  69. appContainer, isCompactMode, pageId,
  70. } = props;
  71. return (
  72. <>
  73. <PageReactionButtons appContainer={appContainer} pageId={pageId}></PageReactionButtons>
  74. {/*
  75. TODO : https://estoc.weseek.co.jp/redmine/issues/80789
  76. CONDITION :isAbleToShowPageManagement = !isNotFoundPage && !isTrashPage && !isSharedUser
  77. */}
  78. {/* if (CONDITION) then PageReactionButtons */}
  79. </>
  80. );
  81. };
  82. const SearchResultSubNavButtonWrapper = withUnstatedContainers(SearchResultSubNavButton, [AppContainer]);
  83. export default SearchResultSubNavButtonWrapper;