Skip to content

Commit

Permalink
Invalidation updates + assumptions integration (#1507)
Browse files Browse the repository at this point in the history
updated to invalidation + assumption api integration
  • Loading branch information
nipunarora-eGov authored Oct 11, 2024
1 parent 8f2ad09 commit 8b0f9d2
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect, Fragment } from "react";
import { useTranslation } from "react-i18next";
import { Card, Header, LabelFieldPair } from "@egovernments/digit-ui-react-components";
import { Dropdown, RadioButtons } from "@egovernments/digit-ui-components";
import { Dropdown, PopUp, RadioButtons,CardText,Button } from "@egovernments/digit-ui-components";
import { useMyContext } from "../utils/context";

const AssumptionsForm = ({ onSelect, ...props }) => {
Expand All @@ -15,6 +15,17 @@ const AssumptionsForm = ({ onSelect, ...props }) => {
const [executionCount, setExecutionCount] = useState(0);
const resourceDistributionStrategyCode = props?.props?.sessionData?.CAMPAIGN_DETAILS?.campaignDetails?.distributionStrat?.resourceDistributionStrategyCode;

const [showPopup,setShowPopup] = useState(false)

//to show alert
useEffect(() => {
//if there are any assumptions filled show this popup by default
if(props.props.sessionData?.HYPOTHESIS?.Assumptions?.assumptionValues.length > 0){
setShowPopup(true)
}
}, [])


useEffect(() => {
if (executionCount < 5) {
if(resourceDistributionStrategyCode === "MIXED"){
Expand Down Expand Up @@ -136,6 +147,39 @@ const AssumptionsForm = ({ onSelect, ...props }) => {
</LabelFieldPair>
</Card>
)}

{showPopup && <PopUp
className={"boundaries-pop-module"}
type={"alert"}
alertHeading={t("MP_WARNING_ASSUMPTIONS_FORM")}
alertMessage={t("MP_ASSUMPTIONS_INVALIDATION_MESSAGE")}
// heading={t("MP_ASSUMTI")}
// children={[
// <div>
// <CardText style={{ margin: 0 }}>{t("ES_CAMPAIGN_UPDATE_TYPE_MODAL_TEXT") + " "}</CardText>
// </div>,
// ]}
onOverlayClick={() => {
setShowPopup(false);
}}
onClose={() => {
setShowPopup(false);
}}
footerChildren={[
<Button
className={"campaign-type-alert-button"}
type={"button"}
size={"large"}
variation={"secondary"}
label={t("MP_ACK")}
onClick={() => {
setShowPopup(false);
// setCanUpdate(true);
}}
/>
]}
// sortFooterChildren={true}
></PopUp>}
</Card>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect, Fragment, createContext, useContext } from 'react';
import Hypothesis from './Hypothesis';
import { Stepper, TextBlock, ActionBar, Button, Card, Toast } from '@egovernments/digit-ui-components';
import { Stepper, TextBlock, ActionBar, Button, Card, Toast, Loader } from '@egovernments/digit-ui-components';
import { useTranslation } from "react-i18next";
import { useMyContext } from "../utils/context";
import { concat } from 'lodash';
Expand Down Expand Up @@ -29,11 +29,24 @@ const HypothesisWrapper = ({ onSelect, props: customProps }) => {
const [showToast, setShowToast] = useState(null);
const [deletedAssumptions, setDeletedAssumptions] = useState([]);
const [executionCount, setExecutionCount] = useState(0);

const tenantId = Digit.ULBService.getCurrentTenantId();

const { campaignId, microplanId, key, ...queryParams } = Digit.Hooks.useQueryParams();

//fetching existing plan object
const { isLoading: isLoadingPlanObject, data: planObject, error: errorPlan, refetch: refetchPlan } = Digit.Hooks.microplanv1.useSearchPlanConfig(
{
PlanConfigurationSearchCriteria: {
tenantId,
id: microplanId,
},
},
{
enabled: microplanId ? true : false,
// queryKey: currentKey,
}
);


const moveToPreviousStep = () => {
if(internalKey >1){
setInternalKey((prevKey) => prevKey - 1);
Expand Down Expand Up @@ -95,9 +108,9 @@ const HypothesisWrapper = ({ onSelect, props: customProps }) => {
}

// Move to the next step if validation passes
if (internalKey < assumptionCategories.length) {
setInternalKey((prevKey) => prevKey + 1); // Update key in URL
}
// if (internalKey < assumptionCategories.length) {
// setInternalKey((prevKey) => prevKey + 1); // Update key in URL
// }

//after everything is done make an api call and assume it will be successfull(let user go to next screen)
// API CALL
Expand All @@ -109,6 +122,18 @@ const HypothesisWrapper = ({ onSelect, props: customProps }) => {
name:"SUB_HYPOTHESIS"
},
assumptionsToUpdate
},{
onSuccess: (data) => {
if (internalKey < assumptionCategories.length) {
setInternalKey((prevKey) => prevKey + 1); // Update key in URL
}
refetchPlan();
},
onError: (error, variables) => {
console.error(error)

// setShowToast(({ key: "error", label: error?.message ? error.message : t("FAILED_TO_UPDATE_RESOURCE") }))
},
})


Expand Down Expand Up @@ -263,6 +288,10 @@ const HypothesisWrapper = ({ onSelect, props: customProps }) => {



if(isLoadingPlanObject){
return <Loader />
}

return (
<Fragment>
<AssumptionContext.Provider value={{ assumptionValues, handleAssumptionChange, setAssumptionValues, deletedAssumptions,setDeletedAssumptions }}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from "lodash"
const requestBodyGenerator = () => { };

//checking for duplicates
Expand Down Expand Up @@ -149,7 +150,7 @@ const createUpdatePlanProject = async (req) => {
try {
//later this object must have an invalidation config which can be used to invalidate data such as files uploaded,assumptions,formulas etc...

const { totalFormData, state, setShowToast, setCurrentKey, setCurrentStep, config, campaignObject, planObject } = req;
const { totalFormData, state, setShowToast, setCurrentKey, setCurrentStep, config, campaignObject, planObject,invalidateConfig } = req;
const { microplanId, campaignId } = Digit.Hooks.useQueryParams();
const tenantId = Digit.ULBService.getCurrentTenantId()
//now basically we need to decide from which screen this hook was triggered and take action accordingly
Expand Down Expand Up @@ -212,11 +213,49 @@ const createUpdatePlanProject = async (req) => {
}

case "ASSUMPTIONS_FORM":
// here we have to invalidate the existing assumptions in update call if there is a change in assumptionsForm
// check whether the currentAssumptionsForm is equal to prev assumptionsForm (if so then skip this update call)


if(_.isEqual(planObject?.additionalDetails?.assumptionsForm,totalFormData?.ASSUMPTIONS_FORM?.
assumptionsForm
)){
setCurrentKey((prev) => prev + 1);
setCurrentStep((prev) => prev + 1);
return {
triggeredFrom,
};
}
//otherwise update with invalidating assumptions and formula(operations)
const invalidatedAssumptions = planObject.assumptions.length>0 ? planObject.assumptions.map(row =>{
return {
...row,
active:false
}
}) : []
const invalidatedOperations = planObject.operations.length>0 ? planObject.assumptions.map(row =>{
return {
...row,
active:false
}
}) : []
const updatedPlanObjAssumptionsForm = {
...planObject,
assumptions:invalidatedAssumptions,
operations:invalidatedOperations,
additionalDetails:{
...planObject?.additionalDetails,
assumptionsForm:totalFormData.ASSUMPTIONS_FORM.assumptionsForm
assumptionsForm:totalFormData.ASSUMPTIONS_FORM.assumptionsForm,
campaignType: totalFormData.CAMPAIGN_DETAILS.campaignDetails.campaignType.code,

DistributionProcess:totalFormData.ASSUMPTIONS_FORM.assumptionsForm.selectedDistributionProcess ? totalFormData.ASSUMPTIONS_FORM.assumptionsForm.selectedDistributionProcess.code : totalFormData.CAMPAIGN_DETAILS.campaignDetails.distributionStrat.resourceDistributionStrategyCode,

RegistrationProcess:totalFormData.ASSUMPTIONS_FORM.assumptionsForm.selectedRegistrationProcess?totalFormData.ASSUMPTIONS_FORM.assumptionsForm.selectedRegistrationProcess.code :totalFormData.CAMPAIGN_DETAILS.campaignDetails.distributionStrat.resourceDistributionStrategyCode,

resourceDistributionStrategyCode: totalFormData.CAMPAIGN_DETAILS.campaignDetails.distributionStrat.resourceDistributionStrategyCode,

isRegistrationAndDistributionHappeningTogetherOrSeparately: totalFormData.CAMPAIGN_DETAILS.campaignDetails.distributionStrat.resourceDistributionStrategyCode==="MIXED" ?"SEPARATELY": totalFormData.ASSUMPTIONS_FORM.assumptionsForm.selectedRegistrationDistributionMode?.code,

}
}
const planResAssumptionsForm = await updatePlan(updatedPlanObjAssumptionsForm);
Expand Down Expand Up @@ -300,6 +339,7 @@ const createUpdatePlanProject = async (req) => {
}

await updatePlan(upatedPlanObjSubHypothesis);
return;
case "UPLOADDATA":
setCurrentKey((prev) => prev + 1);
setCurrentStep((prev) => prev + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ const App = ({ path, stateCode, userType, tenants,BOUNDARY_HIERARCHY_TYPE, hiera
{ name: "MicroplanNamingConvention" },
{ name: "MicroplanNamingRegex" },
{ name: "ResourceDistributionStrategy"},
{name: "rolesForMicroplan"},
{ name: "rolesForMicroplan"},
{ name: "HypothesisAssumptions"},
{ name: "RuleConfigureOutput" },
{ name: "AutoFilledRuleConfigurations" },
{ name: "RuleConfigureOperators" },
{ name: "RegistrationAndDistributionHappeningTogetherOrSeparately"}


{ name: "RegistrationAndDistributionHappeningTogetherOrSeparately"},
{ name: "hierarchyConfig"}
],
{
cacheTime:Infinity,
Expand All @@ -80,39 +79,39 @@ const App = ({ path, stateCode, userType, tenants,BOUNDARY_HIERARCHY_TYPE, hiera
{
cacheTime:Infinity,
select:(data) => {
dispatch({
type: "MASTER_DATA",
state: {
hierarchyConfig:[
{
"hierarchy": "Workbench",
"lowestHierarchy": "Post Administrative",
"splitBoundariesOn" : "District",
"isActive": false
},
{
"hierarchy": "ADMIN",
"lowestHierarchy": "Post Administrative",
"splitBoundariesOn" : "District",
"isActive": false
},
{
"hierarchy": "Health",
"lowestHierarchy": "Post Administrative",
"splitBoundariesOn" : "District",
"isActive": false
},
{
"hierarchy": "MICROPLAN",
"lowestHierarchy": "Village",
"splitBoundariesOn" : "District",
"isActive": true
}
],
...data?.["HCM-ADMIN-CONSOLE"],
},
// dispatch({
// type: "MASTER_DATA",
// state: {
// hierarchyConfig:[
// {
// "hierarchy": "Workbench",
// "lowestHierarchy": "Post Administrative",
// "splitBoundariesOn" : "District",
// "isActive": false
// },
// {
// "hierarchy": "ADMIN",
// "lowestHierarchy": "Post Administrative",
// "splitBoundariesOn" : "District",
// "isActive": false
// },
// {
// "hierarchy": "Health",
// "lowestHierarchy": "Post Administrative",
// "splitBoundariesOn" : "District",
// "isActive": false
// },
// {
// "hierarchy": "MICROPLAN",
// "lowestHierarchy": "Village",
// "splitBoundariesOn" : "District",
// "isActive": true
// }
// ],
// ...data?.["HCM-ADMIN-CONSOLE"],
// },

});
// });
}
},
{schemaCode:"ADDITIONAL_MASTER_DATA"} //mdmsv2
Expand Down

0 comments on commit 8b0f9d2

Please sign in to comment.