page-operation.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 bookmark = async(pageId: string): Promise<void> => {
  33. try {
  34. await apiv3Put('/bookmarks', { pageId, bool: true });
  35. }
  36. catch (err) {
  37. toastError(err);
  38. }
  39. };
  40. export const unbookmark = async(pageId: string): Promise<void> => {
  41. try {
  42. await apiv3Put('/bookmarks', { pageId, bool: false });
  43. }
  44. catch (err) {
  45. toastError(err);
  46. }
  47. };
  48. export const exportAsMarkdown = (pageId: string, revisionId: string, format: string): void => {
  49. const url = new URL(urljoin(window.location.origin, '_api/v3/page/export', pageId));
  50. url.searchParams.append('format', format);
  51. url.searchParams.append('revisionId', revisionId);
  52. window.location.href = url.href;
  53. };
  54. /**
  55. * send request to fix broken paths caused by unexpected events such as server shutdown while renaming page paths
  56. */
  57. export const resumeRenameOperation = async(pageId: string): Promise<void> => {
  58. await apiv3Post('/pages/resume-rename', { pageId });
  59. };