Skip to content

Commit

Permalink
Merge pull request #67 from Rushikesh-Sonawane99/release-1.0.0
Browse files Browse the repository at this point in the history
Issue #PS-1255 chore: Modified validations for edit user profile
  • Loading branch information
itsvick authored Aug 1, 2024
2 parents 02db8b2 + da43169 commit edac8bb
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 134 deletions.
34 changes: 17 additions & 17 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 @@ -98,24 +98,24 @@ const AddFacilitatorModal: React.FC<AddFacilitatorModalprops> = ({
data: IChangeEvent<any, RJSFSchema, any>,
event: React.FormEvent<any>
) => {
if(!isEditModal){
if (!isEditModal) {
setOpenModal(true);
}
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
182 changes: 91 additions & 91 deletions src/components/AddLeanerModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface AddLearnerModalProps {
formData?: object;
isEditModal?: boolean;
userId?: string;
onReload?: (() => void)| undefined;
onReload?: (() => void) | undefined;
}
const AddLearnerModal: React.FC<AddLearnerModalProps> = ({
open,
Expand All @@ -33,7 +33,7 @@ const AddLearnerModal: React.FC<AddLearnerModalProps> = ({
formData,
isEditModal = false,
userId,
onReload
onReload,
}) => {
const [schema, setSchema] = React.useState<any>();
const [uiSchema, setUiSchema] = React.useState<any>();
Expand Down 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 Expand Up @@ -174,11 +174,11 @@ const AddLearnerModal: React.FC<AddLearnerModalProps> = ({
userData: userData,
customFields: customFields,
};
const response = await editEditUser(userId, object);
const response = await editEditUser(userId, object);
if (response) {
showToastMessage(t('COMMON.LEARNER_UPDATED_SUCCESSFULLY'), 'success');
setReloadProfile(true);
onReload?.();
setReloadProfile(true);
onReload?.();
}
} else {
const response = await createUser(apiBody);
Expand Down Expand Up @@ -215,41 +215,41 @@ const AddLearnerModal: React.FC<AddLearnerModalProps> = ({
justifyContent: 'space-between',
}}
>
<>
<Button
variant="outlined"
color="primary"
sx={{
'&.Mui-disabled': {
backgroundColor: theme?.palette?.primary?.main,
},
minWidth: '84px',
height: '2.5rem',
padding: theme.spacing(1),
fontWeight: '500',
width: '48%',
}}
onClick={onClose}
>
{t('COMMON.BACK')}
</Button>
<Button
variant="contained"
color="primary"
sx={{
'&.Mui-disabled': {
backgroundColor: theme?.palette?.primary?.main,
},
minWidth: '84px',
height: '2.5rem',
padding: theme.spacing(1),
fontWeight: '500',
width: '48%',
}}
onClick={secondaryActionHandler}
>
{t('COMMON.SUBMIT')}
</Button>
<>
<Button
variant="outlined"
color="primary"
sx={{
'&.Mui-disabled': {
backgroundColor: theme?.palette?.primary?.main,
},
minWidth: '84px',
height: '2.5rem',
padding: theme.spacing(1),
fontWeight: '500',
width: '48%',
}}
onClick={onClose}
>
{t('COMMON.BACK')}
</Button>
<Button
variant="contained"
color="primary"
sx={{
'&.Mui-disabled': {
backgroundColor: theme?.palette?.primary?.main,
},
minWidth: '84px',
height: '2.5rem',
padding: theme.spacing(1),
fontWeight: '500',
width: '48%',
}}
onClick={secondaryActionHandler}
>
{t('COMMON.SUBMIT')}
</Button>
</>
</div>
);
Expand All @@ -272,45 +272,45 @@ const AddLearnerModal: React.FC<AddLearnerModalProps> = ({

return (
<>
<SimpleModal
open={open}
onClose={onClose}
showFooter={false}
modalTitle={t('COMMON.NEW_LEARNER')}
>
{formData
? schema &&
uiSchema && (
<DynamicForm
schema={schema}
uiSchema={uiSchema}
onSubmit={handleSubmit}
onChange={handleChange}
onError={handleError}
widgets={{}}
showErrorList={true}
customFields={customFields}
formData={formData}
>
{/* <CustomSubmitButton onClose={primaryActionHandler} /> */}
</DynamicForm>
)
: schema &&
uiSchema && (
<DynamicForm
schema={schema}
uiSchema={uiSchema}
onSubmit={handleSubmit}
onChange={handleChange}
onError={handleError}
widgets={{}}
showErrorList={true}
customFields={customFields}
>
{/* <CustomSubmitButton onClose={primaryActionHandler} /> */}
</DynamicForm>
)}
</SimpleModal>
<SimpleModal
open={open}
onClose={onClose}
showFooter={false}
modalTitle={t('COMMON.NEW_LEARNER')}
>
{formData
? schema &&
uiSchema && (
<DynamicForm
schema={schema}
uiSchema={uiSchema}
onSubmit={handleSubmit}
onChange={handleChange}
onError={handleError}
widgets={{}}
showErrorList={true}
customFields={customFields}
formData={formData}
>
{/* <CustomSubmitButton onClose={primaryActionHandler} /> */}
</DynamicForm>
)
: schema &&
uiSchema && (
<DynamicForm
schema={schema}
uiSchema={uiSchema}
onSubmit={handleSubmit}
onChange={handleChange}
onError={handleError}
widgets={{}}
showErrorList={true}
customFields={customFields}
>
{/* <CustomSubmitButton onClose={primaryActionHandler} /> */}
</DynamicForm>
)}
</SimpleModal>
</>
);
};
Expand Down
37 changes: 25 additions & 12 deletions src/pages/learner/[userId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const LearnerProfile: React.FC = () => {
const [reload, setReload] = React.useState(false);

const handleReload = () => {
setReload(prev => !prev);
setReload((prev) => !prev);
};

const StyledMenu = styled((props: MenuProps) => (
Expand Down Expand Up @@ -198,44 +198,57 @@ 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 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') {
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];
// 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
Loading

0 comments on commit edac8bb

Please sign in to comment.