bookmark-utils.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import type { IRevision, Ref } from '@growi/core';
  2. import type {
  3. BookmarkedPage,
  4. BookmarkFolderItems,
  5. } from '~/interfaces/bookmark-info';
  6. import { apiv3Delete, apiv3Post, apiv3Put } from './apiv3-client';
  7. // Check if bookmark folder item has childFolder or bookmarks
  8. export const hasChildren = ({
  9. childFolder,
  10. bookmarks,
  11. }: {
  12. childFolder?: BookmarkFolderItems[];
  13. bookmarks?: BookmarkedPage[];
  14. }): boolean => {
  15. return !!(
  16. (childFolder && childFolder.length > 0) ||
  17. (bookmarks && bookmarks.length > 0)
  18. );
  19. };
  20. // Add new folder helper
  21. export const addNewFolder = async (
  22. name: string,
  23. parent: string | null,
  24. ): Promise<void> => {
  25. await apiv3Post('/bookmark-folder', { name, parent });
  26. };
  27. // Put bookmark to a folder
  28. export const addBookmarkToFolder = async (
  29. pageId: string,
  30. folderId: string | null,
  31. ): Promise<void> => {
  32. await apiv3Post('/bookmark-folder/add-bookmark-to-folder', {
  33. pageId,
  34. folderId,
  35. });
  36. };
  37. // Delete bookmark folder
  38. export const deleteBookmarkFolder = async (
  39. bookmarkFolderId: string,
  40. ): Promise<void> => {
  41. await apiv3Delete(`/bookmark-folder/${bookmarkFolderId}`);
  42. };
  43. // Rename page from bookmark item control
  44. export const renamePage = async (
  45. pageId: string,
  46. revisionId: Ref<IRevision> | undefined,
  47. newPagePath: string,
  48. ): Promise<void> => {
  49. await apiv3Put('/pages/rename', { pageId, revisionId, newPagePath });
  50. };
  51. // Update bookmark by isBookmarked status
  52. export const toggleBookmark = async (
  53. pageId: string,
  54. status: boolean,
  55. ): Promise<void> => {
  56. await apiv3Put('/bookmark-folder/update-bookmark', { pageId, status });
  57. };
  58. // Update Bookmark folder
  59. export const updateBookmarkFolder = async (
  60. bookmarkFolderId: string,
  61. name: string,
  62. parent: string | null,
  63. childFolder: BookmarkFolderItems[],
  64. ): Promise<void> => {
  65. await apiv3Put('/bookmark-folder', {
  66. bookmarkFolderId,
  67. name,
  68. parent,
  69. childFolder,
  70. });
  71. };