PageItemControl.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import React, { FC } from 'react';
  2. import {
  3. UncontrolledDropdown, DropdownMenu, DropdownToggle, DropdownItem,
  4. } from 'reactstrap';
  5. import toastr from 'toastr';
  6. import { useTranslation } from 'react-i18next';
  7. import { IPageHasId } from '~/interfaces/page';
  8. import { apiv3Put } from '~/client/util/apiv3-client';
  9. import { toastError } from '~/client/util/apiNotification';
  10. import { useSWRBookmarkInfo } from '~/stores/bookmark';
  11. type PageItemControlProps = {
  12. page: Partial<IPageHasId>
  13. isEnableActions: boolean
  14. isDeletable: boolean
  15. onClickDeleteButton?: (pageId: string) => void
  16. }
  17. const PageItemControl: FC<PageItemControlProps> = (props: PageItemControlProps) => {
  18. const {
  19. page, isEnableActions, onClickDeleteButton, isDeletable,
  20. } = props;
  21. console.log('page', page);
  22. const { t } = useTranslation('');
  23. const { data: bookmarkInfo, error: bookmarkInfoError, mutate: mutateBookmarkInfo } = useSWRBookmarkInfo(page._id);
  24. const deleteButtonHandler = () => {
  25. if (onClickDeleteButton != null && page._id != null) {
  26. onClickDeleteButton(page._id);
  27. }
  28. };
  29. const addBookmarkClickHandler = (async() => {
  30. try {
  31. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  32. await apiv3Put('/bookmarks', { pageId: page._id, bool: true });
  33. mutateBookmarkInfo();
  34. }
  35. catch (err) {
  36. toastError(err);
  37. }
  38. });
  39. return (
  40. <UncontrolledDropdown>
  41. <DropdownToggle color="transparent" className="btn-link border-0 rounded grw-btn-page-management p-0">
  42. <i className="icon-options fa fa-rotate-90 text-muted p-1"></i>
  43. </DropdownToggle>
  44. <DropdownMenu positionFixed modifiers={{ preventOverflow: { boundariesElement: undefined } }}>
  45. {/* TODO: if there is the following button in XD add it here
  46. <button
  47. type="button"
  48. className="btn btn-link p-0"
  49. value={page.path}
  50. onClick={(e) => {
  51. window.location.href = e.currentTarget.value;
  52. }}
  53. >
  54. <i className="icon-login" />
  55. </button>
  56. */}
  57. {/*
  58. TODO: add function to the following buttons like using modal or others
  59. ref: https://estoc.weseek.co.jp/redmine/issues/79026
  60. */}
  61. {/* TODO: show dropdown when permalink section is implemented */}
  62. {!isEnableActions && (
  63. <DropdownItem>
  64. <p>
  65. {t('search_result.currently_not_implemented')}
  66. </p>
  67. </DropdownItem>
  68. )}
  69. {isEnableActions && (
  70. <DropdownItem onClick={addBookmarkClickHandler}>
  71. {/* <DropdownItem onClick={() => { console.log('hogehoge') }}> */}
  72. <i className="fa fa-fw fa-bookmark-o"></i>
  73. {t('Add to bookmark')}
  74. </DropdownItem>
  75. )}
  76. {isEnableActions && (
  77. <DropdownItem onClick={() => toastr.warning(t('search_result.currently_not_implemented'))}>
  78. <i className="icon-fw icon-docs"></i>
  79. {t('Duplicate')}
  80. </DropdownItem>
  81. )}
  82. {isEnableActions && (
  83. <DropdownItem onClick={() => toastr.warning(t('search_result.currently_not_implemented'))}>
  84. <i className="icon-fw icon-action-redo"></i>
  85. {t('Move/Rename')}
  86. </DropdownItem>
  87. )}
  88. {isDeletable && isEnableActions && (
  89. <>
  90. <DropdownItem divider />
  91. <DropdownItem className="text-danger pt-2" onClick={deleteButtonHandler}>
  92. <i className="icon-fw icon-trash"></i>
  93. {t('Delete')}
  94. </DropdownItem>
  95. </>
  96. )}
  97. </DropdownMenu>
  98. </UncontrolledDropdown>
  99. );
  100. };
  101. export default PageItemControl;