Skip to content

Commit 940f99f

Browse files
committed
Pre-selecting parent groups for planting based on their for-term attributes.
1 parent b9af4b2 commit 940f99f

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/components/Groups/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const augmentGroupObject = (groups, id, locale) => {
5757
* @param {Boolean} createChildren whether to create children list in each group
5858
* @returns {Array} sorted array of augmented group objects
5959
*/
60-
const getGroups = lruMemoize((groups, locale, createChildren = false) => {
60+
export const getGroups = lruMemoize((groups, locale, createChildren = false) => {
6161
// make a copy of groups so we can augment it
6262
const result = {};
6363
Object.keys(groups).forEach(id => {

src/pages/GroupsSuperadmin/GroupsSuperadmin.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,33 @@ import { loggedInUserSelector } from '../../redux/selectors/users.js';
3737

3838
import { isSuperadminRole } from '../../components/helpers/usersRoles.js';
3939
import { getErrorMessage } from '../../locales/apiErrorMessages.js';
40+
import { getGroups as getGroupsHelper } from '../../components/Groups/helpers.js';
4041

4142
const DEFAULT_EXPIRATION = 7; // days
4243

4344
// keep only courses (term parents) and term groups
4445
const plantingGroupFilter = group => group.attributes?.course?.length > 0 || group.attributes?.term?.length > 0;
4546

46-
const plantingCheckboxSelector = lruMemoize(
47-
term => group =>
48-
group.attributes?.course?.length > 0 &&
49-
!group.children.some(g => g.attributes?.term?.includes(`${term.year}-${term.term}`))
50-
);
47+
const groupCheckboxPredicate = (group, term) =>
48+
group.attributes?.course?.length > 0 &&
49+
!group.children.some(g => g.attributes?.term?.includes(`${term.year}-${term.term}`));
50+
51+
const plantingCheckboxSelector = lruMemoize(term => group => groupCheckboxPredicate(group, term));
5152

5253
const highlightClassGenerator = lruMemoize(
5354
term => group => (group.attributes?.term?.includes(`${term.year}-${term.term}`) ? 'text-success fw-bold' : '')
5455
);
5556

57+
const getPlantingCandidates = (groups, term) => {
58+
const candidates = {};
59+
getGroupsHelper(groups, 'en', true)
60+
.filter(g => groupCheckboxPredicate(g, term) && g.attributes?.['for-term']?.includes(`${term.term}`))
61+
.forEach(g => {
62+
candidates[g.id] = true;
63+
});
64+
return candidates;
65+
};
66+
5667
class GroupsSuperadmin extends Component {
5768
state = {
5869
modalGroup: null,
@@ -78,19 +89,21 @@ class GroupsSuperadmin extends Component {
7889
modalGroupError: null,
7990
});
8091

81-
openModalPlant = () =>
92+
openModalPlant = (groups, term) => {
93+
const plantGroups = getPlantingCandidates(groups, term);
8294
this.setState({
8395
modalPlant: true,
8496
modalGroup: null,
85-
plantGroups: null,
97+
plantGroups,
8698
plantTexts: null,
87-
plantGroupsCount: 0,
99+
plantGroupsCount: Object.keys(plantGroups).length,
88100
plantGroupsPending: false,
89101
plantGroupsErrors: null,
90102
plantedGroups: 0,
91103
});
104+
};
92105

93-
closeModalPlant = () => this.setState({ modalPlant: false });
106+
closeModalPlant = () => this.setState({ modalPlant: false, plantGroups: null, plantGroupsCount: 0 });
94107

95108
cancelGroupPlanting = () => {
96109
if (!this.state.plantGroupsPending) {
@@ -124,13 +137,11 @@ class GroupsSuperadmin extends Component {
124137
plantTermGroupsFormSubmit = plantTexts => {
125138
this.setState({
126139
plantTexts,
127-
plantGroups: {},
128-
plantGroupsCount: 0,
129140
plantGroupsPending: false,
130141
plantGroupsErrors: null,
131142
plantedGroups: 0,
143+
modalPlant: false,
132144
});
133-
this.closeModalPlant();
134145
};
135146

136147
changePlantGroups = (id, newState) => {
@@ -342,7 +353,9 @@ class GroupsSuperadmin extends Component {
342353
<div className="text-center">
343354
{terms && terms.length > 0 && !this.state.plantTexts && (
344355
<Dropdown as={ButtonGroup}>
345-
<Button variant="success" onClick={this.openModalPlant}>
356+
<Button
357+
variant="success"
358+
onClick={() => this.openModalPlant(groups, this.state.plantTerm || terms[0])}>
346359
<Icon icon="leaf" gapRight />
347360
<FormattedMessage
348361
id="app.groupsSupervisor.plantTermButton"

0 commit comments

Comments
 (0)