Skip to content

Commit 77288f4

Browse files
authored
Merge pull request #2558 from tekdi/feat-lead-mapping-new
Feat lead mapping new to 13 qa feat
2 parents 03749b0 + ca78c5c commit 77288f4

File tree

1 file changed

+103
-2
lines changed

1 file changed

+103
-2
lines changed

apps/admin-app-repo/src/pages/user-mobilizer.tsx

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ const Mobilizer = () => {
8686
const [open, setOpen] = useState(false);
8787
const [checked, setChecked] = useState(false);
8888
const [userID, setUserId] = useState('');
89+
const [workingVillageId, setWorkingVillageId] = useState('');
90+
const [workingLocationId, setWorkingLocationId] = useState('');
91+
// const [workingVillageValues, setWorkingVillageValues] = useState([]);
8992

9093
const searchStoreKey = 'mobilizer';
9194
const initialFormDataSearch =
@@ -137,6 +140,20 @@ const Mobilizer = () => {
137140
// console.log('responseForm', responseForm);
138141
let alterSchema = responseForm?.schema;
139142
let alterUISchema = responseForm?.uiSchema;
143+
console.log('alterSchema@@@', alterSchema);
144+
if (alterSchema) {
145+
setWorkingVillageId(alterSchema?.properties?.working_village?.fieldId);
146+
setWorkingLocationId(
147+
alterSchema?.properties?.working_location?.fieldId
148+
);
149+
}
150+
const keysToRemove = ['working_village'];
151+
keysToRemove.forEach((key) => delete alterSchema.properties[key]);
152+
keysToRemove.forEach((key) => delete alterUISchema[key]);
153+
//also remove from required if present
154+
alterSchema.required =
155+
alterSchema.required?.filter((key) => !keysToRemove.includes(key)) ||
156+
[];
140157
if (alterUISchema?.firstName) {
141158
alterUISchema.firstName['ui:disabled'] = true;
142159
}
@@ -179,6 +196,38 @@ const Mobilizer = () => {
179196
setTenantId(localStorage.getItem('tenantId'));
180197
}, []);
181198

199+
const extractVillageIdsFromWorkingLocation = (
200+
workingLocation: any
201+
): string[] | null => {
202+
if (!workingLocation || !Array.isArray(workingLocation)) {
203+
return null;
204+
}
205+
206+
const villageIds: string[] = [];
207+
208+
// Iterate through all states -> districts -> blocks -> villages
209+
for (const state of workingLocation) {
210+
if (state.districts && Array.isArray(state.districts)) {
211+
for (const district of state.districts) {
212+
if (district.blocks && Array.isArray(district.blocks)) {
213+
for (const block of district.blocks) {
214+
if (block.villages && Array.isArray(block.villages)) {
215+
for (const village of block.villages) {
216+
if (village.id) {
217+
villageIds.push(String(village.id));
218+
}
219+
}
220+
}
221+
}
222+
}
223+
}
224+
}
225+
}
226+
227+
// Return null if no villages found, otherwise return array of IDs
228+
return villageIds.length > 0 ? villageIds : null;
229+
};
230+
182231
const updatedUiSchema = {
183232
...uiSchema,
184233
'ui:submitButtonOptions': {
@@ -1024,6 +1073,24 @@ const Mobilizer = () => {
10241073
// window.alert('userDetails' + userDetails);
10251074
// window.alert('selectedUserId' + selectedUserId);
10261075
if (selectedUserId && userDetails) {
1076+
const workingLocation = userDetails?.customFields?.find(
1077+
(field) => field.fieldId === workingLocationId
1078+
);
1079+
const workingLocationValue = workingLocation?.value;
1080+
// console.log(
1081+
// 'workingLocationValue@@@',
1082+
// workingLocationValue
1083+
// );
1084+
// console.log('workingLocationValue@@@Id', workingLocationId);
1085+
const villageIds =
1086+
extractVillageIdsFromWorkingLocation(
1087+
workingLocationValue
1088+
);
1089+
// setWorkingVillageValues(villageIds);
1090+
// console.log(
1091+
// 'workingLocationValue@@@villageIds@@@',
1092+
// villageIds
1093+
// );
10271094
// Validate that villages are selected for EVERY block in working_location
10281095
const validationResult =
10291096
validateVillagesSelected(userDetails);
@@ -1083,17 +1150,51 @@ const Mobilizer = () => {
10831150

10841151
delete userData.email;
10851152

1153+
// Extract village IDs from working location right before using them
1154+
const workingLocation = userDetails?.customFields?.find(
1155+
(field: any) => field.fieldId === workingLocationId
1156+
);
1157+
const workingLocationValue = workingLocation?.value;
1158+
const villageIds =
1159+
extractVillageIdsFromWorkingLocation(
1160+
workingLocationValue
1161+
);
1162+
1163+
// Modify customFields to add/update workingVillageId with villageIds
1164+
const updatedCustomFields = [...customFields];
1165+
const workingVillageIndex = updatedCustomFields.findIndex(
1166+
(field: any) => field.fieldId === workingVillageId
1167+
);
1168+
1169+
if (
1170+
workingVillageId &&
1171+
villageIds &&
1172+
villageIds.length > 0
1173+
) {
1174+
if (workingVillageIndex !== -1) {
1175+
// Update existing field
1176+
updatedCustomFields[workingVillageIndex].value =
1177+
villageIds;
1178+
} else {
1179+
// Add new field
1180+
updatedCustomFields.push({
1181+
fieldId: workingVillageId,
1182+
value: villageIds,
1183+
});
1184+
}
1185+
}
1186+
10861187
const object = {
10871188
userData: userData,
1088-
customField: customFields,
1189+
customField: updatedCustomFields,
10891190
};
10901191

10911192
//update user details
10921193
const updateUserResponse = await enrollUserTenant({
10931194
userId: selectedUserId,
10941195
tenantId: tenantId,
10951196
roleId: roleId,
1096-
customField: customFields,
1197+
customField: updatedCustomFields,
10971198
userData: userData,
10981199
});
10991200
console.log(

0 commit comments

Comments
 (0)