diff --git a/src/pages/district.tsx b/src/pages/district.tsx index bd512133..6d55c217 100644 --- a/src/pages/district.tsx +++ b/src/pages/district.tsx @@ -30,6 +30,7 @@ import { } from "@/services/CohortService/cohortService"; import useStore from "@/store/store"; import { getUserDetailsInfo } from "@/services/UserList"; +import { getCohortList as getMyCohorts } from "@/services/GetCohortList"; type StateDetail = { stateCode: string | undefined; @@ -85,6 +86,7 @@ const District: React.FC = () => { const [cohortIdForEdit, setCohortIdForEdit] = useState(); const [districtValueForDelete, setDistrictValueForDelete] = useState(""); const [countOfBlocks, setCountOfBlocks] = useState(0); + const [cohortIdofState,setCohortIdofState] = useState(""); useEffect(() => { const fetchUserDetail = async () => { @@ -140,6 +142,39 @@ const District: React.FC = () => { fetchDistricts(); }, [stateCode]); + const getStatecohorts = async () => { + let userId: any; + try { + if (typeof window !== "undefined" && window.localStorage) { + userId = localStorage.getItem(Storage.USER_ID); + } + + const response: any = await getMyCohorts(userId); + const cohortData = response?.result?.cohortData; + if (Array.isArray(cohortData)) { + const stateCohort = cohortData.find(cohort => cohort.type === "STATE"); + + if (stateCohort) { + const cohortIdOfState = stateCohort.cohortId; + setCohortIdofState(cohortIdOfState); + } else { + console.error("No STATE type cohort found"); + } + } else { + console.error("cohortData is not an array or is undefined"); + } + + } catch (error) { + console.error("Error fetching and filtering cohort districts", error); + setLoading(false); + } + }; + + useEffect(() => { + getStatecohorts(); + }, []); + + const getFilteredCohortData = async () => { try { const reqParams = { @@ -168,6 +203,7 @@ const District: React.FC = () => { updatedBy: any; }) => { const transformedName = transformLabel(districtDetail.name); + const matchingDistrict = districtsOptionRead.find( (district: { label: string }) => @@ -347,7 +383,7 @@ const District: React.FC = () => { name: name, type: "DISTRICT", status: Status.ACTIVE, - parentId: cohortId, + parentId: cohortIdofState, customFields: [ { fieldId: stateFieldId, @@ -359,6 +395,7 @@ const District: React.FC = () => { try { const cohortCreateResponse = await createCohort(queryParameters); if (cohortCreateResponse) { + paginatedData() showToastMessage(t("COMMON.DISTRICT_ADDED_SUCCESS"), "success"); } else if (cohortCreateResponse.responseCode === 409) { showToastMessage(t("COMMON.DISTRICT_DUPLICATION_FAILURE"), "error"); @@ -409,6 +446,7 @@ const District: React.FC = () => { queryParameters ); if (cohortCreateResponse) { + paginatedData() showToastMessage(t("COMMON.DISTRICT_UPDATED_SUCCESS"), "success"); } else if (cohortCreateResponse.responseCode === 409) { showToastMessage(t("COMMON.DISTRICT_DUPLICATION_FAILURE"), "error"); @@ -561,7 +599,7 @@ const District: React.FC = () => { - {districtData.length > 0 ? ( + {paginatedData().length > 0 ? ( { const { t } = useTranslation(); const [stateData, setStateData] = useState([]); @@ -76,9 +74,10 @@ const State: React.FC = () => { const [userName, setUserName] = React.useState(""); const [statesProfilesData, setStatesProfilesData] = useState([]); const [pagination, setPagination] = useState(true); - + const [stateDataOption, setStateDataOptinon] = useState([]); + const [stateCodArrray, setStateCodeArr] = useState([]); + const [stateNameArray, setStateNameArr] = useState([]); const setPid = useStore((state) => state.setPid); - const columns = [ { key: "label", title: t("MASTER.STATE"), width: "160" }, { key: "value", title: t("MASTER.CODE"), width: "160" }, @@ -88,24 +87,100 @@ const State: React.FC = () => { { key: "updatedAt", title: t("MASTER.UPDATED_AT"), width: "160" }, // { key: "actions", title: t("MASTER.ACTIONS"), width: "160" }, ]; + const fetchStateData = async () => { + try { + setLoading(true); + const limit = pageLimit; + const offset = pageOffset * limit; + const data = { + limit: limit, + offset: offset, + fieldName: "states", + optionName: searchKeyword || "", + sort: sortBy, + }; + const resp = await getStateBlockDistrictList(data); + const states = resp?.result?.values || []; + setStateDataOptinon(states); + const stateNameArra = states.map((item: any) => item.label); + setStateNameArr(stateNameArra); + console.log("stateNameArray", stateNameArray); + const stateCodeArra = states.map((item: any) => item.value); + setStateCodeArr(stateCodeArra); + console.log("stateDataOptinon", stateCodeArra); + if (resp?.result?.fieldId) { + setFieldId(resp.result.fieldId); + } else { + console.error("Unexpected fieldId:", resp?.result?.fieldId); + } + } catch (error) { + console.error("Error fetching state data", error); + } finally { + setLoading(false); + } + }; + useEffect(() => { + fetchStateData(); + }, []); + const getStatecohorts = async () => { + try { + const reqParams = { + limit: 0, + offset: 0, + filters: { + name: searchKeyword, + type: "STATE", + }, + sort: sortBy, + }; + const response = await getCohortList(reqParams); + const statecohortDetails = response?.results?.cohortDetails || []; + const filteredStateData = statecohortDetails + .map((stateDetail: any) => { + const transformedName = transformLabel(stateDetail.name); + const matchingState = stateDataOption.find( + (state: { label: string }) => state.label === transformedName + ); + return { + label: transformedName, + value: matchingState ? matchingState.value : null, + createdAt: stateDetail.createdAt, + updatedAt: stateDetail.updatedAt, + createdBy: stateDetail.createdBy, + updatedBy: stateDetail.updatedBy, + cohortId: stateDetail.cohortId, + }; + }) + .filter((state: { label: any }) => + stateNameArray.includes(state.label) + ); + setStateData(filteredStateData); + console.log(stateData); + } catch (error) { + console.error("Error fetching and filtering cohort states", error); + } + }; + useEffect(() => { + if (stateDataOption.length > 0 && stateNameArray.length > 0) { + getStatecohorts(); + } +}, [stateDataOption, stateNameArray, searchKeyword, pageLimit, pageOffset, sortBy]); + const handleEdit = (rowData: StateDetail) => { setSelectedStateForEdit(rowData); setAddStateModalOpen(true); }; - const handleDelete = (rowData: StateDetail) => { setSelectedStateForDelete(rowData); setConfirmationDialogOpen(true); }; - const handleSortChange = async (event: SelectChangeEvent) => { const sortOrder = event.target.value === "Z-A" ? SORT.DESCENDING : SORT.ASCENDING; setSortBy(["name", sortOrder]); setSelectedSort(event.target.value); }; - const handleConfirmDelete = async () => { if (selectedStateForDelete) { try { @@ -123,16 +198,13 @@ const State: React.FC = () => { setConfirmationDialogOpen(false); } }; - const handleSearch = (keyword: string) => { setSearchKeyword(keyword); }; - const handleAddStateClick = () => { setSelectedStateForEdit(null); setAddStateModalOpen(true); }; - const handleAddStateSubmit = async ( name: string, value: string, @@ -145,7 +217,6 @@ const State: React.FC = () => { if (fieldId) { const isUpdating = selectedState !== null; const response = await createOrUpdateOption(fieldId, newState); - const queryParameters = { name: name, type: "STATE", @@ -153,20 +224,15 @@ const State: React.FC = () => { parentId: null, customFields: [], }; - console.log("before cohortList"); - if (!isUpdating) { await createCohort(queryParameters); } - if (response) { await fetchStateData(); - const successMessage = isUpdating ? t("COMMON.STATE_UPDATED_SUCCESS") : t("COMMON.STATE_ADDED_SUCCESS"); - showToastMessage(successMessage, "success"); } else { console.error("Failed to create/update state:", response); @@ -186,14 +252,12 @@ const State: React.FC = () => { ); setPageLimit(newSize); }; - const handlePaginationChange = ( event: React.ChangeEvent, value: number ) => { setPageOffset(value - 1); }; - const PagesSelector = () => ( <> @@ -209,7 +273,6 @@ const State: React.FC = () => { ); - const PageSizeSelectorFunction = () => ( { /> ); - - const fetchStateData = async () => { - try { - setLoading(true); - const limit = pageLimit; - const offset = pageOffset * limit; - - const data = { - limit: limit, - offset: offset, - fieldName: "states", - optionName: searchKeyword || "", - sort: sortBy, - }; - - console.log("fetchStateData", data); - - const resp = await getStateBlockDistrictList(data); - - if (resp?.result?.fieldId) { - setFieldId(resp.result.fieldId); - setStateData(resp.result.values); - - const totalCount = resp?.result?.totalCount || 0; - - setPaginationCount(totalCount); - - console.log("totalCount", totalCount); - - setPagination(totalCount > 10); - setPageSizeArray( - totalCount > 15 - ? [5, 10, 15] - : totalCount > 10 - ? [5, 10] - : totalCount > 5 - ? [5] - : [] - ); - - setPageCount(Math.ceil(totalCount / limit)); - } else { - console.error("Unexpected fieldId:", resp?.result?.fieldId); - } - } catch (error) { - console.error("Error fetching state data", error); - setStateData([]); - } finally { - setLoading(false); - } - }; - useEffect(() => { - fetchStateData(); - }, [searchKeyword, pageLimit, pageOffset, sortBy]); - return ( { ) : ( ({ - label: stateDetail.label ?? "", - value: stateDetail.value ?? "", - createdAt: stateDetail.createdAt, - updatedAt: stateDetail.updatedAt, - createdBy: stateDetail.createdBy, - updatedBy: stateDetail.updatedBy, - }))} + data={stateData} limit={pageLimit} offset={pageOffset} paginationEnable={paginationCount >= Numbers.FIVE} @@ -331,9 +332,7 @@ const State: React.FC = () => { ); }; - export default State; - export const getServerSideProps = async ({ locale }: { locale: string }) => { return { props: { @@ -341,3 +340,7 @@ export const getServerSideProps = async ({ locale }: { locale: string }) => { }, }; }; + + + +