@@ -78,6 +78,11 @@ const AssignBatchModal: React.FC<AssignBatchModalProps> = ({
7878 typeof window !== 'undefined' &&
7979 localStorage . getItem ( 'role' ) === Role . TEACHER ;
8080
81+ // Check if user is a Lead
82+ const isLead =
83+ typeof window !== 'undefined' &&
84+ localStorage . getItem ( 'role' ) === Role . TEAM_LEADER ;
85+
8186 const handleAssign = async ( ) => {
8287 if ( ! center || ! batch || selectedLearnerIds . length === 0 ) {
8388 return ;
@@ -145,10 +150,10 @@ const AssignBatchModal: React.FC<AssignBatchModalProps> = ({
145150
146151 const hasLocationSelection = true ;
147152 //bug fix for location atatch to center dropdown
148- // Boolean(locationFilters.states?.length) &&
149- // Boolean(locationFilters.districts?.length) &&
150- // Boolean(locationFilters.blocks?.length) &&
151- // Boolean(locationFilters.villages?.length);
153+ // Boolean(locationFilters.states?.length) &&
154+ // Boolean(locationFilters.districts?.length) &&
155+ // Boolean(locationFilters.blocks?.length) &&
156+ // Boolean(locationFilters.villages?.length);
152157
153158 // const buildCenterFilters = async() => {
154159 // const filters: Record<string, string[] | string> = {
@@ -228,110 +233,184 @@ const AssignBatchModal: React.FC<AssignBatchModalProps> = ({
228233 const fetchCenters = async ( ) => {
229234 setLoadingCenters ( true ) ;
230235 try {
231- // First, fetch all batches to get parentIds using getCohortData
232- const batchResponseData = await getCohortData (
233- localStorage . getItem ( 'userId' ) || ''
234- ) ;
235- const cohortData = batchResponseData ?. result ?? [ ] ;
236-
237- // Helper function to recursively extract all batches
238- const extractBatches = ( cohorts : any [ ] ) : any [ ] => {
239- let batches : any [ ] = [ ] ;
240- cohorts . forEach ( ( cohort : any ) => {
241- // Batches are items in childData that have a parentId, or items with type BATCH
242- if (
243- cohort ?. parentId ||
244- cohort ?. type === 'BATCH' ||
245- cohort ?. cohortType === 'BATCH'
246- ) {
247- // Only include active batches
236+ if ( isLead ) {
237+ // For Lead role: Use getCohortData with children=true and customField=true
238+ const cohortResponseData = await getCohortData (
239+ localStorage . getItem ( 'userId' ) || '' ,
240+ { customField : true , children : true }
241+ ) ;
242+ const cohortData = cohortResponseData ?. result ?? [ ] ;
243+
244+ // Extract centers (cohorts with type COHORT or no parentId)
245+ const centerDetails = cohortData . filter (
246+ ( cohort : any ) =>
247+ cohort ?. type === 'COHORT' &&
248+ ( cohort ?. cohortStatus === 'active' ||
249+ cohort ?. status === 'active' ) &&
250+ ! cohort ?. parentId
251+ ) ;
252+
253+ // Map centers
254+ const centerOptions = centerDetails . map ( ( cohort : any ) => {
255+ const cf = cohort . customField || cohort . customFields ;
256+ return {
257+ label : `${ capitalizeFirstChar (
258+ cohort . cohortName || cohort . name
259+ ) } (${ capitalizeFirstChar ( getField ( cf , 'TYPE_OF_CENTER' ) ) } )`,
260+ value : cohort . cohortId ,
261+ } ;
262+ } ) ;
263+
264+ setCenters ( centerOptions ) ;
265+
266+ // Extract all batches from childData of all centers
267+ const allBatchDetails : any [ ] = [ ] ;
268+ centerDetails . forEach ( ( center : any ) => {
269+ if ( center ?. childData && Array . isArray ( center . childData ) ) {
270+ center . childData . forEach ( ( batch : any ) => {
271+ // Only include active batches
272+ if (
273+ batch ?. type === 'BATCH' &&
274+ ( batch ?. status === 'active' ||
275+ batch ?. cohortStatus === 'active' )
276+ ) {
277+ allBatchDetails . push ( {
278+ ...batch ,
279+ parentId : center . cohortId ,
280+ } ) ;
281+ }
282+ } ) ;
283+ }
284+ } ) ;
285+
286+ // Store all batches for later filtering by center
287+ const allBatchOptions = allBatchDetails . map ( ( batch : any ) => ( {
288+ label :
289+ capitalizeFirstChar ( batch . name || batch . cohortName ) ||
290+ t ( 'USER_REGISTRATION.UNNAMED_BATCH' ) ,
291+ value : batch . cohortId ,
292+ parentId : batch . parentId ,
293+ } ) ) ;
294+
295+ setAllBatches ( allBatchOptions ) ;
296+
297+ // Set the first center as selected if available
298+ const centerStillValid = centerOptions . some (
299+ ( option : any ) => option . value === center
300+ ) ;
301+ if ( ! centerStillValid ) {
302+ const nextCenter = centerOptions [ 0 ] ?. value ?? '' ;
303+ setCenter ( nextCenter ) ;
304+ setBatch ( '' ) ;
305+ setBatches ( [ ] ) ;
306+ }
307+ } else {
308+ // For Instructor role: Keep existing logic
309+ // First, fetch all batches to get parentIds using getCohortData
310+ const batchResponseData = await getCohortData (
311+ localStorage . getItem ( 'userId' ) || ''
312+ ) ;
313+ const cohortData = batchResponseData ?. result ?? [ ] ;
314+
315+ // Helper function to recursively extract all batches
316+ const extractBatches = ( cohorts : any [ ] ) : any [ ] => {
317+ let batches : any [ ] = [ ] ;
318+ cohorts . forEach ( ( cohort : any ) => {
319+ // Batches are items in childData that have a parentId, or items with type BATCH
248320 if (
249- cohort ?. status === 'active' ||
250- cohort ?. cohortStatus === 'active'
321+ cohort ?. parentId ||
322+ cohort ?. type === 'BATCH' ||
323+ cohort ?. cohortType === 'BATCH'
251324 ) {
252- batches . push ( cohort ) ;
325+ // Only include active batches
326+ if (
327+ cohort ?. status === 'active' ||
328+ cohort ?. cohortStatus === 'active'
329+ ) {
330+ batches . push ( cohort ) ;
331+ }
253332 }
254- }
255- // Recursively search in childData
256- if ( cohort ?. childData && Array . isArray ( cohort . childData ) ) {
257- batches = batches . concat ( extractBatches ( cohort . childData ) ) ;
258- }
333+ // Recursively search in childData
334+ if ( cohort ?. childData && Array . isArray ( cohort . childData ) ) {
335+ batches = batches . concat ( extractBatches ( cohort . childData ) ) ;
336+ }
337+ } ) ;
338+ return batches ;
339+ } ;
340+
341+ const batchDetails = extractBatches ( cohortData ) ;
342+ // Store all batches for later filtering by center
343+ const allBatchOptions = batchDetails . map ( ( batch : any ) => ( {
344+ label :
345+ capitalizeFirstChar ( batch . name || batch . cohortName ) ||
346+ t ( 'USER_REGISTRATION.UNNAMED_BATCH' ) ,
347+ value : batch . cohortId ,
348+ parentId : batch . parentId ,
349+ } ) ) ;
350+ setAllBatches ( allBatchOptions ) ;
351+
352+ // Extract all unique parentIds from batches
353+ const parentIds = Array . from (
354+ new Set (
355+ batchDetails
356+ . map ( ( batch : any ) => batch . parentId )
357+ . filter (
358+ ( id : any ) =>
359+ id &&
360+ id !== 'Select' &&
361+ typeof id === 'string' &&
362+ id . trim ( ) !== ''
363+ )
364+ )
365+ ) ;
366+
367+ if ( parentIds . length === 0 ) {
368+ setCenters ( [ ] ) ;
369+ setCenter ( '' ) ;
370+ setBatches ( [ ] ) ;
371+ setAllBatches ( [ ] ) ;
372+ setBatch ( '' ) ;
373+ return ;
374+ }
375+
376+ // Fetch centers using parentIds as cohortId filter
377+ const centerResponse = await getCohortListService ( {
378+ limit : 200 ,
379+ offset : 0 ,
380+ sort : [ 'name' , 'asc' ] ,
381+ filters : {
382+ cohortId : parentIds ,
383+ type : 'COHORT' ,
384+ status : [ 'active' ] ,
385+ } ,
259386 } ) ;
260- return batches ;
261- } ;
262-
263- const batchDetails = extractBatches ( cohortData ) ;
264- // Store all batches for later filtering by center
265- const allBatchOptions = batchDetails . map ( ( batch : any ) => ( {
266- label :
267- capitalizeFirstChar ( batch . name || batch . cohortName ) ||
268- t ( 'USER_REGISTRATION.UNNAMED_BATCH' ) ,
269- value : batch . cohortId ,
270- parentId : batch . parentId ,
271- } ) ) ;
272- setAllBatches ( allBatchOptions ) ;
273-
274- // Extract all unique parentIds from batches
275- const parentIds = Array . from (
276- new Set (
277- batchDetails
278- . map ( ( batch : any ) => batch . parentId )
279- . filter (
280- ( id : any ) =>
281- id &&
282- id !== 'Select' &&
283- typeof id === 'string' &&
284- id . trim ( ) !== ''
285- )
286- )
287- ) ;
288-
289- if ( parentIds . length === 0 ) {
290- setCenters ( [ ] ) ;
291- setCenter ( '' ) ;
292- setBatches ( [ ] ) ;
293- setAllBatches ( [ ] ) ;
294- setBatch ( '' ) ;
295- return ;
296- }
297387
298- // Fetch centers using parentIds as cohortId filter
299- const centerResponse = await getCohortListService ( {
300- limit : 200 ,
301- offset : 0 ,
302- sort : [ 'name' , 'asc' ] ,
303- filters : {
304- cohortId : parentIds ,
305- type : 'COHORT' ,
306- status : [ 'active' ] ,
307- } ,
308- } ) ;
309-
310- const centerDetails = centerResponse ?. results ?. cohortDetails ?? [ ] ;
311- const centerOptions = centerDetails . map ( ( cohort : any ) => {
312- const cf = cohort . customField ;
313-
314- return {
315- label : isTeacher
316- ? `${ capitalizeFirstChar ( cohort . name ) } `
317- : `${ capitalizeFirstChar ( cohort . name ) } (${ capitalizeFirstChar (
318- getField ( cf , 'TYPE_OF_CENTER' )
319- ) } )`,
320- value : cohort . cohortId ,
321- } ;
322- } ) ;
323-
324- setCenters ( centerOptions ) ;
325-
326- // Set the first center as selected if available
327- const centerStillValid = centerOptions . some (
328- ( option : any ) => option . value === center
329- ) ;
330- if ( ! centerStillValid ) {
331- const nextCenter = centerOptions [ 0 ] ?. value ?? '' ;
332- setCenter ( nextCenter ) ;
333- setBatch ( '' ) ;
334- setBatches ( [ ] ) ;
388+ const centerDetails = centerResponse ?. results ?. cohortDetails ?? [ ] ;
389+ const centerOptions = centerDetails . map ( ( cohort : any ) => {
390+ const cf = cohort . customField ;
391+
392+ return {
393+ label : isTeacher
394+ ? `${ capitalizeFirstChar ( cohort . name ) } `
395+ : `${ capitalizeFirstChar ( cohort . name ) } (${ capitalizeFirstChar (
396+ getField ( cf , 'TYPE_OF_CENTER' )
397+ ) } )`,
398+ value : cohort . cohortId ,
399+ } ;
400+ } ) ;
401+
402+ setCenters ( centerOptions ) ;
403+
404+ // Set the first center as selected if available
405+ const centerStillValid = centerOptions . some (
406+ ( option : any ) => option . value === center
407+ ) ;
408+ if ( ! centerStillValid ) {
409+ const nextCenter = centerOptions [ 0 ] ?. value ?? '' ;
410+ setCenter ( nextCenter ) ;
411+ setBatch ( '' ) ;
412+ setBatches ( [ ] ) ;
413+ }
335414 }
336415 } catch ( error ) {
337416 console . error ( 'Error fetching centers:' , error ) ;
@@ -347,7 +426,7 @@ const AssignBatchModal: React.FC<AssignBatchModalProps> = ({
347426
348427 fetchCenters ( ) ;
349428 // eslint-disable-next-line react-hooks/exhaustive-deps
350- } , [ open , locationKey , isTeacher ] ) ;
429+ } , [ open , locationKey , isTeacher , isLead ] ) ;
351430
352431 useEffect ( ( ) => {
353432 if ( ! center ) {
@@ -417,7 +496,9 @@ const AssignBatchModal: React.FC<AssignBatchModalProps> = ({
417496 mb : 0.5 ,
418497 } }
419498 >
420- { t ( 'USER_REGISTRATION.LEARNERS_SELECTED' , { count : selectedLearners . length } ) }
499+ { t ( 'USER_REGISTRATION.LEARNERS_SELECTED' , {
500+ count : selectedLearners . length ,
501+ } ) }
421502 </ Typography >
422503 < Typography
423504 variant = "body2"
@@ -637,7 +718,9 @@ const AssignBatchModal: React.FC<AssignBatchModalProps> = ({
637718 } ,
638719 } }
639720 >
640- { t ( 'USER_REGISTRATION.ASSIGN_BATCH_LEARNERS' , { count : selectedLearners . length } ) }
721+ { t ( 'USER_REGISTRATION.ASSIGN_BATCH_LEARNERS' , {
722+ count : selectedLearners . length ,
723+ } ) }
641724 </ Button >
642725 </ Box >
643726 </ Box >
0 commit comments