Skip to content

Commit

Permalink
settings-ui: [#855] set membership policy
Browse files Browse the repository at this point in the history
  • Loading branch information
fenekku committed Jul 9, 2024
1 parent 2e1b919 commit 0b33e3f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) => (
<React.Fragment key={item.value}>
<RadioField
key={item.value}
fieldPath="access.visibility"
fieldPath={fieldPath}
label={item.text}
labelIcon={item.icon}
checked={_get(field.value, "access.visibility") === item.value}
checked={_get(field.value, fieldPath) === item.value}
value={item.value}
onChange={createHandleChange(item.value)}
/>
<label className="helptext">{item.helpText}</label>
</React.Fragment>
Expand Down Expand Up @@ -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) => (
<React.Fragment key={item.value}>
<RadioField
key={item.value}
fieldPath="access.member_policy"
label={item.text}
labelIcon={item.icon}
checked={item.value === _get(field.value, "access.member_policy")}
value={item.value}
disabled={isDisabled}
/>
<label className="helptext">{item.helpText}</label>
</React.Fragment>
))}
</>
);
};

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",
},
};
Expand All @@ -105,6 +153,7 @@ class CommunityPrivilegesForm extends Component {
</Header.Subheader>
</Header>
<VisibilityField formConfig={formConfig} />

<Header as="h2" size="small">
{i18next.t("Members visibility")}
<Header.Subheader className="mt-5">
Expand All @@ -114,6 +163,19 @@ class CommunityPrivilegesForm extends Component {
</Header.Subheader>
</Header>
<MembersVisibilityField formConfig={formConfig} />

{!_isEmpty(formConfig.access.member_policy) && (
<>
<Header as="h2" size="small">
{i18next.t("Membership Policy")}
<Header.Subheader className="mt-5">
{i18next.t("Controls if anyone can request to join your community.")}
</Header.Subheader>
</Header>
<MemberPolicyField formConfig={formConfig} />
</>
)}

{/* TODO: Re-enable once properly integrated to be displayed */}
{/*
<Grid.Column width={6}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CommunityPrivilegesForm from "./CommunityPriviledgesForm";
import CommunityPrivilegesForm from "./CommunityPrivilegesForm";
import ReactDOM from "react-dom";
import React from "react";

Expand Down
3 changes: 3 additions & 0 deletions invenio_communities/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,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 = False
"""Feature flag for membership request."""
25 changes: 25 additions & 0 deletions invenio_communities/views/communities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -341,6 +359,12 @@ 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 {}
)

return render_community_theme_template(
"invenio_communities/details/settings/privileges.html",
theme=community_ui.get("theme", {}),
Expand All @@ -349,6 +373,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,
Expand Down

0 comments on commit 0b33e3f

Please sign in to comment.