Skip to content

Commit

Permalink
Issue #PS-1255 chore: Modified validations for edit user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushikesh-Sonawane99 committed Aug 1, 2024
1 parent 02db8b2 commit 168f94d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 56 deletions.
32 changes: 16 additions & 16 deletions src/components/AddFacilitator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import { tenantId } from '../../app.config';
interface AddFacilitatorModalprops {
open: boolean;
onClose: () => void;
formData?: object;
userFormData?: object;
isEditModal?: boolean;
userId?: string;
}
const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
open,
onClose,
formData,
userFormData,
isEditModal = false,
userId,
}) => {
Expand Down Expand Up @@ -104,18 +104,18 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
const target = event.target as HTMLFormElement;
const elementsArray = Array.from(target.elements);

for (const element of elementsArray) {
if (
(element instanceof HTMLInputElement ||
element instanceof HTMLSelectElement ||
element instanceof HTMLTextAreaElement) &&
(element.value === '' ||
(Array.isArray(element.value) && element.value.length === 0))
) {
element.focus();
return;
}
}
// for (const element of elementsArray) {
// if (
// (element instanceof HTMLInputElement ||
// element instanceof HTMLSelectElement ||
// element instanceof HTMLTextAreaElement) &&
// (element.value === '' ||
// (Array.isArray(element.value) && element.value.length === 0))
// ) {
// element.focus();
// return;
// }
// }

const formData = data.formData;
console.log('Form data submitted:', formData);
Expand Down Expand Up @@ -235,7 +235,7 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
showFooter={false}
modalTitle={t('COMMON.NEW_FACILITATOR')}
>
{formData
{userFormData
? schema &&
uiSchema && (
<DynamicForm
Expand All @@ -247,7 +247,7 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
widgets={{}}
showErrorList={true}
customFields={customFields}
formData={formData}
formData={userFormData}
>
{/* <CustomSubmitButton onClose={primaryActionHandler} /> */}
</DynamicForm>
Expand Down
24 changes: 12 additions & 12 deletions src/components/AddLeanerModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ const AddLearnerModal: React.FC<AddLearnerModalProps> = ({
const target = event.target as HTMLFormElement;
const elementsArray = Array.from(target.elements);

for (const element of elementsArray) {
if (
(element instanceof HTMLInputElement ||
element instanceof HTMLSelectElement ||
element instanceof HTMLTextAreaElement) &&
(element.value === '' ||
(Array.isArray(element.value) && element.value.length === 0))
) {
element.focus();
return;
}
}
// for (const element of elementsArray) {
// if (
// (element instanceof HTMLInputElement ||
// element instanceof HTMLSelectElement ||
// element instanceof HTMLTextAreaElement) &&
// (element.value === '' ||
// (Array.isArray(element.value) && element.value.length === 0))
// ) {
// element.focus();
// return;
// }
// }
console.log('Form data submitted:', data.formData);

const formData = data.formData;
Expand Down
44 changes: 30 additions & 14 deletions src/pages/learner/[userId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,44 +198,60 @@ const LearnerProfile: React.FC = () => {
let initialFormData: any = {};
formFields.fields.forEach((item: any) => {
const userData = response?.userData;
const customField = userData?.customFields?.find(
const customFieldValue = userData?.customFields?.find(
(field: any) => field.fieldId === item.fieldId
);
const getValue = (data: any, field: any) => {
if (item.default) {
return item.default;
}
if (item?.isMultiSelect) {
if (data[item.name] && item?.maxSelections > 1) {
return [field.value];
return [field?.value];
} else if (item?.type === 'checkbox') {
return String(field.value).split(',');
return String(field?.value).split(',');
} else {
return field.value;
return field?.value;
}
} else {
if (item?.type === 'numeric') {
return Number(field.value);
return parseInt(String(field?.value));
} else if (item?.type === 'text') {
return String(field.value);
return String(field?.value);
} else {
return field.value;
return field?.value;
}
}
};
if (item.coreField) {
if (item?.isMultiSelect) {
if (userData[item.name] && item?.maxSelections > 1) {
initialFormData[item.name] = [userData[item.name]];
} else {
initialFormData[item.name] = userData[item.name] || '';
}
else if (item?.type === "checkbox") {
initialFormData[item.name]= String(userData[item.name]).split(",");
}
} else if (item?.type === 'numeric') {
else {
initialFormData[item.name] = userData[item.name];
}
} else if (item?.type === "numeric") {
console.log(item?.name);
initialFormData[item.name] = Number(userData[item.name]);
} else if (item?.type === 'text') {
} else if (item?.type === "text" && userData[item.name]) {
initialFormData[item.name] = String(userData[item.name]);
} else {
initialFormData[item.name] = userData[item.name];
}
else {
// console.log(item.name);
if (userData[item.name]) {
initialFormData[item.name] = userData[item.name];
}
}
} else {
initialFormData[item.name] = getValue(userData, customField);
const fieldValue = getValue(userData, customFieldValue);

if (fieldValue) {
initialFormData[item.name] = fieldValue;
}
}
});
console.log('initialFormData', initialFormData);
Expand Down
41 changes: 28 additions & 13 deletions src/pages/user-profile/[userId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const TeacherProfile = () => {
const [blockName, setBlockName] = useState('');
const [isError, setIsError] = React.useState<boolean>(false);
const [isData, setIsData] = React.useState<boolean>(false);
const [formData, setFormData] = useState<{ [key: string]: any }>({});
const [userFormData, setUserFormData] = useState<{ [key: string]: any }>({});
const [openAddLearnerModal, setOpenAddLearnerModal] = React.useState(false);

const handleOpenAddLearnerModal = () => {
Expand All @@ -85,7 +85,7 @@ const TeacherProfile = () => {
let initialFormData: any = {};
formFields.fields.forEach((item: any) => {
const userData = response?.userData;
const customField = userData?.customFields?.find(
const customFieldValue = userData?.customFields?.find(
(field: any) => field.fieldId === item.fieldId
);
const getValue = (data: any, field: any) => {
Expand All @@ -94,15 +94,21 @@ const TeacherProfile = () => {
}
if (item?.isMultiSelect) {
if (data[item.name] && item?.maxSelections > 1) {
return [field.value];
if (field.value) {
return [field.value];
}
return null;
} else if (item?.type === 'checkbox') {
return String(field.value).split(',');
if (field.value) {
return String(field.value).split(',');
}
return null;
} else {
return field.value;
}
} else {
if (item?.type === 'numeric') {
return Number(field.value);
return parseInt(String(field.value));
} else if (item?.type === 'text') {
return String(field.value);
} else {
Expand All @@ -114,18 +120,27 @@ const TeacherProfile = () => {
if (item?.isMultiSelect) {
if (userData[item.name] && item?.maxSelections > 1) {
initialFormData[item.name] = [userData[item.name]];
} else if (item?.type === 'checkbox') {
initialFormData[item.name] = String(userData[item.name]).split(',');
} else {
initialFormData[item.name] = userData[item.name] || '';
initialFormData[item.name] = userData[item.name];
}
} else if (item?.type === 'numeric') {
initialFormData[item.name] = Number(userData[item.name]);
} else if (item?.type === 'text') {
} else if (item?.type === 'text' && userData[item.name]) {
initialFormData[item.name] = String(userData[item.name]);
} else {
initialFormData[item.name] = userData[item.name];
if (userData[item.name]) {
initialFormData[item.name] = userData[item.name];
}
}
} else {
initialFormData[item.name] = getValue(userData, customField);
// For Custom Fields
const fieldValue = getValue(userData, customFieldValue);

if (fieldValue) {
initialFormData[item.name] = fieldValue;
}
}
});
console.log('initialFormData', initialFormData);
Expand All @@ -136,10 +151,10 @@ const TeacherProfile = () => {
try {
let formFields;
const response = await getUserDetails(userId, true);
formFields = await getFormRead('USERS', 'TEACHER'); //TODO: Change for TL
formFields = await getFormRead('USERS', 'TEACHER');
console.log('response', response);
console.log('formFields', formFields);
setFormData(mapFields(formFields, response?.result));
setUserFormData(mapFields(formFields, response?.result));
} catch (error) {
console.error('Error fetching data or initializing form:', error);
}
Expand Down Expand Up @@ -190,7 +205,7 @@ const TeacherProfile = () => {
const fetchFormData = async () => {
try {
const formContextType =
(userRole === Role.TEAM_LEADER && selfUserId === userId)
userRole === Role.TEAM_LEADER && selfUserId === userId
? FormContextType.TEAM_LEADER
: FormContextType.TEACHER;
const response: FormData = await getFormRead(
Expand Down Expand Up @@ -543,7 +558,7 @@ const TeacherProfile = () => {
<AddFacilitatorModal
open={openAddLearnerModal}
onClose={handleCloseAddLearnerModal}
formData={formData}
userFormData={userFormData}
isEditModal={true}
userId={userId}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const toPascalCase = (name: string | any) => {
}

return name
.toLowerCase()
?.toLowerCase()
.split(' ')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
Expand Down

0 comments on commit 168f94d

Please sign in to comment.