From d1f378f708a46b8a483782166f7464aa86d154b2 Mon Sep 17 00:00:00 2001 From: Guillaume Viger Date: Tue, 23 Apr 2024 11:26:27 -0400 Subject: [PATCH] settings-ui: [#855] set membership policy --- ...dgesForm.js => CommunityPrivilegesForm.js} | 68 ++++++++++++++++++- .../settings/privileges/index.js | 2 +- invenio_communities/config.py | 3 + invenio_communities/views/communities.py | 20 ++++++ 4 files changed, 89 insertions(+), 4 deletions(-) rename invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/{CommunityPriviledgesForm.js => CommunityPrivilegesForm.js} (70%) diff --git a/invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/CommunityPriviledgesForm.js b/invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/CommunityPrivilegesForm.js similarity index 70% rename from invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/CommunityPriviledgesForm.js rename to invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/CommunityPrivilegesForm.js index ce4d30036..c609ff091 100644 --- a/invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/CommunityPriviledgesForm.js +++ b/invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/CommunityPrivilegesForm.js @@ -10,6 +10,7 @@ import { i18next } from "@translations/invenio_communities/i18next"; import { CommunitySettingsForm } from "..//components/CommunitySettingsForm"; import _get from "lodash/get"; +import _isEmpty from "lodash/isEmpty"; import { useField } from "formik"; import React, { Component } from "react"; import { RadioField } from "react-invenio-forms"; @@ -18,17 +19,31 @@ import PropTypes from "prop-types"; const VisibilityField = ({ label, formConfig, ...props }) => { const [field] = useField(props); + const fieldPath = "access.visibility"; + + function createHandleChange(radioValue) { + function handleChange({ event, data, formikProps }) { + formikProps.form.setFieldValue(fieldPath, radioValue); + // dependent fields + if (radioValue === "restricted") { + formikProps.form.setFieldValue("access.member_policy", "closed"); + } + } + return handleChange; + } + return ( <> {formConfig.access.visibility.map((item) => ( @@ -76,14 +91,47 @@ MembersVisibilityField.defaultProps = { label: "", }; +const MemberPolicyField = ({ label, formConfig, ...props }) => { + const [field] = useField(props); + const isDisabled = _get(field.value, "access.visibility") === "restricted"; + + return ( + <> + {formConfig.access.member_policy.map((item) => ( + + + + + ))} + + ); +}; + +MemberPolicyField.propTypes = { + label: PropTypes.string, + formConfig: PropTypes.object.isRequired, +}; + +MemberPolicyField.defaultProps = { + label: "", +}; + class CommunityPrivilegesForm extends Component { getInitialValues = () => { return { access: { visibility: "public", members_visibility: "public", + member_policy: "closed", // TODO: Re-enable once properly integrated to be displayed - // member_policy: "open", // record_policy: "open", }, }; @@ -105,6 +153,7 @@ class CommunityPrivilegesForm extends Component { +
{i18next.t("Members visibility")} @@ -114,6 +163,19 @@ class CommunityPrivilegesForm extends Component {
+ + {!_isEmpty(formConfig.access.member_policy) && ( + <> +
+ {i18next.t("Membership Policy")} + + {i18next.t("Controls if anyone can request to join your community.")} + +
+ + + )} + {/* TODO: Re-enable once properly integrated to be displayed */} {/* diff --git a/invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/index.js b/invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/index.js index ecbfcbd2d..fff65cd03 100644 --- a/invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/index.js +++ b/invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/index.js @@ -1,4 +1,4 @@ -import CommunityPrivilegesForm from "./CommunityPriviledgesForm"; +import CommunityPrivilegesForm from "./CommunityPrivilegesForm"; import ReactDOM from "react-dom"; import React from "react"; diff --git a/invenio_communities/config.py b/invenio_communities/config.py index 104f94941..9119b1be9 100644 --- a/invenio_communities/config.py +++ b/invenio_communities/config.py @@ -316,3 +316,6 @@ COMMUNITIES_ALWAYS_SHOW_CREATE_LINK = False """Controls visibility of 'New Community' btn based on user's permission when set to True.""" + +COMMUNITIES_ALLOW_MEMBERSHIP_REQUESTS = True +"""Feature flag for membership request.""" diff --git a/invenio_communities/views/communities.py b/invenio_communities/views/communities.py index f6481f58c..6d9816684 100644 --- a/invenio_communities/views/communities.py +++ b/invenio_communities/views/communities.py @@ -82,6 +82,24 @@ ] +MEMBER_POLICY_FIELDS = [ + { + "text": "Open", + "value": "open", + "icon": "user plus", + "helpText": _("Users can request to join your community."), + }, + { + "text": "Closed", + "value": "closed", + "icon": "user times", + "helpText": _( + "Users cannot request to join your community. Only invited users can become members of your community." + ), + }, +] + + HEADER_PERMISSIONS = { "read", "update", @@ -307,6 +325,7 @@ def communities_settings_privileges(pid_value, community, community_ui): if not permissions["can_manage_access"]: raise PermissionDeniedError() + member_policy = MEMBER_POLICY_FIELDS if current_app.config["COMMUNITIES_ALLOW_MEMBERSHIP_REQUESTS"] else {} # noqa return render_community_theme_template( "invenio_communities/details/settings/privileges.html", theme=community_ui.get("theme", {}), @@ -315,6 +334,7 @@ def communities_settings_privileges(pid_value, community, community_ui): access=dict( visibility=VISIBILITY_FIELDS, members_visibility=MEMBERS_VISIBILITY_FIELDS, + member_policy=member_policy, ), ), permissions=permissions,