|
|
@@ -1,172 +1,49 @@
|
|
|
-import React, { useCallback, useState } from 'react';
|
|
|
+import React, { useState, useCallback } from 'react';
|
|
|
|
|
|
import { pagePathUtils } from '@growi/core/dist/utils';
|
|
|
import { format } from 'date-fns';
|
|
|
-import { useRouter } from 'next/router';
|
|
|
|
|
|
-import { createPage, exist } from '~/client/services/page-operation';
|
|
|
+import { useOnTemplateButtonClicked } from '~/client/services/use-on-template-button-clicked';
|
|
|
import { toastError } from '~/client/util/toastr';
|
|
|
+import { LabelType } from '~/interfaces/template';
|
|
|
import { useCurrentUser } from '~/stores/context';
|
|
|
import { useSWRxCurrentPage } from '~/stores/page';
|
|
|
-import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
import { CreateButton } from './CreateButton';
|
|
|
import { DropendMenu } from './DropendMenu';
|
|
|
import { DropendToggle } from './DropendToggle';
|
|
|
-
|
|
|
-const logger = loggerFactory('growi:cli:PageCreateButton');
|
|
|
+import { useOnNewButtonClicked, useOnTodaysButtonClicked } from './hooks';
|
|
|
|
|
|
export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
- const router = useRouter();
|
|
|
const { data: currentPage, isLoading } = useSWRxCurrentPage();
|
|
|
const { data: currentUser } = useCurrentUser();
|
|
|
|
|
|
const [isHovered, setIsHovered] = useState(false);
|
|
|
- const [isCreating, setIsCreating] = useState(false);
|
|
|
|
|
|
const now = format(new Date(), 'yyyy/MM/dd');
|
|
|
const userHomepagePath = pagePathUtils.userHomepagePath(currentUser);
|
|
|
const todaysPath = `${userHomepagePath}/memo/${now}`;
|
|
|
|
|
|
- const onMouseEnterHandler = () => {
|
|
|
- setIsHovered(true);
|
|
|
- };
|
|
|
-
|
|
|
- const onMouseLeaveHandler = () => {
|
|
|
- setIsHovered(false);
|
|
|
- };
|
|
|
-
|
|
|
- const onClickCreateNewPageButtonHandler = useCallback(async() => {
|
|
|
- if (isLoading) return;
|
|
|
-
|
|
|
- try {
|
|
|
- setIsCreating(true);
|
|
|
-
|
|
|
- const parentPath = currentPage == null
|
|
|
- ? '/'
|
|
|
- : currentPage.path;
|
|
|
-
|
|
|
- const params = {
|
|
|
- isSlackEnabled: false,
|
|
|
- slackChannels: '',
|
|
|
- grant: 4,
|
|
|
- // grant: currentPage?.grant || 1,
|
|
|
- // grantUserGroupId: currentPage?.grantedGroup?._id,
|
|
|
- shouldGeneratePath: true,
|
|
|
- };
|
|
|
-
|
|
|
- const response = await createPage(parentPath, '', params);
|
|
|
-
|
|
|
- router.push(`${response.page.id}#edit`);
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
- logger.warn(err);
|
|
|
- toastError(err);
|
|
|
- }
|
|
|
- finally {
|
|
|
- setIsCreating(false);
|
|
|
- }
|
|
|
- }, [currentPage, isLoading, router]);
|
|
|
-
|
|
|
- const onClickCreateTodaysButtonHandler = useCallback(async() => {
|
|
|
- if (currentUser == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- setIsCreating(true);
|
|
|
-
|
|
|
- // TODO: get grant, grantUserGroupId data from parent page
|
|
|
- // https://redmine.weseek.co.jp/issues/133892
|
|
|
- const params = {
|
|
|
- isSlackEnabled: false,
|
|
|
- slackChannels: '',
|
|
|
- grant: 4,
|
|
|
- };
|
|
|
-
|
|
|
- const res = await exist(JSON.stringify([todaysPath]));
|
|
|
- if (!res.pages[todaysPath]) {
|
|
|
- await createPage(todaysPath, '', params);
|
|
|
- }
|
|
|
-
|
|
|
- router.push(`${todaysPath}#edit`);
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
- logger.warn(err);
|
|
|
- toastError(err);
|
|
|
- }
|
|
|
- finally {
|
|
|
- setIsCreating(false);
|
|
|
- }
|
|
|
- }, [currentUser, router, todaysPath]);
|
|
|
-
|
|
|
- const onClickTemplateForChildrenButtonHandler = useCallback(async() => {
|
|
|
- if (isLoading) return;
|
|
|
+ const { onClickHandler: onClickNewButton, isPageCreating: isNewPageCreating } = useOnNewButtonClicked(isLoading, currentPage);
|
|
|
+ const { onClickHandler: onClickTodaysButton, isPageCreating: isTodaysPageCreating } = useOnTodaysButtonClicked(todaysPath, currentUser);
|
|
|
+ const { onClickHandler: onClickTemplateButton, isPageCreating: isTemplatePageCreating } = useOnTemplateButtonClicked(currentPage?.path);
|
|
|
|
|
|
+ const onClickTemplateButtonHandler = useCallback(async(label: LabelType) => {
|
|
|
try {
|
|
|
- setIsCreating(true);
|
|
|
-
|
|
|
- const path = currentPage == null || currentPage.path === '/'
|
|
|
- ? '/_template'
|
|
|
- : `${currentPage.path}/_template`;
|
|
|
-
|
|
|
- const params = {
|
|
|
- isSlackEnabled: false,
|
|
|
- slackChannels: '',
|
|
|
- grant: 4,
|
|
|
- // grant: currentPage?.grant || 1,
|
|
|
- // grantUserGroupId: currentPage?.grantedGroup?._id,
|
|
|
- };
|
|
|
-
|
|
|
- const res = await exist(JSON.stringify([path]));
|
|
|
- if (!res.pages[path]) {
|
|
|
- await createPage(path, '', params);
|
|
|
- }
|
|
|
-
|
|
|
- router.push(`${path}#edit`);
|
|
|
+ await onClickTemplateButton(label);
|
|
|
}
|
|
|
catch (err) {
|
|
|
- logger.warn(err);
|
|
|
toastError(err);
|
|
|
}
|
|
|
- finally {
|
|
|
- setIsCreating(false);
|
|
|
- }
|
|
|
- }, [currentPage, isLoading, router]);
|
|
|
-
|
|
|
- const onClickTemplateForDescendantsButtonHandler = useCallback(async() => {
|
|
|
- if (isLoading) return;
|
|
|
-
|
|
|
- try {
|
|
|
- setIsCreating(true);
|
|
|
+ }, [onClickTemplateButton]);
|
|
|
|
|
|
- const path = currentPage == null || currentPage.path === '/'
|
|
|
- ? '/__template'
|
|
|
- : `${currentPage.path}/__template`;
|
|
|
-
|
|
|
- const params = {
|
|
|
- isSlackEnabled: false,
|
|
|
- slackChannels: '',
|
|
|
- grant: 4,
|
|
|
- // grant: currentPage?.grant || 1,
|
|
|
- // grantUserGroupId: currentPage?.grantedGroup?._id,
|
|
|
- };
|
|
|
-
|
|
|
- const res = await exist(JSON.stringify([path]));
|
|
|
- if (!res.pages[path]) {
|
|
|
- await createPage(path, '', params);
|
|
|
- }
|
|
|
+ const onMouseEnterHandler = () => {
|
|
|
+ setIsHovered(true);
|
|
|
+ };
|
|
|
|
|
|
- router.push(`${path}#edit`);
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
- logger.warn(err);
|
|
|
- toastError(err);
|
|
|
- }
|
|
|
- finally {
|
|
|
- setIsCreating(false);
|
|
|
- }
|
|
|
- }, [currentPage, isLoading, router]);
|
|
|
+ const onMouseLeaveHandler = () => {
|
|
|
+ setIsHovered(false);
|
|
|
+ };
|
|
|
|
|
|
return (
|
|
|
<div
|
|
|
@@ -177,8 +54,8 @@ export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
<div className="btn-group flex-grow-1">
|
|
|
<CreateButton
|
|
|
className="z-2"
|
|
|
- onClick={onClickCreateNewPageButtonHandler}
|
|
|
- disabled={isCreating}
|
|
|
+ onClick={onClickNewButton}
|
|
|
+ disabled={isNewPageCreating || isTodaysPageCreating || isTemplatePageCreating}
|
|
|
/>
|
|
|
</div>
|
|
|
{ isHovered && (
|
|
|
@@ -190,10 +67,9 @@ export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
/>
|
|
|
<DropendMenu
|
|
|
todaysPath={todaysPath}
|
|
|
- onClickCreateNewPageButtonHandler={onClickCreateNewPageButtonHandler}
|
|
|
- onClickCreateTodaysButtonHandler={onClickCreateTodaysButtonHandler}
|
|
|
- onClickTemplateForChildrenButtonHandler={onClickTemplateForChildrenButtonHandler}
|
|
|
- onClickTemplateForDescendantsButtonHandler={onClickTemplateForDescendantsButtonHandler}
|
|
|
+ onClickCreateNewPageButtonHandler={onClickNewButton}
|
|
|
+ onClickCreateTodaysButtonHandler={onClickTodaysButton}
|
|
|
+ onClickTemplateButtonHandler={onClickTemplateButtonHandler}
|
|
|
/>
|
|
|
</div>
|
|
|
)}
|