Skip to content

Commit

Permalink
added the checks for the update campaign flow (#1788)
Browse files Browse the repository at this point in the history
  • Loading branch information
jagankumar-egov authored Nov 11, 2024
1 parent 62b7995 commit e4e0a5d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import DeliveryDetailsSummary from "./components/DeliveryDetailsSummary";
import DataUploadSummary from "./components/DataUploadSummary";
import CampaignDetailsSummary from "./components/CampaignDetailsSummary";
import BoundaryDetailsSummary from "./components/BoundaryDetailsSummary";
import UpdateBoundary from "./pages/employee/UpdateBoundary";
import UpdateBoundary from "./pages/employee/UpdateCampaign";
import UpdateBoundaryWrapper from "./components/UpdateBoundaryWrapper";
// import SelectingBoundaryComponent from "./components/SelectingBoundaryComponent";
import { Wrapper } from "./components/SelectingBoundaryComponent";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,8 @@ export const UICustomizations = {
</Link>
</span>
);

case "CM_DRAFT_TYPE":
return value ? t("CM_UPDATE_REQUEST") : t("CM_CREATE_REQUEST");
case "CAMPAIGN_START_DATE":
return Digit.DateUtils.ConvertEpochToDate(value);
case "CAMPAIGN_END_DATE":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@ export const myCampaignConfig = {
jsonPath: "campaignName",
// additionalCustomization: true,
},
{
label: "CM_DRAFT_TYPE",
jsonPath: "parentId",
additionalCustomization: true,
},
{
label: "CAMPAIGN_TYPE",
jsonPath: "projectType",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { Toast , Stepper} from "@egovernments/digit-ui-components";
import _ from "lodash";
import { UpdateBoundaryConfig } from "../../configs/UpdateBoundaryConfig";
import { CONSOLE_MDMS_MODULENAME } from "../../Module";
import { compareIdentical, groupByTypeRemap, resourceData, updateUrlParams } from "../../utils/setupCampaignHelpers";

/**
* The `SetupCampaign` function in JavaScript handles the setup and management of campaign details,
* The `UpdateCampaign` function in JavaScript handles the Updating of campaign details,
* including form data handling, validation, and submission.
* @returns The `SetupCampaign` component is being returned. It consists of a form setup for creating
* or updating a campaign with multiple steps like campaign details, delivery details, boundary
* @returns The `UpdateCampaign` component is being returned. It consists of a form setup for Updating
* a Created campaign with multiple steps like boundary
* details, targets, facility details, user details, and review details. The form data is validated at
* each step, and the user can navigate between steps using a stepper component. The form submission
* triggers API calls to create or update the campaign
Expand All @@ -20,54 +21,15 @@ import { CONSOLE_MDMS_MODULENAME } from "../../Module";



function groupByTypeRemap(data) {
if (!data) return null;

const result = {};

data.forEach((item) => {
const type = item?.type;
const boundaryType = item?.type;
const parentCode = item?.parent !== undefined ? item.parent : null;

if (!result[type]) {
result[type] = {};
}

if (!result[type][parentCode]) {
result[type][parentCode] = {
parentCode,
boundaryTypeData: {
TenantBoundary: [
{
boundary: [],
},
],
},
};
}

const targetBoundaryArray = result[type][parentCode].boundaryTypeData.TenantBoundary[0].boundary;
targetBoundaryArray.push({ ...item, boundaryType });
});
}
// Example usage:
// updateUrlParams({ id: 'sdjkhsdjkhdshfsdjkh', anotherParam: 'value' });
function updateUrlParams(params) {
const url = new URL(window.location.href);
Object.entries(params).forEach(([key, value]) => {
url.searchParams.set(key, value);
});
window.history.replaceState({}, "", url);
}

const UpdateBoundary = ({hierarchyData }) => {
const UpdateCampaign = ({hierarchyData }) => {
const tenantId = Digit.ULBService.getCurrentTenantId();
const { t } = useTranslation();
const history = useHistory();
const [currentStep, setCurrentStep] = useState(0);
const [totalFormData, setTotalFormData] = useState({});
const [isSubmitting, setIsSubmitting] = useState(false);
const [isDataCreating, setIsDataCreating] = useState(false);

const [campaignConfig, setCampaignConfig] = useState(UpdateBoundaryConfig(totalFormData, null, isSubmitting ));
const [shouldUpdate, setShouldUpdate] = useState(false);
const [params, setParams, clearParams] = Digit.Hooks.useSessionStorage("HCM_CAMPAIGN_UPDATE_FORM_DATA", {});
Expand All @@ -90,7 +52,6 @@ const UpdateBoundary = ({hierarchyData }) => {
const keyParam = searchParams.get("key");
return keyParam ? parseInt(keyParam) : 1;
});
const [displayMenu, setDisplayMenu] = useState(null);
const [fetchBoundary, setFetchBoundary] = useState(() => Boolean(searchParams.get("fetchBoundary")));
const [fetchUpload, setFetchUpload] = useState(false);
const [active, setActive] = useState(0);
Expand Down Expand Up @@ -272,14 +233,8 @@ const UpdateBoundary = ({hierarchyData }) => {

}

function resourceData(facilityData, boundaryData, userData) {
const resources = [facilityData, boundaryData, userData].filter((data) => data !== null && data !== undefined);
return resources;
}

const compareIdentical = (draftData, payload) => {
return _.isEqual(draftData, payload);
};

//API CALL
useEffect(async () => {
if (shouldUpdate === true) {
Expand Down Expand Up @@ -319,6 +274,8 @@ const UpdateBoundary = ({hierarchyData }) => {
// }
payloadData.deliveryRules = CampaignData?.CampaignDetails?.[0]?.deliveryRules;
if (compareIdentical(draftData, payloadData) === false) {
setIsDataCreating(true);

await updateCampaign(payloadData, {
onError: (error, variables) => {
console.log(error);
Expand All @@ -338,6 +295,11 @@ const UpdateBoundary = ({hierarchyData }) => {
);
Digit.SessionStorage.del("HCM_CAMPAIGN_UPDATE_FORM_DATA");
},
onSettled: () => {
// This will always run after the mutation completes
setIsDataCreating(false);
// Final function logic here
},
});
}
};
Expand Down Expand Up @@ -376,6 +338,7 @@ const UpdateBoundary = ({hierarchyData }) => {
// payloadData.deliveryRules = temp;
// }
payloadData.deliveryRules = CampaignData?.CampaignDetails?.[0]?.deliveryRules;
setIsDataCreating(true);

await mutate(payloadData, {
onError: (error, variables) => {
Expand All @@ -391,6 +354,11 @@ const UpdateBoundary = ({hierarchyData }) => {
setCurrentKey(currentKey + 1);
}
},
onSettled: () => {
// This will always run after the mutation completes
setIsDataCreating(false);
// Final function logic here
},
});
};

Expand Down Expand Up @@ -849,20 +817,6 @@ const UpdateBoundary = ({hierarchyData }) => {
return <Loader />;
}

function onActionSelect(action) {
setDisplayMenu(false);
switch (action) {
case "UPDATE_DATES":
history.push(`/${window.contextPath}/employee/campaign/update-dates-boundary?id=${id}`, {
name: draftData?.campaignName,
projectId: draftData?.projectId,
data: draftData,
});
break;
default:
break;
}
}

return (
<React.Fragment>
Expand All @@ -882,6 +836,8 @@ const UpdateBoundary = ({hierarchyData }) => {
};
})}
onSubmit={onSubmit}
isDisabled={isDataCreating}

showSecondaryLabel={currentKey > 1 ? true : false}
secondaryLabel={isChangeDates === "true" && currentKey == 6 ? t("HCM_BACK") : noAction === "false" ? null : t("HCM_BACK")}
actionClassName={"actionBarClass"}
Expand All @@ -903,5 +859,5 @@ const UpdateBoundary = ({hierarchyData }) => {
);
};

export default UpdateBoundary;
export default UpdateCampaign;

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SelectingBoundaries from "../../components/SelectingBoundaries";
import ConfigureApp from "./ConfigureApp";
import { CreateChecklist} from "./CreateChecklist";
import SearchChecklist from "./SearchChecklist";
import UpdateBoundary from "./UpdateBoundary";
import UpdateCampaign from "./UpdateCampaign";
import GeoPode from "./GeoPode";
import ViewBoundary from "./ViewBoundary";
import ViewHierarchy from "./ViewHierarchy";
Expand Down Expand Up @@ -147,7 +147,7 @@ const App = ({ path, BOUNDARY_HIERARCHY_TYPE, hierarchyData }) => {
<PrivateRoute path={`${path}/boundary/geopode`} component={()=> <GeoPode />} />
<PrivateRoute path={`${path}/boundary/view-all-hierarchy`} component={()=> <ViewBoundary />} />
<PrivateRoute path={`${path}/boundary/view-hierarchy`} component={()=> <ViewHierarchy />} />
<PrivateRoute path={`${path}/update-campaign`} component={() => <UpdateBoundary />} />
<PrivateRoute path={`${path}/update-campaign`} component={() => <UpdateCampaign />} />
</AppContainer>
</Switch>
</React.Fragment>
Expand Down

0 comments on commit e4e0a5d

Please sign in to comment.