Skip to content

Commit 0b33e3f

Browse files
committed
settings-ui: [inveniosoftware#855] set membership policy
1 parent 2e1b919 commit 0b33e3f

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

Diff for: invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/CommunityPriviledgesForm.js renamed to invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/CommunityPrivilegesForm.js

+65-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import { i18next } from "@translations/invenio_communities/i18next";
1111
import { CommunitySettingsForm } from "..//components/CommunitySettingsForm";
1212
import _get from "lodash/get";
13+
import _isEmpty from "lodash/isEmpty";
1314
import { useField } from "formik";
1415
import React, { Component } from "react";
1516
import { RadioField } from "react-invenio-forms";
@@ -18,17 +19,31 @@ import PropTypes from "prop-types";
1819

1920
const VisibilityField = ({ label, formConfig, ...props }) => {
2021
const [field] = useField(props);
22+
const fieldPath = "access.visibility";
23+
24+
function createHandleChange(radioValue) {
25+
function handleChange({ event, data, formikProps }) {
26+
formikProps.form.setFieldValue(fieldPath, radioValue);
27+
// dependent fields
28+
if (radioValue === "restricted") {
29+
formikProps.form.setFieldValue("access.member_policy", "closed");
30+
}
31+
}
32+
return handleChange;
33+
}
34+
2135
return (
2236
<>
2337
{formConfig.access.visibility.map((item) => (
2438
<React.Fragment key={item.value}>
2539
<RadioField
2640
key={item.value}
27-
fieldPath="access.visibility"
41+
fieldPath={fieldPath}
2842
label={item.text}
2943
labelIcon={item.icon}
30-
checked={_get(field.value, "access.visibility") === item.value}
44+
checked={_get(field.value, fieldPath) === item.value}
3145
value={item.value}
46+
onChange={createHandleChange(item.value)}
3247
/>
3348
<label className="helptext">{item.helpText}</label>
3449
</React.Fragment>
@@ -76,14 +91,47 @@ MembersVisibilityField.defaultProps = {
7691
label: "",
7792
};
7893

94+
const MemberPolicyField = ({ label, formConfig, ...props }) => {
95+
const [field] = useField(props);
96+
const isDisabled = _get(field.value, "access.visibility") === "restricted";
97+
98+
return (
99+
<>
100+
{formConfig.access.member_policy.map((item) => (
101+
<React.Fragment key={item.value}>
102+
<RadioField
103+
key={item.value}
104+
fieldPath="access.member_policy"
105+
label={item.text}
106+
labelIcon={item.icon}
107+
checked={item.value === _get(field.value, "access.member_policy")}
108+
value={item.value}
109+
disabled={isDisabled}
110+
/>
111+
<label className="helptext">{item.helpText}</label>
112+
</React.Fragment>
113+
))}
114+
</>
115+
);
116+
};
117+
118+
MemberPolicyField.propTypes = {
119+
label: PropTypes.string,
120+
formConfig: PropTypes.object.isRequired,
121+
};
122+
123+
MemberPolicyField.defaultProps = {
124+
label: "",
125+
};
126+
79127
class CommunityPrivilegesForm extends Component {
80128
getInitialValues = () => {
81129
return {
82130
access: {
83131
visibility: "public",
84132
members_visibility: "public",
133+
member_policy: "closed",
85134
// TODO: Re-enable once properly integrated to be displayed
86-
// member_policy: "open",
87135
// record_policy: "open",
88136
},
89137
};
@@ -105,6 +153,7 @@ class CommunityPrivilegesForm extends Component {
105153
</Header.Subheader>
106154
</Header>
107155
<VisibilityField formConfig={formConfig} />
156+
108157
<Header as="h2" size="small">
109158
{i18next.t("Members visibility")}
110159
<Header.Subheader className="mt-5">
@@ -114,6 +163,19 @@ class CommunityPrivilegesForm extends Component {
114163
</Header.Subheader>
115164
</Header>
116165
<MembersVisibilityField formConfig={formConfig} />
166+
167+
{!_isEmpty(formConfig.access.member_policy) && (
168+
<>
169+
<Header as="h2" size="small">
170+
{i18next.t("Membership Policy")}
171+
<Header.Subheader className="mt-5">
172+
{i18next.t("Controls if anyone can request to join your community.")}
173+
</Header.Subheader>
174+
</Header>
175+
<MemberPolicyField formConfig={formConfig} />
176+
</>
177+
)}
178+
117179
{/* TODO: Re-enable once properly integrated to be displayed */}
118180
{/*
119181
<Grid.Column width={6}>

Diff for: invenio_communities/assets/semantic-ui/js/invenio_communities/settings/privileges/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import CommunityPrivilegesForm from "./CommunityPriviledgesForm";
1+
import CommunityPrivilegesForm from "./CommunityPrivilegesForm";
22
import ReactDOM from "react-dom";
33
import React from "react";
44

Diff for: invenio_communities/config.py

+3
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,6 @@
314314

315315
COMMUNITIES_ALWAYS_SHOW_CREATE_LINK = False
316316
"""Controls visibility of 'New Community' btn based on user's permission when set to True."""
317+
318+
COMMUNITIES_ALLOW_MEMBERSHIP_REQUESTS = False
319+
"""Feature flag for membership request."""

Diff for: invenio_communities/views/communities.py

+25
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@
8282
]
8383

8484

85+
MEMBER_POLICY_FIELDS = [
86+
{
87+
"text": "Open",
88+
"value": "open",
89+
"icon": "user plus",
90+
"helpText": _("Users can request to join your community."),
91+
},
92+
{
93+
"text": "Closed",
94+
"value": "closed",
95+
"icon": "user times",
96+
"helpText": _(
97+
"Users cannot request to join your community. Only invited users can become members of your community."
98+
),
99+
},
100+
]
101+
102+
85103
HEADER_PERMISSIONS = {
86104
"read",
87105
"update",
@@ -341,6 +359,12 @@ def communities_settings_privileges(pid_value, community, community_ui):
341359
if not permissions["can_manage_access"]:
342360
raise PermissionDeniedError()
343361

362+
member_policy = (
363+
MEMBER_POLICY_FIELDS
364+
if current_app.config["COMMUNITIES_ALLOW_MEMBERSHIP_REQUESTS"]
365+
else {}
366+
)
367+
344368
return render_community_theme_template(
345369
"invenio_communities/details/settings/privileges.html",
346370
theme=community_ui.get("theme", {}),
@@ -349,6 +373,7 @@ def communities_settings_privileges(pid_value, community, community_ui):
349373
access=dict(
350374
visibility=VISIBILITY_FIELDS,
351375
members_visibility=MEMBERS_VISIBILITY_FIELDS,
376+
member_policy=member_policy,
352377
),
353378
),
354379
permissions=permissions,

0 commit comments

Comments
 (0)