import React, { useCallback } from 'react'; import { Collapse } from 'reactstrap'; import type AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer'; import { PageDeleteConfigValue, type IPageDeleteConfigValue, type IPageDeleteConfigValueToProcessValidation, } from '~/interfaces/page-delete-config'; import { validateDeleteConfigs, prepareDeleteConfigValuesForCalc } from '~/utils/page-delete-config'; import { DeletionType, type DeletionTypeValue, getDeletionTypeForT, getDeleteConfigValueForT, isRecursiveDeletion, isTypeDeletion, } from './types'; type Props = { adminGeneralSecurityContainer: AdminGeneralSecurityContainer; t: (key: string) => string; }; export const PageDeleteRightsSettings: React.FC = ({ adminGeneralSecurityContainer, t }) => { const { currentPageDeletionAuthority, currentPageCompleteDeletionAuthority, currentPageRecursiveDeletionAuthority, currentPageRecursiveCompleteDeletionAuthority, } = adminGeneralSecurityContainer.state; const getRecursiveDeletionConfigState = useCallback((deletionType: DeletionTypeValue) => { if (isTypeDeletion(deletionType)) { return [ adminGeneralSecurityContainer.state.currentPageRecursiveDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveDeletionAuthority, ] as const; } return [ adminGeneralSecurityContainer.state.currentPageRecursiveCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveCompleteDeletionAuthority, ] as const; }, [adminGeneralSecurityContainer]); const previousPageRecursiveAuthorityState = useCallback((deletionType: DeletionTypeValue) => { return isTypeDeletion(deletionType) ? adminGeneralSecurityContainer.state.previousPageRecursiveDeletionAuthority : adminGeneralSecurityContainer.state.previousPageRecursiveCompleteDeletionAuthority; }, [adminGeneralSecurityContainer]); const setPagePreviousRecursiveAuthorityState = useCallback((deletionType: DeletionTypeValue, previousState: IPageDeleteConfigValue | null) => { if (isTypeDeletion(deletionType)) { adminGeneralSecurityContainer.changePreviousPageRecursiveDeletionAuthority(previousState); return; } adminGeneralSecurityContainer.changePreviousPageRecursiveCompleteDeletionAuthority(previousState); }, [adminGeneralSecurityContainer]); const expandDeleteOptionsState = useCallback((deletionType: DeletionTypeValue) => { return isTypeDeletion(deletionType) ? adminGeneralSecurityContainer.state.expandOtherOptionsForDeletion : adminGeneralSecurityContainer.state.expandOtherOptionsForCompleteDeletion; }, [adminGeneralSecurityContainer]); const setExpandOtherDeleteOptionsState = useCallback((deletionType: DeletionTypeValue, bool: boolean) => { if (isTypeDeletion(deletionType)) { adminGeneralSecurityContainer.switchExpandOtherOptionsForDeletion(bool); return; } adminGeneralSecurityContainer.switchExpandOtherOptionsForCompleteDeletion(bool); }, [adminGeneralSecurityContainer]); const setDeletionConfigState = useCallback(( newState: IPageDeleteConfigValue, setState: (value: IPageDeleteConfigValue) => void, deletionType: DeletionTypeValue, ) => { setState(newState); if (previousPageRecursiveAuthorityState(deletionType) !== null) { setPagePreviousRecursiveAuthorityState(deletionType, null); } if (isRecursiveDeletion(deletionType)) { return; } const [recursiveState, setRecursiveState] = getRecursiveDeletionConfigState(deletionType); const calculableValue = prepareDeleteConfigValuesForCalc( newState as IPageDeleteConfigValueToProcessValidation, recursiveState as IPageDeleteConfigValueToProcessValidation, ); const shouldForceUpdate = !validateDeleteConfigs(calculableValue[0], calculableValue[1]); if (shouldForceUpdate) { setRecursiveState(newState); setPagePreviousRecursiveAuthorityState(deletionType, recursiveState); setExpandOtherDeleteOptionsState(deletionType, true); } }, [ getRecursiveDeletionConfigState, previousPageRecursiveAuthorityState, setPagePreviousRecursiveAuthorityState, setExpandOtherDeleteOptionsState, ]); const renderPageDeletePermissionDropdown = useCallback(( currentState: IPageDeleteConfigValue, setState: (value: IPageDeleteConfigValue) => void, deletionType: DeletionTypeValue, isButtonDisabled: boolean, ) => { return (
{isRecursiveDeletion(deletionType) ? ( ) : ( )}

{t(`security_settings.${getDeletionTypeForT(deletionType)}_explanation`)}

); }, [t, setDeletionConfigState]); const renderPageDeletePermission = useCallback(( currentState: IPageDeleteConfigValue, setState: (value: IPageDeleteConfigValue) => void, deletionType: DeletionTypeValue, isButtonDisabled: boolean, ) => { const expandDeleteOptions = expandDeleteOptionsState(deletionType); return (
{!isRecursiveDeletion(deletionType) && isTypeDeletion(deletionType) && {t('security_settings.page_delete')}} {!isRecursiveDeletion(deletionType) && !isTypeDeletion(deletionType) && {t('security_settings.page_delete_completely')}}
{!isRecursiveDeletion(deletionType) ? ( <> {renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled)} {currentState === PageDeleteConfigValue.Anyone && deletionType === DeletionType.CompleteDeletion && ( <> { adminGeneralSecurityContainer.switchIsAllGroupMembershipRequiredForPageCompleteDeletion() }} />

{t('security_settings.is_all_group_membership_required_for_page_complete_deletion_explanation')}

)} ) : ( <>

info {/* eslint-disable-next-line react/no-danger */}

{previousPageRecursiveAuthorityState(deletionType) !== null && (
{t('security_settings.forced_update_desc')} {t(getDeleteConfigValueForT(previousPageRecursiveAuthorityState(deletionType)))}
)} {renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled)}
)}
); }, [ adminGeneralSecurityContainer, expandDeleteOptionsState, previousPageRecursiveAuthorityState, renderPageDeletePermissionDropdown, setExpandOtherDeleteOptionsState, t, ]); const isButtonDisabledForDeletion = !validateDeleteConfigs(currentPageDeletionAuthority, PageDeleteConfigValue.AdminAndAuthor); const isButtonDisabledForCompleteDeletion = !validateDeleteConfigs(currentPageCompleteDeletionAuthority, PageDeleteConfigValue.AdminAndAuthor); return ( <>

{t('security_settings.page_delete_rights')}

{[ [currentPageDeletionAuthority, adminGeneralSecurityContainer.changePageDeletionAuthority, DeletionType.Deletion, false], [ currentPageRecursiveDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveDeletionAuthority, DeletionType.RecursiveDeletion, isButtonDisabledForDeletion, ], ].map(arr => renderPageDeletePermission( arr[0] as IPageDeleteConfigValue, arr[1] as (value: IPageDeleteConfigValue) => void, arr[2] as DeletionTypeValue, arr[3] as boolean, ))} {[ [currentPageCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageCompleteDeletionAuthority, DeletionType.CompleteDeletion, false], [ currentPageRecursiveCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveCompleteDeletionAuthority, DeletionType.RecursiveCompleteDeletion, isButtonDisabledForCompleteDeletion, ], ].map(arr => renderPageDeletePermission( arr[0] as IPageDeleteConfigValue, arr[1] as (value: IPageDeleteConfigValue) => void, arr[2] as DeletionTypeValue, arr[3] as boolean, ))} ); };