@@ -37,22 +37,33 @@ import { loggedInUserSelector } from '../../redux/selectors/users.js';
3737
3838import { isSuperadminRole } from '../../components/helpers/usersRoles.js' ;
3939import { getErrorMessage } from '../../locales/apiErrorMessages.js' ;
40+ import { getGroups as getGroupsHelper } from '../../components/Groups/helpers.js' ;
4041
4142const DEFAULT_EXPIRATION = 7 ; // days
4243
4344// keep only courses (term parents) and term groups
4445const 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
5253const 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+
5667class 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