Skip to content

Commit 1d59c90

Browse files
APPLEAPPLE
authored andcommitted
helper change
1 parent ada2406 commit 1d59c90

File tree

1 file changed

+33
-330
lines changed

1 file changed

+33
-330
lines changed
Lines changed: 33 additions & 330 deletions
Original file line numberDiff line numberDiff line change
@@ -1,344 +1,47 @@
1-
export enum Storage {
2-
USER_DATA = 'userData',
3-
NAME = 'name',
4-
USER_ID = 'userId',
5-
}
6-
7-
export const firstLetterInUpperCase = (label: string): string => {
8-
if (!label) {
9-
return '';
10-
}
11-
12-
return label
13-
?.split(' ')
14-
?.map((word) => word?.charAt(0).toUpperCase() + word?.slice(1))
15-
?.join(' ');
16-
};
17-
const getSelectedValueName = (fields: any, label: any) => {
18-
const field = fields.find((f: any) => f.label === label);
19-
if (field && field.selectedValues && field.selectedValues.length > 0) {
20-
return field.selectedValues[0]; // Return the first selected value
1+
//Function to convert names in capitalize case
2+
export const toPascalCase = (name: string | any) => {
3+
if (typeof name !== 'string') {
4+
return name;
215
}
226

23-
return null;
24-
};
25-
export const mapUserData = (userData: any) => {
26-
console.log(userData, 'userData===>');
27-
try {
28-
const getSelectedValue = (label: any) =>
29-
userData.customFields
30-
.find((f: any) => f.label === label)
31-
?.selectedValues.map((v: any) => v?.id?.toString()) || '';
32-
33-
const getSingleSelectedValue = (label: any) =>
34-
userData.customFields
35-
.find((f: any) => f.label === label)
36-
?.selectedValues[0]?.id?.toString() || userData.customFields.find((f: any) => f.label === label)?.selectedValues[0]?.value?.toString() || '';
37-
38-
const getSingleTextValue = (label: any) =>
39-
userData.customFields.find((f: any) => f.label === label)
40-
?.selectedValues[0] || '';
41-
42-
const result: any = {
43-
firstName: userData.firstName || '',
44-
// middleName: userData.middleName || '',
45-
lastName: userData.lastName || '',
46-
email: userData.email || '',
47-
mobile: userData.mobile ? userData.mobile?.toString() : '',
48-
dob: userData.dob || '',
49-
gender: userData.gender || '',
50-
mother_name: getSingleTextValue('MOTHER_NAME'),
51-
number_of_children:getSingleTextValue('NUMBER_OF_CHILDREN'),
52-
number_of_children_in_your_group: getSingleTextValue('NUMBER_OF_CHILDREN_IN_YOUR_GROUP'),
53-
father_name: getSingleTextValue('FATHER_NAME'),
54-
spouse_name: getSingleTextValue('SPOUSE_NAME'),
55-
56-
57-
58-
marital_status: getSelectedValue('MARITAL_STATUS'),
59-
phone_type_accessible
60-
: getSingleSelectedValue('TYPE_OF_PHONE_ACCESSIBLE'),
61-
family_member_details
62-
: getSingleSelectedValue('FAMILY_MEMBER_DETAILS'),
63-
training_check: getSingleSelectedValue('HAVE_YOU_RECEIVE_ANY_PRIOR_TRAINING'),
64-
own_phone_check: getSingleSelectedValue('DOES_THIS_PHONE_BELONG_TO_YOU'),
65-
state: getSelectedValue('STATE'),
66-
district: getSelectedValue('DISTRICT'),
67-
block: getSelectedValue('BLOCK'),
68-
village: getSelectedValue('VILLAGE'),
69-
// is_volunteer: getSingleTextValue('IS_VOLUNTEER'),
70-
drop_out_reason:
71-
getSelectedValue('REASON_FOR_DROP_OUT_FROM_SCHOOL') || [], // array
72-
// Keep program dropdown empty on edit; user should select explicitly
73-
what_program_are_you_part_of: getSelectedValue('WHAT PROGRAM ARE YOU PART OF') || [],
74-
// what_program_are_you_part_of: [],
75-
work_domain:
76-
getSelectedValue(
77-
'WHAT_IS_YOUR_PRIMARY_WORK'
78-
) || [],
79-
80-
what_do_you_want_to_become: getSingleTextValue(
81-
'WHAT_DO_YOU_WANT_TO_BECOME'
82-
),
83-
class: getSelectedValue(
84-
'HIGHEST_EDCATIONAL_QUALIFICATION_OR_LAST_PASSED_GRADE'
85-
), // string
86-
87-
preferred_mode_of_learning:
88-
getSelectedValue('WHAT_IS_YOUR_PREFERRED_MODE_OF_LEARNING') || [],
89-
};
90-
if (userData.middleName) {
91-
result.middleName = userData.middleName;
92-
}
93-
if (getSingleTextValue('MOTHER_NAME')) {
94-
result.mother_name = getSingleTextValue('MOTHER_NAME');
95-
}
96-
if (getSingleTextValue('IS_VOLUNTEER')) {
97-
result.is_volunteer = getSingleTextValue('IS_VOLUNTEER');
98-
}
99-
if (getSingleTextValue('NUMBER_OF_CHILDREN')) {
100-
result.number_of_children = getSingleTextValue('NUMBER_OF_CHILDREN');
101-
result.number_of_children_in_your_group = getSingleTextValue('NUMBER_OF_CHILDREN_IN_YOUR_GROUP');
102-
}
103-
if (getSelectedValueName(userData.customFields, 'NAME_OF_GUARDIAN')) {
104-
result.guardian_name =
105-
getSelectedValueName(userData.customFields, 'NAME_OF_GUARDIAN') || '';
106-
}
107-
if (getSelectedValueName(userData.customFields, 'RELATION_WITH_GUARDIAN')) {
108-
result.guardian_relation =
109-
getSelectedValueName(userData.customFields, 'RELATION_WITH_GUARDIAN') ||
110-
'';
111-
}
112-
if (
113-
getSelectedValueName(userData.customFields, 'PARENT_GUARDIAN_PHONE_NO')
114-
) {
115-
result.parent_phone =
116-
getSelectedValueName(
117-
userData.customFields,
118-
'PARENT_GUARDIAN_PHONE_NO'
119-
) || '';
120-
}
121-
122-
return result;
123-
} catch (error) {
124-
console.log(error);
125-
}
7+
return name
8+
?.toLowerCase()
9+
.split(' ')
10+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
11+
.join(' ');
12612
};
12713

128-
// Usage
129-
export const getMissingFields = (schema: any, userData: any) => {
130-
try {
131-
// use mapped data instead of raw userData
132-
const mappedUserData = mapUserData(userData);
133-
console.log(mappedUserData, 'mappedUserData');
134-
console.log(schema, 'schema');
135-
136-
const isEmpty = (value: any) => {
137-
return (
138-
value === undefined ||
139-
value === null ||
140-
(typeof value === 'string' && value.trim() === '') ||
141-
(Array.isArray(value) && value.length === 0) ||
142-
(typeof value === 'object' &&
143-
!Array.isArray(value) &&
144-
Object.keys(value).length === 0)
145-
);
146-
};
147-
148-
const clonedSchema = JSON.parse(JSON.stringify(schema));
149-
150-
const fieldsToRemove = [
151-
'password',
152-
'confirm_password',
153-
'username',
154-
'program',
155-
'batch',
156-
'center',
157-
'state',
158-
'district',
159-
'block',
160-
'village',
14+
export const formatDate = (dateString: string) => {
15+
if (dateString) {
16+
const dateOnly = dateString?.split('T')[0];
17+
const [year, monthIndex, day] = dateOnly.split('-');
18+
const MONTHS = [
19+
'January', 'February', 'March', 'April', 'May', 'June',
20+
'July', 'August', 'September', 'October', 'November', 'December',
16121
];
162-
163-
fieldsToRemove.forEach((field) => {
164-
delete clonedSchema.properties[field];
165-
});
166-
167-
if (Array.isArray(clonedSchema.required)) {
168-
clonedSchema.required = clonedSchema.required.filter(
169-
(field: any) => !fieldsToRemove.includes(field)
170-
);
171-
}
172-
173-
const guardianFields = [
174-
'guardian_name',
175-
'guardian_relation',
176-
'parent_phone',
177-
];
178-
179-
const result: any = {
180-
type: clonedSchema.type,
181-
properties: {},
182-
required: [],
183-
};
184-
185-
// Check keys using mappedUserData
186-
Object.keys(clonedSchema.properties).forEach((key) => {
187-
if (!(key in mappedUserData) || isEmpty(mappedUserData[key])) {
188-
result.properties[key] = clonedSchema.properties[key];
189-
}
190-
});
191-
192-
if (Array.isArray(clonedSchema.required)) {
193-
result.required = clonedSchema.required.filter(
194-
(field: any) =>
195-
!(field in mappedUserData) || isEmpty(mappedUserData[field])
196-
);
197-
}
198-
199-
// Remove guardian fields from result
200-
guardianFields.forEach((field) => {
201-
delete result.properties[field];
202-
});
203-
204-
if (Array.isArray(result.required)) {
205-
result.required = result.required.filter(
206-
(field: any) => !guardianFields.includes(field)
207-
);
208-
}
209-
210-
// const hasDOB = !!mappedUserData.dob;
211-
// if (hasDOB) {
212-
// guardianFields.forEach((field) => {
213-
// if (!mappedUserData[field] || isEmpty(mappedUserData[field])) {
214-
// // Use the field from schema if available, otherwise fallback
215-
// result.properties[field] = clonedSchema.properties[field] || {
216-
// type: 'string',
217-
// title: field.replace(/_/g, ' ').toUpperCase(),
218-
// };
219-
// }
220-
// });
221-
// } else {
222-
// guardianFields.forEach((field) => {
223-
// if (field in result.properties) {
224-
// delete result.properties[field];
225-
// }
226-
// });
227-
// }
228-
229-
// if (result.properties.dob) {
230-
// guardianFields.forEach((field) => {
231-
// if (!result.properties[field]) {
232-
// result.properties[field] = {
233-
// type: 'string',
234-
// title: field.toUpperCase(),
235-
// };
236-
// }
237-
// });
238-
// } else {
239-
// guardianFields.forEach((field) => {
240-
// if (result.properties[field]) {
241-
// delete result.properties[field];
242-
// }
243-
// });
244-
// }
245-
if(mappedUserData.spouse_name) {
246-
delete result.properties.mother_name;
247-
delete result.properties.father_name;
248-
249-
}
250-
else if(mappedUserData.father_name ) {
251-
delete result.properties.mother_name;
252-
delete result.properties.spouse_name;
253-
}
254-
else if(mappedUserData.mother_name) {
255-
delete result.properties.spouse_name;
256-
delete result.properties.father_name;
257-
}
258-
259-
260-
return result;
261-
} catch (error) {
262-
console.error('Error in getMissingFields:', error);
263-
return null;
22+
const month = MONTHS[parseInt(monthIndex, 10) - 1];
23+
return `${day} ${month}, ${year}`;
26424
}
26525
};
266-
export const maskMobileNumber = (mobile: string) => {
267-
if (mobile && mobile.length < 2) return mobile;
268-
else if (mobile) {
269-
const first = mobile[0];
270-
const last = mobile[mobile.length - 1];
271-
const masked = '*'.repeat(mobile.length - 2);
272-
return first + masked + last;
273-
}
274-
};
275-
export const preserveLocalStorage = () => {
276-
const keysToKeep = [
277-
'preferredLanguage',
278-
'mui-mode',
279-
'mui-color-scheme-dark',
280-
'mui-color-scheme-light',
281-
'hasSeenTutorial',
282-
'lang',
283-
'uiConfig',
284-
'userProgram',
285-
'userProgramTenantId'
286-
];
287-
288-
const valuesToKeep: { [key: string]: any } = {};
289-
290-
keysToKeep.forEach((key: string) => {
291-
const value = localStorage.getItem(key);
292-
if (value !== null) {
293-
valuesToKeep[key] = value;
294-
}
295-
});
296-
297-
localStorage.clear();
29826

299-
keysToKeep.forEach((key: string) => {
300-
if (valuesToKeep[key] !== undefined) {
301-
localStorage.setItem(key, valuesToKeep[key]);
302-
}
303-
});
27+
export const transformLabel = (label: string): string => {
28+
if (typeof label !== 'string') {
29+
return label;
30+
}
31+
return label
32+
?.toLowerCase()
33+
.replace(/[_-]/g, ' ')
34+
.replace(/\b\w/g, (char) => char.toUpperCase());
30435
};
305-
export const isUnderEighteen = (dobString: any): boolean => {
306-
if (!dobString) return false;
307-
308-
const dob = new Date(dobString);
309-
if (isNaN(dob.getTime())) return false; // Invalid date check
31036

311-
const today = new Date();
312-
let age = today.getFullYear() - dob.getFullYear();
313-
const monthDiff = today.getMonth() - dob.getMonth();
314-
const dayDiff = today.getDate() - dob.getDate();
315-
316-
if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) {
317-
age--;
37+
export const firstLetterInUpperCase = (label: string): string => {
38+
if (!label) {
39+
return '';
31840
}
31941

320-
return age < 18;
42+
return label
43+
?.split(' ')
44+
?.map((word) => word?.charAt(0).toUpperCase() + word?.slice(1))
45+
?.join(' ');
32146
};
32247

323-
export const SUPPORTED_MIME_TYPES = [
324-
'application/vnd.ekstep.ecml-archive',
325-
'application/vnd.ekstep.html-archive',
326-
'application/vnd.ekstep.h5p-archive',
327-
'application/pdf',
328-
'video/mp4',
329-
'video/webm',
330-
'application/epub',
331-
'video/x-youtube',
332-
'application/vnd.sunbird.questionset',
333-
];
334-
export const toPascalCase = (name: string | any) => {
335-
if (typeof name !== 'string') {
336-
return name;
337-
}
338-
339-
return name
340-
?.toLowerCase()
341-
.split(' ')
342-
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
343-
.join(' ');
344-
};

0 commit comments

Comments
 (0)