Skip to content

Commit

Permalink
Merge pull request #102 from AkshataKatwal16/admin
Browse files Browse the repository at this point in the history
Issue feat: Resolve required filed issue
  • Loading branch information
itsvick authored Aug 8, 2024
2 parents 3cca8e1 + bea04ec commit 2f9b566
Show file tree
Hide file tree
Showing 18 changed files with 502 additions and 222 deletions.
5 changes: 4 additions & 1 deletion public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
"USERS_EMAIL": "Users Email",
"STATE_NAME_REQUIRED": "State Name is Required",
"DISTRICT_NAME_REQUIRED": "District Name is Required",

"ADD":"Add",
"INACTIVE":"Inactive",
"SELECT_STATE": "Select State",
"SOMETHING_WENT_WRONG": "Something went wrong"
},
Expand Down Expand Up @@ -265,7 +268,7 @@
"ENTER_ONLY_DIGITS": "Please enter only digits",
"MIN_LENGTH_DIGITS_ERROR": "Minimum {{minLength}} Digits required",
"MAX_LENGTH_DIGITS_ERROR": "Maximum {{maxLength}} Digits allowed",
"MIN_LENGTH_CHARACTERS_ERROR": "Minimum {{minLength}} characters required",
"MIN_LENGTH_CHARACTERS_ERROR": "Minimum {{minLength}} characters required",
"MAX_LENGTH_CHARACTERS_ERROR": "Maximum {{maxLength}} characters allowed",
"NUMBER_AND_SPECIAL_CHARACTERS_NOT_ALLOWED": "Numbers and special characters are not allowed",
"CHARACTERS_AND_SPECIAL_CHARACTERS_NOT_ALLOWED": "Characters and special characters are not allowed",
Expand Down
3 changes: 2 additions & 1 deletion src/components/AreaSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useTheme } from "@mui/material/styles";
import { useTranslation } from "next-i18next";
import React from "react";
import MultipleSelectCheckmarks from "./FormControl";
import { capitalizeFirstLetterOfEachWordInArray} from "@/utils/Helper";
interface State {
value: string;
label: string;
Expand Down Expand Up @@ -125,7 +126,7 @@ const AreaSelection: React.FC<DropdownBoxProps> = ({
{isCenterSelection && (
<Grid item xs={12} sm={isMediumScreen ? 12 : 3}>
<MultipleSelectCheckmarks
names={allCenters?.map((centers) => centers.name)}
names={capitalizeFirstLetterOfEachWordInArray(allCenters?.map((centers) => centers.name))}
codes={allCenters?.map((centers) => centers.cohortId)}
tagName={t("CENTERS.CENTERS")}
selectedCategories={selectedCenter}
Expand Down
105 changes: 85 additions & 20 deletions src/components/CommonUserModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { showToastMessage } from "./Toastify";
import { transformArray } from "../utils/Helper";
import { useLocationState } from "@/utils/useLocationState";
import { tenantId } from "../../app.config";

import useSubmittedButtonStore from "@/utils/useSharedState";
interface UserModalProps {
open: boolean;
onClose: () => void;
Expand All @@ -53,8 +53,12 @@ const CommonUserModal: React.FC<UserModalProps> = ({
console.log(userType);
const [schema, setSchema] = React.useState<any>();
const [uiSchema, setUiSchema] = React.useState<any>();
const [submitButtonEnable, setSubmitButtonEnable] = React.useState<boolean>(false);

const { t } = useTranslation();
const [formValue, setFormValue] = useState<any>();
const setSubmittedButtonStatus = useSubmittedButtonStore((state:any) => state.setSubmittedButtonStatus);

const modalTitle = !isEditModal
? userType === FormContextType.STUDENT
? t("LEARNERS.NEW_LEARNER")
Expand Down Expand Up @@ -147,8 +151,9 @@ const CommonUserModal: React.FC<UserModalProps> = ({
data: IChangeEvent<any, RJSFSchema, any>,
event: React.FormEvent<any>,
) => {
console.log("submitted")
// setOpenModal(true);
const target = event.target as HTMLFormElement;
const target = event?.target as HTMLFormElement;
// const elementsArray = Array.from(target.elements);

console.log("onsubmit", data);
Expand Down Expand Up @@ -216,25 +221,33 @@ const CommonUserModal: React.FC<UserModalProps> = ({
fieldSchema?.hasOwnProperty("isDropdown") ||
fieldSchema?.hasOwnProperty("isCheckbox")
) {

apiBody.customFields.push({
fieldId: fieldId,
value: [String(fieldValue)],
});


} else {
if (fieldSchema.checkbox && fieldSchema.type === "array" && isEditModal) {
apiBody.customFields.push({
fieldId: fieldId,
value: String(fieldValue).split(","),
value: [String(fieldValue)],
});


} else {
if (fieldSchema?.checkbox && fieldSchema.type === "array" ) {
if(String(fieldValue).length!=0)
{
apiBody.customFields.push({
fieldId: fieldId,
value: String(fieldValue),
value: String(fieldValue).split(","),
});
}

} else {
if(fieldId)
{
apiBody.customFields.push({
fieldId: fieldId,
value: String(fieldValue),
});
}

}
}
}
});
Expand Down Expand Up @@ -278,6 +291,7 @@ const CommonUserModal: React.FC<UserModalProps> = ({
showToastMessage(t(messageKey), "success");
} else {
const response = await createUser(apiBody);
console.log(response)
if (response) {

const messageKey =
Expand All @@ -289,11 +303,15 @@ const CommonUserModal: React.FC<UserModalProps> = ({

showToastMessage(t(messageKey), "success");
}
else{
showToastMessage(t('COMMON.SOMETHING_WENT_WRONG'), 'error');

}
}

onSubmit(true);
onClose();
} catch (error) {
showToastMessage(t('COMMON.SOMETHING_WENT_WRONG'), 'error');

onClose();
console.log(error);
Expand Down Expand Up @@ -377,17 +395,54 @@ const CommonUserModal: React.FC<UserModalProps> = ({
// console.error('Error creating user', error);
// }
};
useEffect(() => {

if(!open)
{
setSubmitButtonEnable(false)

}
if(dynamicForm && userType!==FormContextType.TEAM_LEADER || isEditModal)
{
setSubmitButtonEnable(true)
}
if(dynamicFormForBlock && userType===FormContextType.TEAM_LEADER || isEditModal)
{
setSubmitButtonEnable(true)

}

}, [dynamicForm,dynamicFormForBlock, open]);
return (
<>
<SimpleModal
open={open}
onClose={onClose}
showFooter={false}
showFooter={true}
modalTitle={modalTitle}
footer={
<Box display="flex" justifyContent="center" mt={2} mb={2}>
<Button
variant="contained"
type="submit"
form="dynamic-form" // Add this line
sx={{
padding: '12px 24px', // Adjust padding as needed
width: '200px', // Set the desired width
}}
disabled={!submitButtonEnable}
onClick={() => {
setSubmittedButtonStatus(true);
console.log("Submit button was clicked");
}}
>
{!isEditModal?t("COMMON.ADD"):t("COMMON.UPDATE")}
</Button>
</Box>
}
>
<>
<Box

{!isEditModal &&( <Box
sx={{
display: "flex",
flexDirection: "column",
Expand Down Expand Up @@ -417,13 +472,14 @@ const CommonUserModal: React.FC<UserModalProps> = ({
selectedCenter={selectedCenter}
handleCenterChangeWrapper={handleCenterChangeWrapper}
/>
</Box>
</>

</Box>)
}
{formData
? schema &&
uiSchema && (
<DynamicForm
id="dynamic-form"

schema={schema}
uiSchema={uiSchema}
onSubmit={handleSubmit}
Expand All @@ -434,14 +490,18 @@ const CommonUserModal: React.FC<UserModalProps> = ({
customFields={customFields}
formData={formData}
>


{/* <CustomSubmitButton onClose={primaryActionHandler} /> */}
</DynamicForm>
)
: userType === "TEAM LEADER"
: userType === FormContextType.TEAM_LEADER
? dynamicFormForBlock &&
schema &&
uiSchema && (
<DynamicForm
id="dynamic-form"

schema={schema}
uiSchema={uiSchema}
onSubmit={handleSubmit}
Expand All @@ -451,6 +511,7 @@ const CommonUserModal: React.FC<UserModalProps> = ({
showErrorList={true}
customFields={customFields}
formData={formValue}

>
{/* <CustomSubmitButton onClose={primaryActionHandler} /> */}
</DynamicForm>
Expand All @@ -459,6 +520,8 @@ const CommonUserModal: React.FC<UserModalProps> = ({
schema &&
uiSchema && (
<DynamicForm
id="dynamic-form"

schema={schema}
uiSchema={uiSchema}
onSubmit={handleSubmit}
Expand All @@ -468,10 +531,12 @@ const CommonUserModal: React.FC<UserModalProps> = ({
showErrorList={true}
customFields={customFields}
formData={formValue}

>
{/* <CustomSubmitButton onClose={primaryActionHandler} /> */}
</DynamicForm>
)}

</SimpleModal>
</>
);
Expand Down
Loading

0 comments on commit 2f9b566

Please sign in to comment.