diff --git a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/Module.js b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/Module.js index 224c2bab5c7..89a3c012ed9 100644 --- a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/Module.js +++ b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/Module.js @@ -1,5 +1,5 @@ import { Loader, TourProvider } from "@egovernments/digit-ui-react-components"; -import React from "react"; +import React, { useState, useEffect } from "react"; import { useRouteMatch } from "react-router-dom"; import EmployeeApp from "./pages/employee"; import { CustomisedHooks } from "./hooks"; @@ -68,27 +68,29 @@ export const CONSOLE_MDMS_MODULENAME = "HCM-ADMIN-CONSOLE"; */ const CampaignModule = ({ stateCode, userType, tenants }) => { const tenantId = Digit.ULBService.getCurrentTenantId(); - const { data: BOUNDARY_HIERARCHY_TYPE, isLoading: hierarchyLoading } = Digit.Hooks.useCustomMDMS( - tenantId, - CONSOLE_MDMS_MODULENAME, - [ - { - name: "HierarchySchema", - filter: `[?(@.type=='${window.Digit.Utils.campaign.getModuleName()}')]`, - }, - ], - { - select: (data) => { - return data?.[CONSOLE_MDMS_MODULENAME]?.HierarchySchema?.[0]?.hierarchy; - }, - }, - { schemaCode: `${CONSOLE_MDMS_MODULENAME}.HierarchySchema` } - ); + const [filteredHierarchyType, setFilteredHierarchyType] = useState(null); + const [employeeData, setEmployeeData] = useState(null); + const [loading, setLoading] = useState(true); + + // Using the new custom hook + const { + data: BOUNDARY_HIERARCHY_TYPE, + isLoading: hierarchyLoading, + rawData: hierarchyData + } = Digit.Hooks.campaign.useEmployeeHierarchyType(tenantId, { + select: (data) => data?.hierarchy + }); + + const hierarchyRelationData = Digit.Hooks.campaign.useBoundaryRelationshipSearch({ + BOUNDARY_HIERARCHY_TYPE, + tenantId + }); - const hierarchyData = Digit.Hooks.campaign.useBoundaryRelationshipSearch({ BOUNDARY_HIERARCHY_TYPE, tenantId }); const modulePrefix = "hcm"; + const moduleCode = BOUNDARY_HIERARCHY_TYPE + ? [`boundary-${BOUNDARY_HIERARCHY_TYPE}`] + : ["campaignmanager", "schema", "admin-schemas", "checklist", "appconfiguration"]; - const moduleCode = BOUNDARY_HIERARCHY_TYPE ? [`boundary-${BOUNDARY_HIERARCHY_TYPE}`] : ["campaignmanager", "schema", "admin-schemas", "checklist", "appconfiguration"]; const { path, url } = useRouteMatch(); const language = Digit.StoreData.getCurrentLanguage(); @@ -112,7 +114,13 @@ const CampaignModule = ({ stateCode, userType, tenants }) => { stateCode={stateCode} url={url} userType={userType} - hierarchyData={hierarchyData} + hierarchyData={hierarchyRelationData} + employeeDetails={hierarchyData?.employee} + allHierarchy={{ + [CONSOLE_MDMS_MODULENAME]: { + HierarchySchema: hierarchyData?.allHierarchies + } + }} /> diff --git a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignType.js b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignType.js index 0962132ce89..2992f67ac83 100644 --- a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignType.js +++ b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/components/CampaignType.js @@ -1,8 +1,8 @@ import React, { useState, useMemo, useRef, useEffect, Fragment } from "react"; import { Header } from "@egovernments/digit-ui-react-components"; import { useTranslation } from "react-i18next"; -import { InfoCard, LabelFieldPair } from "@egovernments/digit-ui-components"; -import { Button, CardText, Dropdown, ErrorMessage, PopUp, Stepper, TextBlock , Card} from "@egovernments/digit-ui-components"; +import { InfoCard, LabelFieldPair, Toast } from "@egovernments/digit-ui-components"; +import { Button, CardText, Dropdown, ErrorMessage, PopUp, Stepper, TextBlock , Card, Loader} from "@egovernments/digit-ui-components"; const CampaignSelection = ({ onSelect, formData, formState, ...props }) => { const { t } = useTranslation(); @@ -20,10 +20,27 @@ const CampaignSelection = ({ onSelect, formData, formState, ...props }) => { const [currentStep , setCurrentStep] = useState(1); const currentKey = searchParams.get("key"); const source = searchParams.get("source"); + const [showToast, setShowToast] = useState(false); const [key, setKey] = useState(() => { const keyParam = searchParams.get("key"); return keyParam ? parseInt(keyParam) : 1; }); + const [loading, setLoading] = useState(true); + + const { + data: BOUNDARY_HIERARCHY_TYPE, + isLoading: hierarchyLoading, + rawData: rawData + } = Digit.Hooks.campaign.useEmployeeHierarchyType(tenantId, { + select: (data) => data?.hierarchy + }); + + const employeeDetails = useMemo(() => { + return rawData?.employee; + }, [rawData]); + const hierarchies = useMemo(()=>{ + return rawData?.allHierarchies; + }, [rawData]) function updateUrlParams(params) { const url = new URL(window.location.href); @@ -33,6 +50,68 @@ const CampaignSelection = ({ onSelect, formData, formState, ...props }) => { window.history.replaceState({}, "", url); } + const [filteredProjectTypes, setFilteredProjectTypes] = useState([]); + const getGroupsFromMatchingHierarchies = (employeeDetails, hierarchies) => { + if (!employeeDetails?.assignments || !hierarchies?.length) { + return []; + } + + // Get employee departments from assignments + const employeeDepartments = new Set( + employeeDetails.assignments + .filter(assignment => assignment.department) + .map(assignment => assignment.department.toUpperCase()) + ); + + // Filter hierarchies where employee department matches hierarchy department + const matchingHierarchies = hierarchies.filter(hierarchy => { + // Handle both single department and array of departments + const hierarchyDepartments = Array.isArray(hierarchy?.department) + ? hierarchy?.department + : [hierarchy?.department]; + + // Check if any of the hierarchy departments match any employee departments + return hierarchyDepartments.some(dept => + employeeDepartments.has(dept?.toUpperCase()) + ); + }); + + // Extract unique groups from matching hierarchies + const groups = new Set( + matchingHierarchies + .filter(hierarchy => hierarchy.group) + .flatMap(hierarchy => hierarchy.group) + ); + return Array.from(groups); + // return [ "POLIO"]; + }; + + // Updated function to filter project types + const filterProjectTypesByHierarchy = (employeeDetails, hierarchies, allProjectTypes) => { + if (!allProjectTypes?.["HCM-PROJECT-TYPES"]?.projectTypes) { + return []; + } + + // Get groups from matching hierarchies + const validGroups = getGroupsFromMatchingHierarchies(employeeDetails, hierarchies); + + // Filter project types based on valid groups + return allProjectTypes["HCM-PROJECT-TYPES"].projectTypes.filter(project => { + if (!project.group) return false; + return validGroups.includes(project.group); + }); + }; + + // Usage in your component + useEffect(() => { + if (employeeDetails && projectType && hierarchies) { + const filtered = filterProjectTypesByHierarchy(employeeDetails, hierarchies, projectType); + setLoading(false); + setFilteredProjectTypes(filtered); + if(!filtered) setShowToast(true); + } + }, [employeeDetails, projectType, hierarchies]); + useEffect(() => { if (props?.props?.isSubmitting && !type) { setError({ message: "CAMPAIGN_FIELD_MANDATORY" }); @@ -92,127 +171,139 @@ const CampaignSelection = ({ onSelect, formData, formState, ...props }) => { return ( <> -
-
- - - - - - -
+ {loading && } + {!loading && ( +
+
+ + + + + + +
-
- -
{t(`HCM_CAMPAIGN_TYPE_HEADER`)}
-

{t(`HCM_CAMPAIGN_TYPE_DESCRIPTION`)}

- -
- {`${t("HCM_CAMPAIGN_TYPE")}`} - * -
-
{ - if (props?.props?.sessionData?.HCM_CAMPAIGN_TYPE?.projectType && !canUpdate && source!=="microplan") { - setShowPopUp(true); +
+ +
{t(`HCM_CAMPAIGN_TYPE_HEADER`)}
+

{t(`HCM_CAMPAIGN_TYPE_DESCRIPTION`)}

+ +
+ {`${t("HCM_CAMPAIGN_TYPE")}`} + * +
+
{ + if (props?.props?.sessionData?.HCM_CAMPAIGN_TYPE?.projectType && !canUpdate && source !== "microplan") { + setShowPopUp(true); + return; + } return; - } - return; - }} - onFocus={(e) => { - if (props?.props?.sessionData?.HCM_CAMPAIGN_TYPE?.projectType && !canUpdate) { - setShowPopUp(true); + }} + onFocus={(e) => { + if (props?.props?.sessionData?.HCM_CAMPAIGN_TYPE?.projectType && !canUpdate) { + setShowPopUp(true); + return; + } return; - } - return; - }} - > - { - setStartValidation(true); - handleChange(value); }} - disabled = {source === "microplan"} - - /> - {error?.message && } -
-
- {showBeneficiary && ( - -
{`${t("HCM_BENEFICIARY_TYPE")}`}
-
{t(`CAMPAIGN_TYPE_${beneficiaryType}`)}
+ > + { + setStartValidation(true); + handleChange(value); + }} + disabled={source === "microplan"} + /> + {error?.message && } +
- )} - - + {showBeneficiary && ( + +
{`${t("HCM_BENEFICIARY_TYPE")}`}
+
{t(`CAMPAIGN_TYPE_${beneficiaryType}`)}
+
+ )} + + +
+ + {showPopUp && ( + + {t("ES_CAMPAIGN_UPDATE_TYPE_MODAL_TEXT") + " "} +
, + ]} + onOverlayClick={() => { + setShowPopUp(false); + }} + onClose={() => { + setShowPopUp(false); + }} + footerChildren={[ +
- {showPopUp && ( - - {t("ES_CAMPAIGN_UPDATE_TYPE_MODAL_TEXT") + " "} -
, - ]} - onOverlayClick={() => { - setShowPopUp(false); - }} - onClose={() => { - setShowPopUp(false); - }} - footerChildren={[ -