import React, { useState, FC } from 'react'; import toastr from 'toastr'; import { Modal, ModalHeader, ModalBody, ModalFooter, } from 'reactstrap'; import { useTranslation } from 'react-i18next'; import { apiPost } from '~/client/util/apiv1-client'; import { usePageDeleteModalStatus, usePageDeleteModalOpened } from '~/stores/ui'; import { IPageApiv1Result } from '~/interfaces/page'; import ApiErrorMessageList from './PageManagement/ApiErrorMessageList'; const deleteIconAndKey = { completely: { color: 'danger', icon: 'fire', translationKey: 'completely', }, temporary: { color: 'primary', icon: 'trash', translationKey: 'page', }, }; type Props = { isOpen: boolean, isDeleteCompletelyModal: boolean, isAbleToDeleteCompletely: boolean, onClose?: () => void, } const PageDeleteModal: FC = (props: Props) => { const { t } = useTranslation(''); const { isDeleteCompletelyModal, isAbleToDeleteCompletely, } = props; const { data: pagesDataToDelete, close: closeDeleteModal } = usePageDeleteModalStatus(); const { data: isOpened } = usePageDeleteModalOpened(); const [isDeleteRecursively, setIsDeleteRecursively] = useState(true); const [isDeleteCompletely, setIsDeleteCompletely] = useState(isDeleteCompletelyModal && isAbleToDeleteCompletely); const deleteMode = isDeleteCompletely ? 'completely' : 'temporary'; // eslint-disable-next-line @typescript-eslint/no-unused-vars const [errs, setErrs] = useState(null); function changeIsDeleteRecursivelyHandler() { setIsDeleteRecursively(!isDeleteRecursively); } function changeIsDeleteCompletelyHandler() { if (!isAbleToDeleteCompletely) { return; } setIsDeleteCompletely(!isDeleteCompletely); } async function deletePage() { // toastr.warning(t('search_result.currently_not_implemented')); // Todo implement page delete function at https://redmine.weseek.co.jp/issues/82222 // setErrs(null); if (pagesDataToDelete?.pages != null && (pagesDataToDelete.pages.length > 0)) { const pageToDelete = pagesDataToDelete?.pages[0]; try { // control flag // If is it not true, Request value must be `null`. const recursively = isDeleteRecursively != true ? true : null; const completely = isDeleteCompletely != true  ? true : null; const result = await apiPost('/pages.remove', { page_id: pageToDelete.pageId, revision_id: pageToDelete.revisionId, recursively, completely, }) as IPageApiv1Result; const trashPagePath = result.page.path; window.location.href = encodeURI(trashPagePath); } catch (err) { setErrs(err); } } } async function deleteButtonHandler() { deletePage(); } function renderDeleteRecursivelyForm() { return (
); } // DeleteCompletely is currently disabled // TODO1 : Retrive isAbleToDeleteCompleltly state everywhere in the system via swr. // Story: https://redmine.weseek.co.jp/issues/82222 // TODO2 : use toaster // TASK : https://redmine.weseek.co.jp/issues/82299 function renderDeleteCompletelyForm() { return (
{/* ↓↓ undo this comment out at https://redmine.weseek.co.jp/issues/82222 ↓↓ */} {/*
); } const renderPagePathsToDelete = () => { if (pagesDataToDelete != null && pagesDataToDelete.pages != null) { return pagesDataToDelete.pages.map(page =>
{ page.path }
); } return <>; }; return ( { t(`modal_delete.delete_${deleteIconAndKey[deleteMode].translationKey}`) }

{/* Todo: change the way to show path on modal when too many pages are selected */} {/* https://redmine.weseek.co.jp/issues/82787 */} {renderPagePathsToDelete()}
{renderDeleteRecursivelyForm()} {!isDeleteCompletelyModal && renderDeleteCompletelyForm()}
); }; export default PageDeleteModal;