@@ -175,6 +175,8 @@ const Index = () => {
175175 const [ mobilizerPrefilledState , setMobilizerPrefilledState ] = useState ( { } ) ;
176176 const [ mobilizerAddSchema , setMobilizerAddSchema ] = useState ( null ) ;
177177 const [ mobilizerAddUiSchema , setMobilizerAddUiSchema ] = useState ( null ) ;
178+ const [ workingVillageId , setWorkingVillageId ] = useState ( '' ) ;
179+ const [ workingLocationId , setWorkingLocationId ] = useState ( '' ) ;
178180
179181 const [ selectedValue , setSelectedValue ] = useState < any > ( ) ;
180182 const [ selectedBlockValue , setSelectedBlockValue ] = useState < any > (
@@ -697,6 +699,19 @@ const Index = () => {
697699 ] ) ;
698700 let alterSchema = responseForm ?. schema ;
699701 let alterUISchema = responseForm ?. uiSchema ;
702+ if ( alterSchema ) {
703+ setWorkingVillageId ( alterSchema ?. properties ?. working_village ?. fieldId ) ;
704+ setWorkingLocationId (
705+ alterSchema ?. properties ?. working_location ?. fieldId
706+ ) ;
707+ const keysToRemove = [ 'working_village' ] ;
708+ keysToRemove . forEach ( ( key ) => delete alterSchema . properties [ key ] ) ;
709+ keysToRemove . forEach ( ( key ) => delete alterUISchema [ key ] ) ;
710+ //also remove from required if present
711+ alterSchema . required =
712+ alterSchema . required ?. filter ( ( key ) => ! keysToRemove . includes ( key ) ) ||
713+ [ ] ;
714+ }
700715 if ( alterUISchema ?. firstName ) {
701716 alterUISchema . firstName [ 'ui:disabled' ] = true ;
702717 }
@@ -1320,6 +1335,36 @@ const Index = () => {
13201335 } ;
13211336 } ;
13221337
1338+ const extractVillageIdsFromWorkingLocation = (
1339+ workingLocation : any
1340+ ) : string [ ] | null => {
1341+ if ( ! workingLocation || ! Array . isArray ( workingLocation ) ) {
1342+ return null ;
1343+ }
1344+
1345+ const villageIds : string [ ] = [ ] ;
1346+
1347+ for ( const state of workingLocation ) {
1348+ if ( state . districts && Array . isArray ( state . districts ) ) {
1349+ for ( const district of state . districts ) {
1350+ if ( district . blocks && Array . isArray ( district . blocks ) ) {
1351+ for ( const block of district . blocks ) {
1352+ if ( block . villages && Array . isArray ( block . villages ) ) {
1353+ for ( const village of block . villages ) {
1354+ if ( village . id ) {
1355+ villageIds . push ( String ( village . id ) ) ;
1356+ }
1357+ }
1358+ }
1359+ }
1360+ }
1361+ }
1362+ }
1363+ }
1364+
1365+ return villageIds . length > 0 ? villageIds : null ;
1366+ } ;
1367+
13231368 const handleOpenMobilizerModal = ( ) => {
13241369 setMobilizerPrefilledState ( { } ) ;
13251370 setMobilizerFormStep ( 0 ) ;
@@ -1757,14 +1802,79 @@ const Index = () => {
17571802
17581803 delete userData . email ;
17591804
1805+ let workingLocationValue =
1806+ userDetails ?. working_location ;
1807+ if (
1808+ ! workingLocationValue &&
1809+ userDetails ?. customFields &&
1810+ Array . isArray ( userDetails . customFields )
1811+ ) {
1812+ const workingLocationById = workingLocationId
1813+ ? userDetails . customFields . find (
1814+ ( field : any ) =>
1815+ field . fieldId === workingLocationId
1816+ )
1817+ : null ;
1818+ const workingLocationField =
1819+ workingLocationById ||
1820+ userDetails . customFields . find ( ( field : any ) => {
1821+ if (
1822+ field . value &&
1823+ Array . isArray ( field . value ) &&
1824+ field . value . length > 0
1825+ ) {
1826+ const firstItem = field . value [ 0 ] ;
1827+ return (
1828+ firstItem &&
1829+ typeof firstItem === 'object' &&
1830+ 'stateId' in firstItem &&
1831+ 'stateName' in firstItem &&
1832+ 'districts' in firstItem
1833+ ) ;
1834+ }
1835+ return false ;
1836+ } ) ;
1837+ if ( workingLocationField ?. value ) {
1838+ workingLocationValue =
1839+ workingLocationField . value ;
1840+ }
1841+ }
1842+
1843+ const villageIds =
1844+ extractVillageIdsFromWorkingLocation (
1845+ workingLocationValue
1846+ ) ;
1847+
1848+ const updatedCustomFields = [ ...customFields ] ;
1849+ const workingVillageIndex =
1850+ updatedCustomFields . findIndex (
1851+ ( field : any ) =>
1852+ field . fieldId === workingVillageId
1853+ ) ;
1854+ if (
1855+ workingVillageId &&
1856+ villageIds &&
1857+ villageIds . length > 0
1858+ ) {
1859+ if ( workingVillageIndex !== - 1 ) {
1860+ updatedCustomFields [ workingVillageIndex ] . value =
1861+ villageIds ;
1862+ } else {
1863+ updatedCustomFields . push ( {
1864+ fieldId : workingVillageId ,
1865+ value : villageIds ,
1866+ } ) ;
1867+ }
1868+ }
1869+
17601870 const mobilizerRoleId =
17611871 'a4694781-65c1-4b92-8ff1-ad490ab6d140' ; // RoleId.MOBILIZER
17621872
17631873 const updateUserResponse = await enrollUserTenant ( {
17641874 userId : selectedMobilizerUserId ,
17651875 tenantId : tenantId ,
17661876 roleId : mobilizerRoleId ,
1767- customField : customFields ,
1877+ customField : updatedCustomFields ,
17681878 userData : userData ,
17691879 } ) ;
17701880 console . log (
0 commit comments