diff --git a/public/locales/en/common.json b/public/locales/en/common.json index af778485..59538c81 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -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", diff --git a/src/components/AddBlockModal.tsx b/src/components/AddBlockModal.tsx index 30fbeb71..47f00bb2 100644 --- a/src/components/AddBlockModal.tsx +++ b/src/components/AddBlockModal.tsx @@ -59,11 +59,24 @@ export const AddBlockModal: React.FC = ({ const [districtsOptionRead, setDistrictsOptionRead] = useState([]); const [districtCodeArr, setDistrictCodeArr] = useState([]); const [districtNameArr, setDistrictNameArr] = useState([]); - const [cohortIdAddNewDropdown, setCohortIdAddNewDropdown] = useState(""); + const [stateCode, setStateCode] = useState(""); + const [stateName, setStateName] = useState(""); 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 || "", @@ -88,6 +101,7 @@ export const AddBlockModal: React.FC = ({ const fetchDistricts = async () => { try { const data = await getDistrictsForState({ + controllingfieldfk: stateCode || "", fieldName: "districts", }); @@ -119,11 +133,9 @@ export const AddBlockModal: React.FC = ({ limit: 0, offset: 0, filters: { - name: "", - states: "", + states: stateCode, type: "DISTRICT", }, - sort: ["name", "asc"], }; const response = await getCohortList(reqParams); @@ -185,7 +197,7 @@ export const AddBlockModal: React.FC = ({ 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"); @@ -303,10 +315,10 @@ export const AddBlockModal: React.FC = ({ MenuProps={{ PaperProps: { sx: { - maxHeight: 400, + maxHeight: 400, }, }, - }} + }} fullWidth displayEmpty variant="outlined" diff --git a/src/components/HeaderComponent.tsx b/src/components/HeaderComponent.tsx index 5efded54..fb6e0816 100644 --- a/src/components/HeaderComponent.tsx +++ b/src/components/HeaderComponent.tsx @@ -71,7 +71,7 @@ const HeaderComponent = ({ selectedCenter, handleCenterChange, statusValue, - + shouldFetchDistricts=true, setStatusValue, }: any) => { const { t } = useTranslation(); @@ -226,8 +226,10 @@ const HeaderComponent = ({ } }; - fetchData(); - }, []); + if (shouldFetchDistricts) { + fetchData(); + } + }, [shouldFetchDistricts]); const handleChange = (event: React.SyntheticEvent, newValue: any) => { console.log(newValue); setStatusValue(newValue); diff --git a/src/pages/block.tsx b/src/pages/block.tsx index 6bcf7bb3..18f3c146 100644 --- a/src/pages/block.tsx +++ b/src/pages/block.tsx @@ -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", }); @@ -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); @@ -466,10 +467,9 @@ const Block: React.FC = () => { const handleDistrictChange = async (event: SelectChangeEvent) => { const selectedDistrict = event.target.value; setSelectedDistrict(selectedDistrict); - setShowAllBlocks("") + setShowAllBlocks(""); console.log("selectedDistrict", selectedDistrict); - const selectedDistrictData = districtData.find( (district) => district.value === selectedDistrict ); @@ -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); }; @@ -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") } @@ -796,6 +797,7 @@ const Block: React.FC = () => { selectedSort={selectedSort} handleSortChange={handleSortChange} showSort={true} + shouldFetchDistricts={false} > {loading ? ( { }, }} > - {t("COMMON.ALL")} + {/* {t("COMMON.ALL")} */} {districtData.map((districtDetail) => ( { const [districtValueForDelete, setDistrictValueForDelete] = useState(""); const [countOfBlocks, setCountOfBlocks] = useState(0); const [cohortIdofState, setCohortIdofState] = useState(""); + const [previousSearch,setPreviouseSearch] = useState({}); + // 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 () => { @@ -139,7 +160,7 @@ const District: React.FC = () => { useEffect(() => { fetchDistricts(); - }, [stateCode]); + }, [stateCode, stateValue]); // get cohort id of state const getStatecohorts = async () => { @@ -205,6 +226,7 @@ const District: React.FC = () => { updatedBy: any; }) => { const transformedName = districtDetail.name; + console.log(districtsOptionRead); const matchingDistrict = districtsOptionRead.find( (district: { label: string }) => @@ -233,7 +255,7 @@ 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([]); @@ -241,10 +263,10 @@ const District: React.FC = () => { } }; useEffect(() => { - if (stateCode) { + if (stateCode && stateValue) { getFilteredCohortData(); } - }, [searchKeyword, pageLimit, pageOffset, stateCode, sortBy]); + }, [searchKeyword, pageLimit, pageOffset, stateCode, sortBy, stateValue]); const getBlockDataCohort = async () => { try { @@ -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 { @@ -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); diff --git a/src/pages/state.tsx b/src/pages/state.tsx index 32832c47..da0b28f7 100644 --- a/src/pages/state.tsx +++ b/src/pages/state.tsx @@ -1,4 +1,3 @@ - import React, { useState, useEffect } from "react"; import KaTableComponent from "../components/KaTableComponent"; import { serverSideTranslations } from "next-i18next/serverSideTranslations"; @@ -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); @@ -289,6 +296,7 @@ const State: React.FC = () => { handleSortChange={handleSortChange} showAddNew={false} showSort={true} + shouldFetchDistricts={false} selectedSort={selectedSort} showFilter={false} handleSearch={handleSearch} @@ -339,7 +347,3 @@ export const getServerSideProps = async ({ locale }: { locale: string }) => { }, }; }; - - - -