From 979f68d99bf7c8a6aa06350fc7f4a47a8d3e49fc Mon Sep 17 00:00:00 2001 From: suryansh-egov Date: Wed, 23 Oct 2024 17:26:43 +0530 Subject: [PATCH] hierarchy (#1647) * hierarchy * loader added * cleanup --- .../src/configs/UICustomizations.js | 2 + .../src/pages/employee/Boundary.js | 148 +++++++++++++++- .../src/pages/employee/ViewHierarchy.js | 160 +++++++++++++----- 3 files changed, 260 insertions(+), 50 deletions(-) diff --git a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/configs/UICustomizations.js b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/configs/UICustomizations.js index e571d95226f..93e18badda7 100644 --- a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/configs/UICustomizations.js +++ b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/configs/UICustomizations.js @@ -212,6 +212,7 @@ export const UICustomizations = { }); return res; } + const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); const generateTemplate = async () => { const res = await Digit.CustomService.getResponse({ url: `/project-factory/v1/data/_download`, @@ -228,6 +229,7 @@ export const UICustomizations = { } const downloadExcelTemplate = async () => { const res = await generateFile(); + await delay(2000); const resFile = await generateTemplate(); if (resFile && resFile?.GeneratedResource?.[0]?.fileStoreid) { // Splitting filename before .xlsx or .xls diff --git a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/Boundary.js b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/Boundary.js index 3904d10a72e..53e6effc562 100644 --- a/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/Boundary.js +++ b/health/micro-ui/web/micro-ui-internals/packages/modules/campaign-manager/src/pages/employee/Boundary.js @@ -1,5 +1,5 @@ import { Card, Loader } from "@egovernments/digit-ui-components"; -import { Button } from "@egovernments/digit-ui-components"; +import { Button, Toast } from "@egovernments/digit-ui-components"; import React, { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useHistory } from "react-router-dom"; @@ -9,16 +9,21 @@ const config = { defaultHierarchyType: "DEFAULTBOUNDARY", hierarchyType: "DEMOONCONSOLE", }; +// const [config, setConfig] = useState({defaultHierarchyType: "DEFAULTBOUNDARY", +// hierarchyType: "DEMOONCONSOLE"}); const Boundary = () => { const { t } = useTranslation(); const tenantId = Digit.ULBService.getCurrentTenantId(); const searchParams = new URLSearchParams(location.search); - const defaultHierarchyType = config?.defaultHierarchyType || searchParams.get("defaultHierarchyType"); - const hierarchyType = config?.hierarchyType || searchParams.get("hierarchyType"); + const [defaultHierarchyType, setDefaultHierarchyType] = useState(config?.defaultHierarchyType || searchParams.get("defaultHierarchyType")); + const [hierarchyType, setHierarchyType] = useState(config?.hierarchyType || searchParams.get("hierarchyType")); + const [showPopUp, setShowPopUp] = useState(false); const [geoPodeData, setGeoPodeData] = useState(false); const history = useHistory(); + const mdms_context_path = window?.globalConfigs?.getConfig("MDMS_V2_CONTEXT_PATH") || "mdms-v2"; + const reqCriteriaResource = { url: `/boundary-service/boundary-hierarchy-definition/_search`, body: { @@ -29,11 +34,118 @@ const Boundary = () => { hierarchyType: defaultHierarchyType, }, }, + }; const { isLoading, data, isFetching } = Digit.Hooks.useCustomAPIHook(reqCriteriaResource); + const rc = { + url: `/${mdms_context_path}/v2/_search`, + body: { + MdmsCriteria: { + tenantId: tenantId, + schemaCode: "HCM-ADMIN-CONSOLE.HierarchySchema", + }, + }, + changeQueryName: "SCHEMA_SEARCH", + }; + const { isLoading1, data: mdms, isFetching1 } = Digit.Hooks.useCustomAPIHook(rc); + const user = Digit.UserService.getUser(); + const user_name = user?.info?.userName; + + const rc1 = { + url: `/health-hrms/employees/_search`, + body: { + }, + params: { + tenantId: tenantId, + codes: user_name, + sortOrder: "ASC" + + }, + changeQueryName: user_name, + } + const { isLoading2, data:Employees, isFetching2 } = Digit.Hooks.useCustomAPIHook(rc1); + + const [authorized, setAuthorized] = useState(false); + + const getHierarchyTypes = React.useCallback((dataArray, campaign) => { + return dataArray + .filter(item => item.data.type === campaign) + .map(item => item.data.hierarchytype)[0]; + }, []); + + // Separate useEffect for authorization check and hierarchy type updates useEffect(() => { - if (data?.BoundaryHierarchy && data?.BoundaryHierarchy.length > 0) setGeoPodeData(true); + if (!Employees?.Employees?.[0]?.assignments?.[0]?.department || !mdms) return; + + const dep = Employees.Employees[0].assignments[0].department; + const arrayOfHier = mdms.mdms; + + if (dep === null) { + setAuthorized(true); + return; + } + + const searchForDepInCampaign = (dataArray, dep) => { + return dataArray.some( + (item) => item.data.type === "campaign" && item.data.department.includes(dep) + ); + }; + + const result = searchForDepInCampaign(arrayOfHier, dep); + + if (result) { + setAuthorized(true); + + // Get both hierarchy types at once to avoid multiple state updates + const newHierarchyTypes = getHierarchyTypes(arrayOfHier, "campaign"); + const newDefHierarchyTypes = getHierarchyTypes(arrayOfHier, "default"); + + // Only update if values have changed + if (JSON.stringify(hierarchyType) !== JSON.stringify(newHierarchyTypes)) { + setHierarchyType(newHierarchyTypes); + callGenerate(newHierarchyTypes); + // generateFile().catch(error => { + // console.error("Failed to generate file:", error); + // }); + } + + if (JSON.stringify(defaultHierarchyType) !== JSON.stringify(newDefHierarchyTypes)) { + setDefaultHierarchyType(newDefHierarchyTypes); + } + } else { + setAuthorized(false); + } + }, [Employees, mdms, getHierarchyTypes]); + + + const generateFile = async(hierarchyType)=>{ + const res = await Digit.CustomService.getResponse({ + url: `/project-factory/v1/data/_generate`, + body: { + }, + params: { + tenantId: tenantId, + type: "boundaryManagement", + forceUpdate: true, + hierarchyType: hierarchyType, + campaignId: "default" + } + }); + return res; +} + const callGenerate =async(hierarchyType)=>{ + const res = await generateFile(hierarchyType) + return res; + + } + + useEffect(() => { + if (data?.BoundaryHierarchy && data?.BoundaryHierarchy.length > 0) + { + setGeoPodeData(true); + } }, [data]); + const [direct, setDirect] = useState(false); const [directView, setDirectView] = useState(false); @@ -65,7 +177,7 @@ const Boundary = () => { const callGeoPode = (val) => { history.push( - `/${window.contextPath}/employee/campaign/geopode?defaultHierarchyType=${defaultHierarchyType}&hierarchyType=${hierarchyType}&newHierarchy=${val}`, + `/${window.contextPath}/employee/campaign/boundary/geopode?defaultHierarchyType=${defaultHierarchyType}&hierarchyType=${hierarchyType}&newHierarchy=${val}`, { data: data } ); }; @@ -78,11 +190,15 @@ const Boundary = () => { { data: data } ); }; + + const [toast, setToast] = useState(false); + if (isFetching) return ; return ( - + {toast && + setToast(false)} />}
{t("BOUNDARY_DATA_MANAGEMENT")}
@@ -90,14 +206,22 @@ const Boundary = () => {