diff --git a/app.config.ts b/app.config.ts new file mode 100644 index 00000000..b48fe0ee --- /dev/null +++ b/app.config.ts @@ -0,0 +1 @@ +export const tenantId = "ef99949b-7f3a-4a5f-806a-e67e683e38f3" \ No newline at end of file diff --git a/public/locales/en/common.json b/public/locales/en/common.json index cc9a64a3..536de7aa 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -42,7 +42,7 @@ "CODE_NOTIFICATION": "Code Should Be Fill With Captial Letters", "USER_DELETE_SUCCSSFULLY": "User deleted Successfully", "STATE_NAME": "State Name", - "FILTER_BY_STATUS": "Filter By Status :" + "FILTER_BY_STATUS": "Filter By Status" }, "LOGIN_PAGE": { "USERNAME": "Username", diff --git a/src/components/AddFacilitator.tsx b/src/components/AddFacilitator.tsx index f3adfe15..cb89ba75 100644 --- a/src/components/AddFacilitator.tsx +++ b/src/components/AddFacilitator.tsx @@ -23,6 +23,7 @@ import AreaSelection from "./AreaSelection"; import { showToastMessage } from "./Toastify"; import { transformArray } from "../utils/Helper"; import { useLocationState } from "@/utils/useLocationState"; +import { tenantId } from "../../app.config"; interface AddFacilitatorModalprops { open: boolean; @@ -141,7 +142,7 @@ const AddFacilitatorModal: React.FC = ({ password: password, tenantCohortRoleMapping: [ { - tenantId: "ef99949b-7f3a-4a5f-806a-e67e683e38f3", + tenantId: tenantId, roleId: RoleId.TEACHER, cohortId: [selectedCenterCode], }, @@ -216,7 +217,7 @@ const AddFacilitatorModal: React.FC = ({ customFields: customFields, }; const response = await updateUser(userId, object); - showToastMessage(t("LEARNERS.LEARNER_UPDATED_SUCCESSFULLY"), "success"); + showToastMessage(t("FACILITATORS.FACILITATOR_UPDATED_SUCCESSFULLY"), "success"); } else { try{ const response = await createUser(apiBody); diff --git a/src/components/AddLeanerModal.tsx b/src/components/AddLeanerModal.tsx index d405570f..666dbaa8 100644 --- a/src/components/AddLeanerModal.tsx +++ b/src/components/AddLeanerModal.tsx @@ -26,6 +26,7 @@ import { useTranslation } from "react-i18next"; import { transformArray } from "../utils/Helper"; import AreaSelection from "./AreaSelection"; import { showToastMessage } from "./Toastify"; +import { tenantId } from "../../app.config"; interface AddLearnerModalProps { open: boolean; @@ -106,8 +107,7 @@ const AddLearnerModal: React.FC = ({ const target = event.target as HTMLFormElement; const elementsArray = Array.from(target.elements); - -console.log("onsubmit", data); + console.log("onsubmit", data); // for (const element of elementsArray) { // if ( // (element instanceof HTMLInputElement || @@ -140,7 +140,7 @@ console.log("onsubmit", data); password: password, tenantCohortRoleMapping: [ { - tenantId: "ef99949b-7f3a-4a5f-806a-e67e683e38f3", + tenantId: tenantId, roleId: RoleId.STUDENT, cohortId: [selectedCenterCode], }, diff --git a/src/components/AddTeamLeaderModal.tsx b/src/components/AddTeamLeaderModal.tsx new file mode 100644 index 00000000..c4f0d222 --- /dev/null +++ b/src/components/AddTeamLeaderModal.tsx @@ -0,0 +1,388 @@ +import DynamicForm from "@/components/DynamicForm"; +import { + GenerateSchemaAndUiSchema, + customFields, +} from "@/components/GeneratedSchemas"; +import SimpleModal from "@/components/SimpleModal"; +import { + createUser, + getFormRead, + updateUser, +} from "@/services/CreateUserService"; +import { tenantId } from "../../app.config"; +import { + + getCohortList + +} from "@/services/CohortService/cohortService"; +import { generateUsernameAndPassword } from "@/utils/Helper"; +import { FormData } from "@/utils/Interfaces"; +import { + FormContext, + FormContextType, + Role, + RoleId, +} from "@/utils/app.constant"; +import { useLocationState } from "@/utils/useLocationState"; +import { Box, Button, Typography, useTheme } from "@mui/material"; +import { IChangeEvent } from "@rjsf/core"; +import { RJSFSchema } from "@rjsf/utils"; +import React, { useEffect } from "react"; +import { useTranslation } from "react-i18next"; +import { transformArray } from "../utils/Helper"; +import AreaSelection from "./AreaSelection"; +import { showToastMessage } from "./Toastify"; + +interface AddLearnerModalProps { + open: boolean; + onClose: () => void; + formData?: object; + isEditModal?: boolean; + userId?: string; +} +interface FieldProp { + value: string; + label: string; +} +const AddTeamLeaderModal: React.FC = ({ + open, + onClose, + formData, + isEditModal = false, + userId, +}) => { + const [schema, setSchema] = React.useState(); + const [uiSchema, setUiSchema] = React.useState(); + const [formValue, setFormValue] = React.useState(); + + + const [credentials, setCredentials] = React.useState({ + username: "", + password: "", + }); + const { t } = useTranslation(); + const theme = useTheme(); + const { + states, + districts, + blocks, + allCenters, + isMobile, + isMediumScreen, + selectedState, + selectedStateCode, + selectedDistrict, + selectedDistrictCode, + selectedCenter, + dynamicForm, + selectedBlock, + selectedBlockCode, + handleStateChangeWrapper, + handleDistrictChangeWrapper, + handleBlockChangeWrapper, + handleCenterChangeWrapper, + selectedCenterCode, + selectedBlockFieldId, + dynamicFormForBlock + + } = useLocationState(open, onClose); + + useEffect(() => { + const getAddLearnerFormData = async () => { + try { + let userType="TEAM LEADER" + const response: FormData = await getFormRead( + FormContext.USERS, + userType + ); + console.log("sortedFields", response); + + if (response) { + const { schema, uiSchema, formValues } = GenerateSchemaAndUiSchema(response, t); + setFormValue(formValues) + setSchema(schema); + console.log(schema); + setUiSchema(uiSchema); + } + } catch (error) { + console.error("Error fetching form data:", error); + } + }; + getAddLearnerFormData(); + }, []); + + const handleSubmit = async ( + data: IChangeEvent, + event: React.FormEvent + ) => { + // 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; + // } + // } + console.log("Form data submitted:", data.formData); + + const formData = data.formData; + console.log("Form data submitted:", formData); + const schemaProperties = schema.properties; + + const { username, password } = generateUsernameAndPassword( + selectedStateCode, + Role.TEAM_LEADER + ); + + let apiBody: any = { + username: username, + password: password, + tenantCohortRoleMapping: [ + { + tenantId: tenantId, + roleId: RoleId.TEAM_LEADER, + cohortId: [selectedBlockFieldId], + }, + ], + customFields: [], + }; + + Object.entries(formData).forEach(([fieldKey, fieldValue]) => { + const fieldSchema = schemaProperties[fieldKey]; + const fieldId = fieldSchema?.fieldId; + console.log( + `FieldID: ${fieldId}, FieldValue: ${fieldValue}, type: ${typeof fieldValue}` + ); + + if (fieldId === null || fieldId === "null") { + if (typeof fieldValue !== "object") { + apiBody[fieldKey] = fieldValue; + } + } else { + if ( + fieldSchema?.hasOwnProperty("isDropdown") || + fieldSchema.hasOwnProperty("isCheckbox") + ) { + apiBody.customFields.push({ + fieldId: fieldId, + value: [String(fieldValue)], + }); + } else { + apiBody.customFields.push({ + fieldId: fieldId, + value: String(fieldValue), + }); + } + } + }); + if (!isEditModal) { + apiBody.customFields.push({ + fieldId: "394ee6c9-3c4b-4065-ab82-19dc6ab52e67", + value: [selectedBlockCode], + }); + apiBody.customFields.push({ + fieldId: "8d56e5e6-d504-4c51-a848-eb1b0a8d2ed6", + value: [selectedStateCode], + }); + apiBody.customFields.push({ + fieldId: "ea1461c1-2dd3-469d-b58d-52c6acdce30b", + value: [selectedDistrictCode], + }); + } + + try { + if (isEditModal && userId) { + console.log("apiBody", apiBody); + const userData = { + name: apiBody.name, + mobile: apiBody.mobile, + father_name: apiBody.father_name, + }; + const customFields = apiBody.customFields; + console.log(customFields); + const object = { + userData: userData, + customFields: customFields, + }; + const response = await updateUser(userId, object); + showToastMessage(t("TEAM_LEADERS.TEAM_LEADER_UPDATED_SUCCESSFULLY"), "success"); + } else { + const response = await createUser(apiBody); + showToastMessage(t("TEAM_LEADERS.TEAM_LEADER_CREATED_SUCCESSFULLY"), "success"); + } + onClose(); + } catch (error) { + onClose(); + console.log(error); + } + }; + + const handleChange = (event: IChangeEvent) => { + console.log("Form data changed:", event.formData); + // setFormData({ + // ...formData, + // [event.target.name]: event.target.value + // }); + }; + + const handleError = (errors: any) => { + console.log("Form errors:", errors); + }; + + const CustomSubmitButton: React.FC<{ onClose: () => void }> = ({ + onClose, + }) => ( +
+ <> + + + +
+ ); + + const primaryActionHandler = () => { + onClose(); + }; + + const secondaryActionHandler = async (e: React.FormEvent) => { + // console.log('Secondary action handler clicked'); + e.preventDefault(); + // handleGenerateCredentials(); + // try { + // const response = await createUser(learnerFormData); + // console.log('User created successfully', response); + // } catch (error) { + // console.error('Error creating user', error); + // } + }; + + return ( + <> + + <> + + {!dynamicForm && ( + + {t("LEARNERS.FIRST_SELECT_REQUIRED_FIELDS")}{" "} + + )} + + + + + {formData + ? schema && + uiSchema && ( + + {/* */} + + ) + : dynamicFormForBlock && + schema && + uiSchema && ( + + {/* */} + + )} + + + ); +}; + +export default AddTeamLeaderModal; diff --git a/src/components/DynamicForm.tsx b/src/components/DynamicForm.tsx index 34cab4e1..2b081f6e 100644 --- a/src/components/DynamicForm.tsx +++ b/src/components/DynamicForm.tsx @@ -67,6 +67,8 @@ const DynamicForm: React.FC = ({ onError(errors); }; + + function transformErrors(errors: any) { console.log("errors", errors); console.log("schema", schema); diff --git a/src/components/HeaderComponent.tsx b/src/components/HeaderComponent.tsx index a8388359..83b45689 100644 --- a/src/components/HeaderComponent.tsx +++ b/src/components/HeaderComponent.tsx @@ -4,6 +4,7 @@ import { Box, Button, FormControl, + FormLabel, MenuItem, Typography, useMediaQuery, @@ -12,11 +13,16 @@ import Select from "@mui/material/Select"; import { useTheme } from "@mui/material/styles"; import { useTranslation } from "next-i18next"; import { useEffect, useState } from "react"; +import InputLabel from "@mui/material/InputLabel"; +import OutlinedInput from "@mui/material/OutlinedInput"; + import { getStateBlockDistrictList, } from "../services/MasterDataService"; import AreaSelection from "./AreaSelection"; import { transformArray } from "../utils/Helper"; +import MultipleSelectCheckmarks from "./FormControl"; + interface State { value: string; label: string; @@ -175,7 +181,7 @@ const HeaderComponent = ({ isMediumScreen={isMediumScreen} /> )} - + {userType} - + {/* {t("COMMON.FILTER_BY_STATUS")} - - - - + */} + + + {t("COMMON.FILTER_BY_STATUS")} + + + + + {showSort && (