|
@@ -1,9 +1,12 @@
|
|
|
import React, { useCallback, useState } from 'react';
|
|
import React, { useCallback, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
+import { pagePathUtils } from '@growi/core/dist/utils';
|
|
|
|
|
+import { format } from 'date-fns';
|
|
|
import { useRouter } from 'next/router';
|
|
import { useRouter } from 'next/router';
|
|
|
|
|
|
|
|
import { createPage } from '~/client/services/page-operation';
|
|
import { createPage } from '~/client/services/page-operation';
|
|
|
import { toastError } from '~/client/util/toastr';
|
|
import { toastError } from '~/client/util/toastr';
|
|
|
|
|
+import { useCurrentUser } from '~/stores/context';
|
|
|
import { useSWRxCurrentPage } from '~/stores/page';
|
|
import { useSWRxCurrentPage } from '~/stores/page';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
@@ -12,6 +15,7 @@ const logger = loggerFactory('growi:cli:PageCreateButton');
|
|
|
export const PageCreateButton = React.memo((): JSX.Element => {
|
|
export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
const router = useRouter();
|
|
const router = useRouter();
|
|
|
const { data: currentPage, isLoading } = useSWRxCurrentPage();
|
|
const { data: currentPage, isLoading } = useSWRxCurrentPage();
|
|
|
|
|
+ const { data: currentUser } = useCurrentUser();
|
|
|
|
|
|
|
|
const [isHovered, setIsHovered] = useState(false);
|
|
const [isHovered, setIsHovered] = useState(false);
|
|
|
const [isCreating, setIsCreating] = useState(false);
|
|
const [isCreating, setIsCreating] = useState(false);
|
|
@@ -24,7 +28,7 @@ export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
setIsHovered(false);
|
|
setIsHovered(false);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- const onCreateNewPageButtonHandler = useCallback(async() => {
|
|
|
|
|
|
|
+ const onClickCreateNewPageButtonHandler = useCallback(async() => {
|
|
|
if (isLoading) return;
|
|
if (isLoading) return;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
@@ -55,15 +59,109 @@ export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
setIsCreating(false);
|
|
setIsCreating(false);
|
|
|
}
|
|
}
|
|
|
}, [currentPage, isLoading, router]);
|
|
}, [currentPage, isLoading, router]);
|
|
|
- const onCreateTodaysButtonHandler = useCallback(() => {
|
|
|
|
|
- // router.push(`${router.pathname}#edit`);
|
|
|
|
|
- }, [router]);
|
|
|
|
|
- const onTemplateForChildrenButtonHandler = useCallback(() => {
|
|
|
|
|
- // router.push(`${router.pathname}/_template#edit`);
|
|
|
|
|
- }, [router]);
|
|
|
|
|
- const onTemplateForDescendantsButtonHandler = useCallback(() => {
|
|
|
|
|
- // router.push(`${router.pathname}/__template#edit`);
|
|
|
|
|
- }, [router]);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ const onClickCreateTodaysButtonHandler = useCallback(async() => {
|
|
|
|
|
+ if (currentUser == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ setIsCreating(true);
|
|
|
|
|
+
|
|
|
|
|
+ const now = format(new Date(), 'yyyy/MM/dd');
|
|
|
|
|
+ const userHomepagePath = pagePathUtils.userHomepagePath(currentUser);
|
|
|
|
|
+ const todaysPath = `${userHomepagePath}/memo/${now}`;
|
|
|
|
|
+
|
|
|
|
|
+ // TODO: get grant, grantUserGroupId data from parent page
|
|
|
|
|
+ // https://redmine.weseek.co.jp/issues/133892
|
|
|
|
|
+ const params = {
|
|
|
|
|
+ isSlackEnabled: false,
|
|
|
|
|
+ slackChannels: '',
|
|
|
|
|
+ grant: 1,
|
|
|
|
|
+ pageTags: [],
|
|
|
|
|
+ shouldReturnIfPathExists: true,
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ await createPage(todaysPath, '', params);
|
|
|
|
|
+
|
|
|
|
|
+ router.push(`${todaysPath}#edit`);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ logger.warn(err);
|
|
|
|
|
+ toastError(err);
|
|
|
|
|
+ }
|
|
|
|
|
+ finally {
|
|
|
|
|
+ setIsCreating(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [currentUser, router]);
|
|
|
|
|
+
|
|
|
|
|
+ const onClickTemplateForChildrenButtonHandler = useCallback(async() => {
|
|
|
|
|
+ if (isLoading) return;
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ setIsCreating(true);
|
|
|
|
|
+
|
|
|
|
|
+ const parentPath = currentPage == null
|
|
|
|
|
+ ? '/'
|
|
|
|
|
+ : currentPage.path;
|
|
|
|
|
+
|
|
|
|
|
+ const path = `${parentPath}/_template`;
|
|
|
|
|
+
|
|
|
|
|
+ const params = {
|
|
|
|
|
+ isSlackEnabled: false,
|
|
|
|
|
+ slackChannels: '',
|
|
|
|
|
+ grant: currentPage?.grant || 1,
|
|
|
|
|
+ pageTags: [],
|
|
|
|
|
+ grantUserGroupId: currentPage?.grantedGroup?._id,
|
|
|
|
|
+ shouldReturnIfPathExists: true,
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ await createPage(path, '', params);
|
|
|
|
|
+
|
|
|
|
|
+ router.push(`${path}#edit`);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ logger.warn(err);
|
|
|
|
|
+ toastError(err);
|
|
|
|
|
+ }
|
|
|
|
|
+ finally {
|
|
|
|
|
+ setIsCreating(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [currentPage, isLoading, router]);
|
|
|
|
|
+
|
|
|
|
|
+ const onClickTemplateForDescendantsButtonHandler = useCallback(async() => {
|
|
|
|
|
+ if (isLoading) return;
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ setIsCreating(true);
|
|
|
|
|
+
|
|
|
|
|
+ const parentPath = currentPage == null
|
|
|
|
|
+ ? '/'
|
|
|
|
|
+ : currentPage.path;
|
|
|
|
|
+
|
|
|
|
|
+ const path = `${parentPath}/__template`;
|
|
|
|
|
+
|
|
|
|
|
+ const params = {
|
|
|
|
|
+ isSlackEnabled: false,
|
|
|
|
|
+ slackChannels: '',
|
|
|
|
|
+ grant: currentPage?.grant || 1,
|
|
|
|
|
+ pageTags: [],
|
|
|
|
|
+ grantUserGroupId: currentPage?.grantedGroup?._id,
|
|
|
|
|
+ shouldReturnIfPathExists: true,
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ await createPage(path, '', params);
|
|
|
|
|
+
|
|
|
|
|
+ router.push(`${path}#edit`);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ logger.warn(err);
|
|
|
|
|
+ toastError(err);
|
|
|
|
|
+ }
|
|
|
|
|
+ finally {
|
|
|
|
|
+ setIsCreating(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [currentPage, isLoading, router]);
|
|
|
|
|
|
|
|
// TODO: update button design
|
|
// TODO: update button design
|
|
|
// https://redmine.weseek.co.jp/issues/132683
|
|
// https://redmine.weseek.co.jp/issues/132683
|
|
@@ -78,7 +176,7 @@ export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
<div className="btn-group">
|
|
<div className="btn-group">
|
|
|
<button
|
|
<button
|
|
|
className="d-block btn btn-primary"
|
|
className="d-block btn btn-primary"
|
|
|
- onClick={onCreateNewPageButtonHandler}
|
|
|
|
|
|
|
+ onClick={onClickCreateNewPageButtonHandler}
|
|
|
type="button"
|
|
type="button"
|
|
|
data-testid="grw-sidebar-nav-page-create-button"
|
|
data-testid="grw-sidebar-nav-page-create-button"
|
|
|
disabled={isCreating}
|
|
disabled={isCreating}
|
|
@@ -98,7 +196,7 @@ export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
<li>
|
|
<li>
|
|
|
<button
|
|
<button
|
|
|
className="dropdown-item"
|
|
className="dropdown-item"
|
|
|
- onClick={onCreateNewPageButtonHandler}
|
|
|
|
|
|
|
+ onClick={onClickCreateNewPageButtonHandler}
|
|
|
type="button"
|
|
type="button"
|
|
|
disabled={isCreating}
|
|
disabled={isCreating}
|
|
|
>
|
|
>
|
|
@@ -112,7 +210,7 @@ export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
<li>
|
|
<li>
|
|
|
<button
|
|
<button
|
|
|
className="dropdown-item"
|
|
className="dropdown-item"
|
|
|
- onClick={onCreateTodaysButtonHandler}
|
|
|
|
|
|
|
+ onClick={onClickCreateTodaysButtonHandler}
|
|
|
type="button"
|
|
type="button"
|
|
|
>
|
|
>
|
|
|
Create today's
|
|
Create today's
|
|
@@ -123,7 +221,7 @@ export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
<li>
|
|
<li>
|
|
|
<button
|
|
<button
|
|
|
className="dropdown-item"
|
|
className="dropdown-item"
|
|
|
- onClick={onTemplateForChildrenButtonHandler}
|
|
|
|
|
|
|
+ onClick={onClickTemplateForChildrenButtonHandler}
|
|
|
type="button"
|
|
type="button"
|
|
|
>
|
|
>
|
|
|
Template for children
|
|
Template for children
|
|
@@ -132,7 +230,7 @@ export const PageCreateButton = React.memo((): JSX.Element => {
|
|
|
<li>
|
|
<li>
|
|
|
<button
|
|
<button
|
|
|
className="dropdown-item"
|
|
className="dropdown-item"
|
|
|
- onClick={onTemplateForDescendantsButtonHandler}
|
|
|
|
|
|
|
+ onClick={onClickTemplateForDescendantsButtonHandler}
|
|
|
type="button"
|
|
type="button"
|
|
|
>
|
|
>
|
|
|
Template for descendants
|
|
Template for descendants
|