hooks.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { useCallback, useState } from 'react';
  2. import type { PageGrant, IGrantedGroup } from '@growi/core';
  3. import { useRouter } from 'next/router';
  4. import { createPage, exist } from '~/client/services/page-operation';
  5. import { toastError } from '~/client/util/toastr';
  6. export const useOnNewButtonClicked = (
  7. currentPagePath?: string,
  8. currentPageGrant?: PageGrant,
  9. currentPageGrantedGroups?: IGrantedGroup[],
  10. isLoading?: boolean,
  11. ): {
  12. onClickHandler: () => Promise<void>,
  13. isPageCreating: boolean
  14. } => {
  15. const router = useRouter();
  16. const [isPageCreating, setIsPageCreating] = useState(false);
  17. const onClickHandler = useCallback(async() => {
  18. if (isLoading) return;
  19. try {
  20. setIsPageCreating(true);
  21. /**
  22. * !! NOTICE !! - Verification of page createable or not is checked on the server side.
  23. * since the new page path is not generated on the client side.
  24. * need shouldGeneratePath flag.
  25. */
  26. const shouldUseRootPath = currentPagePath == null || currentPageGrant == null;
  27. const parentPath = shouldUseRootPath
  28. ? '/'
  29. : currentPagePath;
  30. const params = {
  31. isSlackEnabled: false,
  32. slackChannels: '',
  33. grant: shouldUseRootPath ? 1 : currentPageGrant,
  34. grantUserGroupIds: shouldUseRootPath ? undefined : currentPageGrantedGroups,
  35. shouldGeneratePath: true,
  36. };
  37. // !! NOTICE !! - if shouldGeneratePath is flagged, send the parent page path
  38. const response = await createPage(parentPath, '', params);
  39. router.push(`/${response.page.id}#edit`);
  40. }
  41. catch (err) {
  42. toastError(err);
  43. }
  44. finally {
  45. setIsPageCreating(false);
  46. }
  47. }, [currentPageGrant, currentPageGrantedGroups, currentPagePath, isLoading, router]);
  48. return { onClickHandler, isPageCreating };
  49. };
  50. export const useOnTodaysButtonClicked = (
  51. todaysPath: string | null,
  52. ): {
  53. onClickHandler: () => Promise<void>,
  54. isPageCreating: boolean
  55. } => {
  56. const router = useRouter();
  57. const [isPageCreating, setIsPageCreating] = useState(false);
  58. const onClickHandler = useCallback(async() => {
  59. if (todaysPath == null) {
  60. return;
  61. }
  62. try {
  63. setIsPageCreating(true);
  64. // TODO: get grant, grantUserGroupId data from parent page
  65. // https://redmine.weseek.co.jp/issues/133892
  66. const params = {
  67. isSlackEnabled: false,
  68. slackChannels: '',
  69. grant: 4,
  70. };
  71. const res = await exist(JSON.stringify([todaysPath]));
  72. if (!res.pages[todaysPath]) {
  73. await createPage(todaysPath, '', params);
  74. }
  75. router.push(`${todaysPath}#edit`);
  76. }
  77. catch (err) {
  78. toastError(err);
  79. }
  80. finally {
  81. setIsPageCreating(false);
  82. }
  83. }, [router, todaysPath]);
  84. return { onClickHandler, isPageCreating };
  85. };