Skip to content

Commit

Permalink
Microplan upload (#1489)
Browse files Browse the repository at this point in the history
* Some changes for upload component for microplan

* Some pr fixes
  • Loading branch information
ashish-egov authored Oct 10, 2024
1 parent f2212f2 commit 4f958b5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { Wrapper } from "./components/SelectingBoundaryComponent";
import SelectingBoundariesDuplicate from "./components/SelectingBoundariesDuplicate";
import CampaignUpdateSummary from "./components/CampaignUpdateSummary";
import XlsPreview from "./components/XlsPreview";
import BulkUpload from "./components/BulkUpload";
import BoundarySummary from "./components/BoundarySummary";

import MultiSelectDropdown from "./components/MultiSelectDropdown";
Expand Down Expand Up @@ -132,6 +133,7 @@ const componentsToRegister = {
UpdateBoundary,
UpdateBoundaryWrapper,
SelectingBoundariesDuplicate,
BulkUpload,
CampaignUpdateSummary,
XlsPreview,
MultiSelectDropdownBoundary:MultiSelectDropdown,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const useResourceData = async (data, hierarchyType, type, tenantId, id ,
Error.isError = true;
return Error;
} else {
Error = errorMessage;
Error = {error:String(error.message)};
Error.isError = true;
return Error;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Header, LoaderWithGap } from "@egovernments/digit-ui-react-components";
import { Header, LoaderWithGap} from "@egovernments/digit-ui-react-components";
import React, { useRef, useState, useEffect, Fragment, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { Card, Modal, CardText } from "@egovernments/digit-ui-react-components";
import BulkUpload from "./../../../campaign-manager/src/components/BulkUpload";
import Ajv from "ajv";
import XLSX from "xlsx";
import { InfoCard, PopUp, Toast, Button, DownloadIcon, Stepper, TextBlock } from "@egovernments/digit-ui-components";
Expand All @@ -19,11 +18,14 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
const { t } = useTranslation();
const tenantId = Digit.ULBService.getCurrentTenantId();
const [uploadedFile, setUploadedFile] = useState([]);
const [processedFile, setProcessedFile] = useState([]);
const params = Digit.SessionStorage.get("HCM_CAMPAIGN_MANAGER_UPLOAD_ID");
const [showInfoCard, setShowInfoCard] = useState(false);
const [errorsType, setErrorsType] = useState({});
const [schema, setSchema] = useState(null);
const [showToast, setShowToast] = useState(null);
const [sheetErrors, setSheetErrors] = useState(0);
// TODO : Use Digit for query params
const searchParams = new URLSearchParams(location.search);
const [key, setKey] = useState(() => {
const keyParam = searchParams.get("key");
Expand All @@ -38,8 +40,9 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
const [fileName, setFileName] = useState(null);
const [downloadError, setDownloadError] = useState(false);
const [resourceId, setResourceId] = useState(null);
const id = searchParams.get("campaignId") || null;

const [showPreview, setShowPreview] = useState(false);
// TODO : Remove hard coded id
const id = searchParams.get("campaignId") || "274f60e0-f6d8-4bf9-b4da-a714a9046e93";
const { data: Schemas, isLoading: isThisLoading } = Digit.Hooks.useCustomMDMS(
tenantId,
"HCM-ADMIN-CONSOLE",
Expand All @@ -63,6 +66,8 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
const campaignType = totalData?.CAMPAIGN_DETAILS?.campaignDetails?.campaignType?.code
const [convertedSchema, setConvertedSchema] = useState({});
const [loader, setLoader] = useState(false);
const XlsPreview = Digit.ComponentRegistryService.getComponent("XlsPreview");
const BulkUpload = Digit.ComponentRegistryService.getComponent("BulkUpload");
const baseKey = 4;

useEffect(() => {
Expand Down Expand Up @@ -310,6 +315,8 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
setIsValidation(false);
setDownloadError(false);
setIsError(false);
setSheetErrors(0);
setShowPreview(false);
setIsSuccess(props?.props?.sessionData?.HCM_CAMPAIGN_UPLOAD_BOUNDARY_DATA?.uploadBoundary?.isSuccess || null);
setShowPopUp(!props?.props?.sessionData?.HCM_CAMPAIGN_UPLOAD_BOUNDARY_DATA?.uploadBoundary?.uploadedFile.length);
break;
Expand All @@ -319,6 +326,8 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
setIsValidation(false);
setDownloadError(false);
setIsError(false);
setSheetErrors(0);
setShowPreview(false);
setIsSuccess(props?.props?.sessionData?.HCM_CAMPAIGN_UPLOAD_FACILITY_DATA?.uploadFacility?.isSuccess || null);
setShowPopUp(!props?.props?.sessionData?.HCM_CAMPAIGN_UPLOAD_FACILITY_DATA?.uploadFacility?.uploadedFile.length);
break;
Expand All @@ -328,6 +337,8 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
setIsValidation(false);
setDownloadError(false);
setIsError(false);
setSheetErrors(0);
setShowPreview(false);
setIsSuccess(props?.props?.sessionData?.HCM_CAMPAIGN_UPLOAD_USER_DATA?.uploadUser?.isSuccess || null);
setShowPopUp(!props?.props?.sessionData?.HCM_CAMPAIGN_UPLOAD_USER_DATA?.uploadUser?.uploadedFile.length);
break;
Expand Down Expand Up @@ -447,6 +458,7 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
return true;
};


const validateTargetData = (data, sheetName, targetError) => {
const ajv = new Ajv({ strict: false }); // Initialize Ajv
let validate = ajv.compile(translatedSchema[type]);
Expand Down Expand Up @@ -737,6 +749,9 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
const onFileDelete = (file, index) => {
setUploadedFile((prev) => prev.filter((i) => i.id !== file.id));
setIsError(false);
setProcessedFile([]);
setSheetErrors(0);
setShowPreview(false);
setIsSuccess(false);
setIsValidation(false);
setApiError(null);
Expand All @@ -755,7 +770,7 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
if (!errorsType[type] && uploadedFile?.length > 0 && !isSuccess) {
// setShowToast({ key: "info", label: t("HCM_VALIDATION_IN_PROGRESS") });
setIsValidation(true);
setIsError(true);
// setIsError(true);
setLoader(true);

try {
Expand Down Expand Up @@ -832,6 +847,7 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
setLoader(false);
setIsValidation(false);
// setShowToast({ key: "error", label: t("HCM_VALIDATION_FAILED"), transitionTime: 5000000 });
setSheetErrors(temp?.additionalDetails?.sheetErrors?.length || 0);
const processedFileStore = temp?.processedFilestoreId;
if (!processedFileStore) {
setShowToast({ key: "error", label: t("HCM_VALIDATION_FAILED"), transitionTime: 5000000 });
Expand All @@ -855,18 +871,22 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
return {
...i,
filestoreId: id,
filename: fileName,
filename: t("HCM_MICROPLAN_PROCESSED_FILE"),
type: fileType,
};
})
.map(({ id, ...rest }) => rest);
// onFileDelete(uploadedFile);
setUploadedFile(fileData);
setShowToast({ key: "warning", label: t("HCM_CHECK_FILE_AGAIN"), transitionTime: 5000000 });
// setUploadedFile(fileData);
setProcessedFile(fileData);
setReadMeInfo(readMeInfoNew);
// setShowToast({ key: "warning", label: t("HCM_CHECK_FILE_AGAIN"), transitionTime: 5000000 });
setIsError(true);
}
}
} catch (error) { }
} catch (error) {
console.log(error);
}
}
};

Expand Down Expand Up @@ -925,7 +945,7 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
if (fileData && fileData?.[0]?.url) {
setDownloadError(false);
if (fileData?.[0]?.id) {
downloadExcelWithCustomName({ fileStoreId: fileData?.[0]?.id, customName: fileData?.[0]?.filename });
Digit.Utils.campaign.downloadExcelWithCustomName({ fileStoreId: fileData?.[0]?.id, customName: fileData?.[0]?.filename });
}
} else {
setDownloadError(true);
Expand Down Expand Up @@ -1037,21 +1057,47 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
populators={{
name: "infocard",
}}
variant="default"
variant= {sheetErrors ? "error" : "default"}
style={{ margin: "0rem", maxWidth: "100%" }}
additionalElements={readMeInfo[type]?.map((info, index) => (
<div key={index} style={{ display: "flex", flexDirection: "column" }}>
<h2>{info?.header}</h2>
<ul style={{ paddingLeft: 0 }}>
{info?.descriptions.map((desc, i) => (
<li key={i} className="info-points">
{desc.isBold ? <h2>{desc.text}</h2> : <p>{desc.text}</p>}
</li>
))}
</ul>
</div>
))}
label={"Info"}
additionalElements={
sheetErrors ? (
[<Button
type="button"
size="large"
variation="default"
label={t("HCM_VIEW_ERROR")}
onClick={() => setShowPreview(true)}
style={{
marginTop: "1rem",
backgroundColor: "#B91900"
}}
textStyles={{
color: "#FFFFFF"
}}
/>]
) : (
readMeInfo[type]?.map((info, index) => (
<div key={index} style={{ display: "flex", flexDirection: "column" }}>
<ul style={{ paddingLeft: 0 }}>
{info?.descriptions.map((desc, i) => (
<li key={i} className="info-points">
{desc.isBold ? (
<h2>{`Step ${i + 1}: ${desc.text}`}</h2>
) : (
<p>{`Step ${i + 1}: ${desc.text}`}</p>
)}
</li>
))}
</ul>
</div>
))
)
}
label={
sheetErrors
? `${sheetErrors} ${sheetErrors === 1 ? t("HCM_MICROPLAN_SINGLE_ERROR") : t("HCM_MICROPLAN_PLURAL_ERRORS")} ${t("HCM_MICROPLAN_ERRORS_FOUND")}`
: t("HCM_MICROPLAN_DATA_UPLOAD_GUIDELINES")
}
/>
</div>
{showPopUp && (
Expand Down Expand Up @@ -1114,6 +1160,7 @@ const UploadDataCustom = React.memo(({ formData, onSelect, ...props }) => {
onClose={closeToast}
/>
)}
{showPreview && <XlsPreview file={processedFile?.[0]} onDownload={() => onFileDownload(processedFile?.[0])} onBack={() => setShowPreview(false)} />}
</div>
</>
);
Expand Down

0 comments on commit 4f958b5

Please sign in to comment.