@@ -85,6 +85,7 @@ const WorkingVillageAssignmentWidget: React.FC<WorkingVillageAssignmentWidgetPro
8585 const themeColor = '#FDBE16' ;
8686 const themeColorLight = 'rgba(253, 190, 22, 0.1)' ; // 10% opacity
8787 const themeColorLighter = 'rgba(253, 190, 22, 0.05)' ; // 5% opacity
88+ const __villagesLoadSeqRef = React . useRef ( 0 ) ;
8889
8990 // Local state management for geography filters and center selection
9091 const [ selectedState , setSelectedState ] = useState < string > ( '' ) ; // Selected state ID
@@ -151,11 +152,19 @@ const WorkingVillageAssignmentWidget: React.FC<WorkingVillageAssignmentWidgetPro
151152 stateName : string ;
152153 } > > ( [ ] ) ;
153154
154- // Store center's CATCHMENT_AREA fetched via API in reassign mode
155- const [ reassignCenterCatchmentArea , setReassignCenterCatchmentArea ] = useState < any > ( null ) ;
155+ // Store center's CATCHMENT_AREA fetched via API in reassign mode (keyed by centerId to avoid stale usage during center switches)
156+ const [ reassignCenterCatchmentArea , setReassignCenterCatchmentArea ] = useState < {
157+ centerId : string ;
158+ selectedValues : any [ ] | null ;
159+ } | null > ( null ) ;
156160
157161 // Load villages for all catchment blocks
158162 const loadVillagesForBlocks = useCallback ( async ( ) => {
163+ const __seq = ++ __villagesLoadSeqRef . current ;
164+ const __centerAtStart = selectedCenter ;
165+ const __blockIdsAtStart = Array . isArray ( catchmentBlocks )
166+ ? catchmentBlocks . map ( ( b ) => String ( b . id ) ) . join ( ',' )
167+ : '' ;
159168 if ( ! selectedCenter || catchmentBlocks . length === 0 ) {
160169 setVillagesByBlock ( { } ) ;
161170 setVillagesDisplayLimit ( { } ) ;
@@ -278,6 +287,18 @@ const WorkingVillageAssignmentWidget: React.FC<WorkingVillageAssignmentWidgetPro
278287 }
279288 }
280289
290+ // Latest-wins guard: prevent older in-flight loads from overwriting the newest center/blocks state
291+ // (this can happen during rapid center changes in reassign flow)
292+ const __isLatest = __seq === __villagesLoadSeqRef . current ;
293+ const __sameCenter = selectedCenter === __centerAtStart ;
294+ const __sameBlocks =
295+ ( Array . isArray ( catchmentBlocks )
296+ ? catchmentBlocks . map ( ( b ) => String ( b . id ) ) . join ( ',' )
297+ : '' ) === __blockIdsAtStart ;
298+ if ( ! __isLatest || ! __sameCenter || ! __sameBlocks ) {
299+ return ;
300+ }
301+
281302 setVillagesByBlock ( villagesData ) ;
282303 setVillagesDisplayLimit ( displayLimits ) ;
283304
@@ -873,7 +894,8 @@ const WorkingVillageAssignmentWidget: React.FC<WorkingVillageAssignmentWidgetPro
873894 return ;
874895 }
875896
876- setReassignCenterCatchmentArea ( null ) ; // Clear previous center's catchment while loading
897+ // Clear previous center's catchment while loading, but keep centerId so extract logic can't accidentally use stale data
898+ setReassignCenterCatchmentArea ( { centerId : selectedCenter , selectedValues : null } ) ;
877899 setSelectedVillages ( new Set ( ) ) ; // Clear village selection when center changes so new center's list is fresh
878900 let isMounted = true ;
879901 const centerIdToFetch = selectedCenter ;
@@ -916,17 +938,29 @@ const WorkingVillageAssignmentWidget: React.FC<WorkingVillageAssignmentWidgetPro
916938 ) ;
917939
918940 if ( catchmentAreaField && catchmentAreaField . selectedValues ) {
919- setReassignCenterCatchmentArea ( catchmentAreaField . selectedValues ) ;
941+ setReassignCenterCatchmentArea ( {
942+ centerId : centerIdToFetch ,
943+ selectedValues : catchmentAreaField . selectedValues ,
944+ } ) ;
920945 } else {
921- setReassignCenterCatchmentArea ( null ) ;
946+ setReassignCenterCatchmentArea ( {
947+ centerId : centerIdToFetch ,
948+ selectedValues : null ,
949+ } ) ;
922950 }
923951 } else {
924- setReassignCenterCatchmentArea ( null ) ;
952+ setReassignCenterCatchmentArea ( {
953+ centerId : centerIdToFetch ,
954+ selectedValues : null ,
955+ } ) ;
925956 }
926957 } catch ( error ) {
927958 console . error ( 'Error fetching center CATCHMENT_AREA:' , error ) ;
928959 if ( isMounted ) {
929- setReassignCenterCatchmentArea ( null ) ;
960+ setReassignCenterCatchmentArea ( {
961+ centerId : centerIdToFetch ,
962+ selectedValues : null ,
963+ } ) ;
930964 }
931965 }
932966 } ;
@@ -941,7 +975,14 @@ const WorkingVillageAssignmentWidget: React.FC<WorkingVillageAssignmentWidgetPro
941975 // Extract catchment blocks from selected center's CATCHMENT_AREA or fetched CATCHMENT_AREA (in reassign mode)
942976 useEffect ( ( ) => {
943977 // Priority 1: In reassign mode, use fetched CATCHMENT_AREA for the currently selected center (fetched when selectedCenter changes)
944- if ( isForReassign && selectedCenter && reassignCenterCatchmentArea ) {
978+ if (
979+ isForReassign &&
980+ selectedCenter &&
981+ reassignCenterCatchmentArea &&
982+ reassignCenterCatchmentArea . centerId === selectedCenter &&
983+ Array . isArray ( reassignCenterCatchmentArea . selectedValues ) &&
984+ reassignCenterCatchmentArea . selectedValues . length > 0
985+ ) {
945986 // Extract blocks from fetched center's CATCHMENT_AREA
946987 const extractedBlocks : Array < {
947988 id : string | number ;
@@ -952,8 +993,9 @@ const WorkingVillageAssignmentWidget: React.FC<WorkingVillageAssignmentWidgetPro
952993 stateName : string ;
953994 } > = [ ] ;
954995
955- if ( Array . isArray ( reassignCenterCatchmentArea ) && reassignCenterCatchmentArea . length > 0 ) {
956- reassignCenterCatchmentArea . forEach ( ( stateData : any ) => {
996+ const __selectedValues = reassignCenterCatchmentArea . selectedValues ;
997+ if ( Array . isArray ( __selectedValues ) && __selectedValues . length > 0 ) {
998+ __selectedValues . forEach ( ( stateData : any ) => {
957999 const stateId = stateData . stateId ;
9581000 const stateName = stateData . stateName || '' ;
9591001
@@ -1847,7 +1889,7 @@ const WorkingVillageAssignmentWidget: React.FC<WorkingVillageAssignmentWidgetPro
18471889 return (
18481890 < Accordion
18491891 key = { block . id }
1850- defaultExpanded
1892+ defaultExpanded = { false }
18511893 sx = { {
18521894 border : '1px solid' ,
18531895 borderColor : 'divider' ,
0 commit comments