/* eslint-disable react/no-danger */ import React from 'react'; import { useTranslation } from 'next-i18next'; import PropTypes from 'prop-types'; import { Collapse } from 'reactstrap'; import AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer'; import { toastSuccess, toastError } from '~/client/util/toastr'; import { PageDeleteConfigValue } from '~/interfaces/page-delete-config'; import { validateDeleteConfigs, prepareDeleteConfigValuesForCalc } from '~/utils/page-delete-config'; import { withUnstatedContainers } from '../../UnstatedUtils'; // used as the prefix of translation const DeletionTypeForT = Object.freeze({ Deletion: 'deletion', CompleteDeletion: 'complete_deletion', RecursiveDeletion: 'recursive_deletion', RecursiveCompleteDeletion: 'recursive_complete_deletion', }); const DeletionType = Object.freeze({ Deletion: 'deletion', CompleteDeletion: 'completeDeletion', RecursiveDeletion: 'recursiveDeletion', RecursiveCompleteDeletion: 'recursiveCompleteDeletion', }); const getDeletionTypeForT = (deletionType) => { switch (deletionType) { case DeletionType.Deletion: return DeletionTypeForT.Deletion; case DeletionType.RecursiveDeletion: return DeletionTypeForT.RecursiveDeletion; case DeletionType.CompleteDeletion: return DeletionTypeForT.CompleteDeletion; case DeletionType.RecursiveCompleteDeletion: return DeletionTypeForT.RecursiveCompleteDeletion; } }; const getDeleteConfigValueForT = (DeleteConfigValue) => { switch (DeleteConfigValue) { case PageDeleteConfigValue.Anyone: case null: return 'security_settings.anyone'; case PageDeleteConfigValue.Inherit: return 'security_settings.inherit'; case PageDeleteConfigValue.AdminOnly: return 'security_settings.admin_only'; case PageDeleteConfigValue.AdminAndAuthor: return 'security_settings.admin_and_author'; } }; /** * Return true if "deletionType" is DeletionType.RecursiveDeletion or DeletionType.RecursiveCompleteDeletion. * @param deletionType Deletion type * @returns boolean */ const isRecursiveDeletion = (deletionType) => { return deletionType === DeletionType.RecursiveDeletion || deletionType === DeletionType.RecursiveCompleteDeletion; }; /** * Return true if "deletionType" is DeletionType.Deletion or DeletionType.RecursiveDeletion. * @param deletionType Deletion type * @returns boolean */ const isTypeDeletion = (deletionType) => { return deletionType === DeletionType.Deletion || deletionType === DeletionType.RecursiveDeletion; }; class SecuritySetting extends React.Component { constructor(props) { super(props); // functions this.putSecuritySetting = this.putSecuritySetting.bind(this); this.getRecursiveDeletionConfigState = this.getRecursiveDeletionConfigState.bind(this); this.previousPageRecursiveAuthorityState = this.previousPageRecursiveAuthorityState.bind(this); this.setPagePreviousRecursiveAuthorityState = this.setPagePreviousRecursiveAuthorityState.bind(this); this.expantDeleteOptionsState = this.expantDeleteOptionsState.bind(this); this.setExpantOtherDeleteOptionsState = this.setExpantOtherDeleteOptionsState.bind(this); this.setDeletionConfigState = this.setDeletionConfigState.bind(this); // render this.renderPageDeletePermission = this.renderPageDeletePermission.bind(this); this.renderPageDeletePermissionDropdown = this.renderPageDeletePermissionDropdown.bind(this); } async putSecuritySetting() { const { t, adminGeneralSecurityContainer } = this.props; try { await adminGeneralSecurityContainer.updateGeneralSecuritySetting(); toastSuccess(t('security_settings.updated_general_security_setting')); } catch (err) { toastError(err); } } getRecursiveDeletionConfigState(deletionType) { const { adminGeneralSecurityContainer } = this.props; if (isTypeDeletion(deletionType)) { return [ adminGeneralSecurityContainer.state.currentPageRecursiveDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveDeletionAuthority, ]; } return [ adminGeneralSecurityContainer.state.currentPageRecursiveCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveCompleteDeletionAuthority, ]; } previousPageRecursiveAuthorityState(deletionType) { const { adminGeneralSecurityContainer } = this.props; return isTypeDeletion(deletionType) ? adminGeneralSecurityContainer.state.previousPageRecursiveDeletionAuthority : adminGeneralSecurityContainer.state.previousPageRecursiveCompleteDeletionAuthority; } setPagePreviousRecursiveAuthorityState(deletionType, previousState) { const { adminGeneralSecurityContainer } = this.props; if (isTypeDeletion(deletionType)) { adminGeneralSecurityContainer.changePreviousPageRecursiveDeletionAuthority(previousState); return; } adminGeneralSecurityContainer.changePreviousPageRecursiveCompleteDeletionAuthority(previousState); } expantDeleteOptionsState(deletionType) { const { adminGeneralSecurityContainer } = this.props; return isTypeDeletion(deletionType) ? adminGeneralSecurityContainer.state.expandOtherOptionsForDeletion : adminGeneralSecurityContainer.state.expandOtherOptionsForCompleteDeletion; } setExpantOtherDeleteOptionsState(deletionType, bool) { const { adminGeneralSecurityContainer } = this.props; if (isTypeDeletion(deletionType)) { adminGeneralSecurityContainer.switchExpandOtherOptionsForDeletion(bool); return; } adminGeneralSecurityContainer.switchExpandOtherOptionsForCompleteDeletion(bool); return; } /** * Force update deletion config for recursive operation when the deletion config for general operation is updated. * @param deletionType Deletion type */ setDeletionConfigState(newState, setState, deletionType) { setState(newState); if (this.previousPageRecursiveAuthorityState(deletionType) !== null) { this.setPagePreviousRecursiveAuthorityState(deletionType, null); } if (isRecursiveDeletion(deletionType)) { return; } const [recursiveState, setRecursiveState] = this.getRecursiveDeletionConfigState(deletionType); const calculableValue = prepareDeleteConfigValuesForCalc(newState, recursiveState); const shouldForceUpdate = !validateDeleteConfigs(calculableValue[0], calculableValue[1]); if (shouldForceUpdate) { setRecursiveState(newState); this.setPagePreviousRecursiveAuthorityState(deletionType, recursiveState); this.setExpantOtherDeleteOptionsState(deletionType, true); } return; } renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled) { const { t } = this.props; return (
{t(`security_settings.${getDeletionTypeForT(deletionType)}_explanation`)}
{t('security_settings.is_all_group_membership_required_for_page_complete_deletion_explanation')}
> )} > ) : ( <>info {/* eslint-disable-next-line react/no-danger */}
{this.previousPageRecursiveAuthorityState(deletionType) !== null && (
{t(getDeleteConfigValueForT(this.previousPageRecursiveAuthorityState(deletionType)))}
{t('Error occurred')} : {adminGeneralSecurityContainer.retrieveError}
error
FIXED
info {t('security_settings.max_age_caution')}