|
@@ -2,13 +2,13 @@ import React, {
|
|
|
useCallback, useMemo, useState,
|
|
useCallback, useMemo, useState,
|
|
|
} from 'react';
|
|
} from 'react';
|
|
|
|
|
|
|
|
-import { HasObjectId, IAttachment } from '@growi/core';
|
|
|
|
|
|
|
+import { IAttachmentHasId } from '^/../../packages/core/dist';
|
|
|
|
|
|
|
|
import { useSWRxAttachments } from '~/stores/attachment';
|
|
import { useSWRxAttachments } from '~/stores/attachment';
|
|
|
import { useIsGuestUser } from '~/stores/context';
|
|
import { useIsGuestUser } from '~/stores/context';
|
|
|
|
|
+import { useDeleteAttachmentModal } from '~/stores/modal';
|
|
|
import { useSWRxCurrentPage, useCurrentPageId } from '~/stores/page';
|
|
import { useSWRxCurrentPage, useCurrentPageId } from '~/stores/page';
|
|
|
|
|
|
|
|
-import { DeleteAttachmentModal } from './PageAttachment/DeleteAttachmentModal';
|
|
|
|
|
import { PageAttachmentList } from './PageAttachment/PageAttachmentList';
|
|
import { PageAttachmentList } from './PageAttachment/PageAttachmentList';
|
|
|
import PaginationWrapper from './PaginationWrapper';
|
|
import PaginationWrapper from './PaginationWrapper';
|
|
|
|
|
|
|
@@ -19,21 +19,18 @@ const checkIfFileInUse = (markdown: string, attachment): boolean => {
|
|
|
|
|
|
|
|
const PageAttachment = (): JSX.Element => {
|
|
const PageAttachment = (): JSX.Element => {
|
|
|
|
|
|
|
|
- const { data: currentPage } = useSWRxCurrentPage();
|
|
|
|
|
- const markdown = currentPage?.revision.body;
|
|
|
|
|
-
|
|
|
|
|
// Static SWRs
|
|
// Static SWRs
|
|
|
const { data: pageId } = useCurrentPageId();
|
|
const { data: pageId } = useCurrentPageId();
|
|
|
const { data: isGuestUser } = useIsGuestUser();
|
|
const { data: isGuestUser } = useIsGuestUser();
|
|
|
|
|
|
|
|
// States
|
|
// States
|
|
|
const [pageNumber, setPageNumber] = useState(1);
|
|
const [pageNumber, setPageNumber] = useState(1);
|
|
|
- const [attachmentToDelete, setAttachmentToDelete] = useState<(IAttachment & HasObjectId) | null>(null);
|
|
|
|
|
- const [deleting, setDeleting] = useState(false);
|
|
|
|
|
- const [deleteError, setDeleteError] = useState('');
|
|
|
|
|
|
|
|
|
|
// SWRs
|
|
// SWRs
|
|
|
const { data: dataAttachments, remove } = useSWRxAttachments(pageId, pageNumber);
|
|
const { data: dataAttachments, remove } = useSWRxAttachments(pageId, pageNumber);
|
|
|
|
|
+ const { open: openDeleteAttachmentModal } = useDeleteAttachmentModal();
|
|
|
|
|
+ const { data: currentPage } = useSWRxCurrentPage();
|
|
|
|
|
+ const markdown = currentPage?.revision.body;
|
|
|
|
|
|
|
|
// Custom hooks
|
|
// Custom hooks
|
|
|
const inUseAttachmentsMap: { [id: string]: boolean } | undefined = useMemo(() => {
|
|
const inUseAttachmentsMap: { [id: string]: boolean } | undefined = useMemo(() => {
|
|
@@ -54,29 +51,9 @@ const PageAttachment = (): JSX.Element => {
|
|
|
setPageNumber(newPageNumber);
|
|
setPageNumber(newPageNumber);
|
|
|
}, []);
|
|
}, []);
|
|
|
|
|
|
|
|
- const onAttachmentDeleteClicked = useCallback((attachment) => {
|
|
|
|
|
- setAttachmentToDelete(attachment);
|
|
|
|
|
- }, []);
|
|
|
|
|
-
|
|
|
|
|
- const onAttachmentDeleteClickedConfirmHandler = useCallback(async(attachment: IAttachment & HasObjectId) => {
|
|
|
|
|
- setDeleting(true);
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- await remove({ attachment_id: attachment._id });
|
|
|
|
|
-
|
|
|
|
|
- setAttachmentToDelete(null);
|
|
|
|
|
- setDeleting(false);
|
|
|
|
|
- }
|
|
|
|
|
- catch {
|
|
|
|
|
- setDeleteError('Something went wrong.');
|
|
|
|
|
- setDeleting(false);
|
|
|
|
|
- }
|
|
|
|
|
- }, [remove]);
|
|
|
|
|
-
|
|
|
|
|
- const onToggleHandler = useCallback(() => {
|
|
|
|
|
- setAttachmentToDelete(null);
|
|
|
|
|
- setDeleteError('');
|
|
|
|
|
- }, []);
|
|
|
|
|
|
|
+ const onAttachmentDeleteClicked = useCallback((attachment: IAttachmentHasId) => {
|
|
|
|
|
+ openDeleteAttachmentModal(attachment, remove);
|
|
|
|
|
+ }, [openDeleteAttachmentModal, remove]);
|
|
|
|
|
|
|
|
// Renderers
|
|
// Renderers
|
|
|
const renderPageAttachmentList = useCallback(() => {
|
|
const renderPageAttachmentList = useCallback(() => {
|
|
@@ -98,30 +75,6 @@ const PageAttachment = (): JSX.Element => {
|
|
|
);
|
|
);
|
|
|
}, [dataAttachments, inUseAttachmentsMap, isGuestUser, onAttachmentDeleteClicked]);
|
|
}, [dataAttachments, inUseAttachmentsMap, isGuestUser, onAttachmentDeleteClicked]);
|
|
|
|
|
|
|
|
- const renderDeleteAttachmentModal = useCallback(() => {
|
|
|
|
|
- if (isGuestUser) {
|
|
|
|
|
- return <></>;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (dataAttachments == null || dataAttachments.attachments.length === 0 || attachmentToDelete == null) {
|
|
|
|
|
- return <></>;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const isOpen = attachmentToDelete != null;
|
|
|
|
|
-
|
|
|
|
|
- return (
|
|
|
|
|
- <DeleteAttachmentModal
|
|
|
|
|
- isOpen={isOpen}
|
|
|
|
|
- toggle={onToggleHandler}
|
|
|
|
|
- attachmentToDelete={attachmentToDelete}
|
|
|
|
|
- deleting={deleting}
|
|
|
|
|
- deleteError={deleteError}
|
|
|
|
|
- onAttachmentDeleteClickedConfirm={onAttachmentDeleteClickedConfirmHandler}
|
|
|
|
|
- />
|
|
|
|
|
- );
|
|
|
|
|
- // eslint-disable-next-line max-len
|
|
|
|
|
- }, [attachmentToDelete, dataAttachments, deleteError, deleting, isGuestUser, onAttachmentDeleteClickedConfirmHandler, onToggleHandler]);
|
|
|
|
|
-
|
|
|
|
|
const renderPaginationWrapper = useCallback(() => {
|
|
const renderPaginationWrapper = useCallback(() => {
|
|
|
if (dataAttachments == null || dataAttachments.attachments.length === 0) {
|
|
if (dataAttachments == null || dataAttachments.attachments.length === 0) {
|
|
|
return <></>;
|
|
return <></>;
|
|
@@ -141,9 +94,6 @@ const PageAttachment = (): JSX.Element => {
|
|
|
return (
|
|
return (
|
|
|
<div data-testid="page-attachment">
|
|
<div data-testid="page-attachment">
|
|
|
{renderPageAttachmentList()}
|
|
{renderPageAttachmentList()}
|
|
|
-
|
|
|
|
|
- {renderDeleteAttachmentModal()}
|
|
|
|
|
-
|
|
|
|
|
{renderPaginationWrapper()}
|
|
{renderPaginationWrapper()}
|
|
|
</div>
|
|
</div>
|
|
|
);
|
|
);
|