Skip to content

Commit

Permalink
Merge pull request #184 from rahulg1254/admin-master
Browse files Browse the repository at this point in the history
Task #226259 feat: Resolved UT issues
  • Loading branch information
itsvick authored Sep 3, 2024
2 parents 7bcbd8a + 37b2888 commit 841a8d4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 31 deletions.
2 changes: 1 addition & 1 deletion public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"CANCEL": "Cancel",
"ARE_YOU_SURE_DELETE": "There are {{block}} Active blocks in this district you cannot delete it.",
"NO_ACTIVE_BLOCKS_DELETE": "There are no active blocks in this district. Do you want to delete it?",
"ARE_YOU_SURE_DELETE_BLOCK": "There are {{centers}} Active centers in this block you cannot delete it",
"ARE_YOU_SURE_DELETE_BLOCK": "There are {{centerCount}} Active centers in this block you cannot delete it",
"NO_ACTIVE_CENTERS_DELETE": "There are no active centers in this block. Do you want to delete it?",
"CONFIRM_DELETE": "Confirm Delete",
"ADD_STATE": "Add New State",
Expand Down
26 changes: 19 additions & 7 deletions src/components/AddBlockModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,24 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({
const [districtsOptionRead, setDistrictsOptionRead] = useState<any>([]);
const [districtCodeArr, setDistrictCodeArr] = useState<any>([]);
const [districtNameArr, setDistrictNameArr] = useState<any>([]);

const [cohortIdAddNewDropdown, setCohortIdAddNewDropdown] = useState<any>("");
const [stateCode, setStateCode] = useState<any>("");
const [stateName, setStateName] = useState<any>("");

const { t } = useTranslation();

useEffect(() => {
const storedUserData = JSON.parse(
localStorage.getItem("adminInfo") || "{}"
);
const stateCodes = storedUserData?.customFields[0]?.code;
const stateNames = storedUserData?.customFields[0]?.value;
setStateCode(stateCodes);
setStateName(stateNames);
}, [open]);

console.log("state", stateCode, stateName);

useEffect(() => {
setFormData({
name: initialValues.name || "",
Expand All @@ -88,6 +101,7 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({
const fetchDistricts = async () => {
try {
const data = await getDistrictsForState({
controllingfieldfk: stateCode || "",
fieldName: "districts",
});

Expand Down Expand Up @@ -119,11 +133,9 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({
limit: 0,
offset: 0,
filters: {
name: "",
states: "",
states: stateCode,
type: "DISTRICT",
},
sort: ["name", "asc"],
};

const response = await getCohortList(reqParams);
Expand Down Expand Up @@ -185,7 +197,7 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({
value: string,
requiredMessage: string
) => {
if (!value) return null;
if (!value) return null;

if (field !== "controllingField" && !/^[a-zA-Z\s]+$/.test(value)) {
return t("COMMON.INVALID_TEXT");
Expand Down Expand Up @@ -303,10 +315,10 @@ export const AddBlockModal: React.FC<AddBlockModalProps> = ({
MenuProps={{
PaperProps: {
sx: {
maxHeight: 400,
maxHeight: 400,
},
},
}}
}}
fullWidth
displayEmpty
variant="outlined"
Expand Down
8 changes: 5 additions & 3 deletions src/components/HeaderComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const HeaderComponent = ({
selectedCenter,
handleCenterChange,
statusValue,

shouldFetchDistricts=true,
setStatusValue,
}: any) => {
const { t } = useTranslation();
Expand Down Expand Up @@ -226,8 +226,10 @@ const HeaderComponent = ({
}
};

fetchData();
}, []);
if (shouldFetchDistricts) {
fetchData();
}
}, [shouldFetchDistricts]);
const handleChange = (event: React.SyntheticEvent, newValue: any) => {
console.log(newValue);
setStatusValue(newValue);
Expand Down
16 changes: 9 additions & 7 deletions src/pages/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ const Block: React.FC = () => {
fetchUserDetail();
}, []);

console.log("stateCode", stateCode);
const fetchDistricts = async () => {
try {
const data = await getDistrictsForState({
controllingfieldfk: stateCode || "",
controllingfieldfk: stateCode,
fieldName: "districts",
});

Expand Down Expand Up @@ -330,7 +331,7 @@ const Block: React.FC = () => {
console.log("Filtered Block Data:", filteredBlockData);

setBlockData(filteredBlockData);
setShowAllBlocks(filteredBlockData)
setShowAllBlocks(filteredBlockData);

const totalCount = filteredBlockData.length;
setPaginationCount(totalCount);
Expand Down Expand Up @@ -466,10 +467,9 @@ const Block: React.FC = () => {
const handleDistrictChange = async (event: SelectChangeEvent<string>) => {
const selectedDistrict = event.target.value;
setSelectedDistrict(selectedDistrict);
setShowAllBlocks("")
setShowAllBlocks("");
console.log("selectedDistrict", selectedDistrict);


const selectedDistrictData = districtData.find(
(district) => district.value === selectedDistrict
);
Expand Down Expand Up @@ -515,9 +515,10 @@ const Block: React.FC = () => {
const blockValue = rowData.value;
setBlockValueForDelete(blockValue);
};
console.log("cohortIdForDelete", cohortIdForDelete);

const handleSearch = (keyword: string) => {
setPageOffset(Numbers.ZERO);
setPageCount(Numbers.ONE);
setSearchKeyword(keyword);
};

Expand Down Expand Up @@ -776,7 +777,7 @@ const Block: React.FC = () => {
message={
countOfCenter > 0
? t("COMMON.ARE_YOU_SURE_DELETE_BLOCK", {
centers: `${countOfCenter}`,
centerCount: `${countOfCenter}`,
})
: t("COMMON.NO_ACTIVE_CENTERS_DELETE")
}
Expand All @@ -796,6 +797,7 @@ const Block: React.FC = () => {
selectedSort={selectedSort}
handleSortChange={handleSortChange}
showSort={true}
shouldFetchDistricts={false}
>
{loading ? (
<Box
Expand Down Expand Up @@ -869,7 +871,7 @@ const Block: React.FC = () => {
},
}}
>
<MenuItem value="All">{t("COMMON.ALL")}</MenuItem>
{/* <MenuItem value="All">{t("COMMON.ALL")}</MenuItem> */}
{districtData.map((districtDetail) => (
<MenuItem
key={districtDetail.value}
Expand Down
35 changes: 30 additions & 5 deletions src/pages/district.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ const District: React.FC = () => {
const [districtValueForDelete, setDistrictValueForDelete] = useState<any>("");
const [countOfBlocks, setCountOfBlocks] = useState<number>(0);
const [cohortIdofState, setCohortIdofState] = useState<any>("");
const [previousSearch,setPreviouseSearch] = useState<any>({});
// useEffect(() => {
// const fetchUserDetail = () => {
// if (typeof window !== "undefined" && window.localStorage) {
// const storedUserData = JSON.parse(
// localStorage.getItem("adminInfo") || "{}"
// );

// const stateCodes = storedUserData?.customFields?.[0].code;
// const stateNames = storedUserData?.customFields?.[0].value;
// const stateField = storedUserData?.customFields[0].fieldId;

// if (stateField) {
// setStateFieldId(stateField);
// setStateCode(stateCodes);
// setStateValue(stateNames);
// }
// }
// };
// fetchUserDetail();
// }, [stateFieldId]);

useEffect(() => {
const fetchUserDetail = async () => {
Expand Down Expand Up @@ -139,7 +160,7 @@ const District: React.FC = () => {

useEffect(() => {
fetchDistricts();
}, [stateCode]);
}, [stateCode, stateValue]);

// get cohort id of state
const getStatecohorts = async () => {
Expand Down Expand Up @@ -205,6 +226,7 @@ const District: React.FC = () => {
updatedBy: any;
}) => {
const transformedName = districtDetail.name;
console.log(districtsOptionRead);

const matchingDistrict = districtsOptionRead.find(
(district: { label: string }) =>
Expand Down Expand Up @@ -233,18 +255,18 @@ const District: React.FC = () => {
const totalCount = filteredDistrictData.length;
setPaginationCount(totalCount);
setPageCount(Math.ceil(totalCount / pageLimit));
setLoading(false)
setLoading(false);
} catch (error) {
console.error("Error fetching and filtering cohort districts", error);
setDistrictData([]);
setLoading(false);
}
};
useEffect(() => {
if (stateCode) {
if (stateCode && stateValue) {
getFilteredCohortData();
}
}, [searchKeyword, pageLimit, pageOffset, stateCode, sortBy]);
}, [searchKeyword, pageLimit, pageOffset, stateCode, sortBy, stateValue]);

const getBlockDataCohort = async () => {
try {
Expand Down Expand Up @@ -306,9 +328,12 @@ const District: React.FC = () => {
};
console.log("cohort id for delte", cohotIdForDelete);
const handleSearch = (keyword: string) => {
setPageOffset(Numbers.ZERO);
setPageCount(Numbers.ONE);
setSearchKeyword(keyword);
};


const handleConfirmDelete = async () => {
if (selectedStateForDelete) {
try {
Expand Down Expand Up @@ -428,7 +453,7 @@ const District: React.FC = () => {
const response = await createOrUpdateOption(districtFieldId, newDistrict);

if (response) {
filteredCohortOptionData()
filteredCohortOptionData();
}
} catch (error) {
console.error("Error adding district:", error);
Expand Down
20 changes: 12 additions & 8 deletions src/pages/state.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import React, { useState, useEffect } from "react";
import KaTableComponent from "../components/KaTableComponent";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
Expand Down Expand Up @@ -159,12 +158,20 @@ const State: React.FC = () => {
console.error("Error fetching and filtering cohort states", error);
}
};


useEffect(() => {
if (stateDataOption.length > 0 && stateNameArray.length > 0) {
getStatecohorts();
getStatecohorts();
}
}, [stateDataOption, stateNameArray, searchKeyword, pageLimit, pageOffset, sortBy]);

}, [
stateDataOption,
stateNameArray,
searchKeyword,
pageLimit,
pageOffset,
sortBy,
]);

const handleEdit = (rowData: StateDetail) => {
setSelectedStateForEdit(rowData);
Expand Down Expand Up @@ -289,6 +296,7 @@ const State: React.FC = () => {
handleSortChange={handleSortChange}
showAddNew={false}
showSort={true}
shouldFetchDistricts={false}
selectedSort={selectedSort}
showFilter={false}
handleSearch={handleSearch}
Expand Down Expand Up @@ -339,7 +347,3 @@ export const getServerSideProps = async ({ locale }: { locale: string }) => {
},
};
};




0 comments on commit 841a8d4

Please sign in to comment.