page-operation.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import urljoin from 'url-join';
  2. import { SubscriptionStatusType } from '~/interfaces/subscription';
  3. import { toastError } from '../util/apiNotification';
  4. import { apiv3Post, apiv3Put } from '../util/apiv3-client';
  5. export const toggleSubscribe = async(pageId: string, currentStatus: SubscriptionStatusType | undefined): Promise<void> => {
  6. try {
  7. const newStatus = currentStatus === SubscriptionStatusType.SUBSCRIBE
  8. ? SubscriptionStatusType.UNSUBSCRIBE
  9. : SubscriptionStatusType.SUBSCRIBE;
  10. await apiv3Put('/page/subscribe', { pageId, status: newStatus });
  11. }
  12. catch (err) {
  13. toastError(err);
  14. }
  15. };
  16. export const toggleLike = async(pageId: string, currentValue?: boolean): Promise<void> => {
  17. try {
  18. await apiv3Put('/page/likes', { pageId, bool: !currentValue });
  19. }
  20. catch (err) {
  21. toastError(err);
  22. }
  23. };
  24. export const toggleBookmark = async(pageId: string, currentValue?: boolean): Promise<void> => {
  25. try {
  26. await apiv3Put('/bookmarks', { pageId, bool: !currentValue });
  27. }
  28. catch (err) {
  29. toastError(err);
  30. }
  31. };
  32. export const toggleContentWidth = async(pageId: string, currentValue?: boolean): Promise<void> => {
  33. try {
  34. await apiv3Put('/page/content-width', { pageId, bool: !currentValue });
  35. }
  36. catch (err) {
  37. toastError(err);
  38. }
  39. };
  40. export const bookmark = async(pageId: string): Promise<void> => {
  41. try {
  42. await apiv3Put('/bookmarks', { pageId, bool: true });
  43. }
  44. catch (err) {
  45. toastError(err);
  46. }
  47. };
  48. export const unbookmark = async(pageId: string): Promise<void> => {
  49. try {
  50. await apiv3Put('/bookmarks', { pageId, bool: false });
  51. }
  52. catch (err) {
  53. toastError(err);
  54. }
  55. };
  56. export const exportAsMarkdown = (pageId: string, revisionId: string, format: string): void => {
  57. const url = new URL(urljoin(window.location.origin, '_api/v3/page/export', pageId));
  58. url.searchParams.append('format', format);
  59. url.searchParams.append('revisionId', revisionId);
  60. window.location.href = url.href;
  61. };
  62. /**
  63. * send request to fix broken paths caused by unexpected events such as server shutdown while renaming page paths
  64. */
  65. export const resumeRenameOperation = async(pageId: string): Promise<void> => {
  66. await apiv3Post('/pages/resume-rename', { pageId });
  67. };