Skip to content

Commit

Permalink
Merge pull request #165 from rahulg1254/admin-master
Browse files Browse the repository at this point in the history
Task #223691 feat: resolved transform label issue on table
  • Loading branch information
itsvick authored Aug 28, 2024
2 parents 4957d1f + aba8827 commit 65ddd80
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
47 changes: 33 additions & 14 deletions src/pages/district.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const District: React.FC = () => {
fetchDistricts();
}, [stateCode]);

// get cohort id of state
const getStatecohorts = async () => {
let userId: any;
try {
Expand Down Expand Up @@ -173,8 +174,9 @@ const District: React.FC = () => {
useEffect(() => {
getStatecohorts();
}, []);




const getFilteredCohortData = async () => {
try {
const reqParams = {
Expand Down Expand Up @@ -202,15 +204,14 @@ const District: React.FC = () => {
createdBy: any;
updatedBy: any;
}) => {
const transformedName = transformLabel(districtDetail.name);
const transformedName = districtDetail.name


const matchingDistrict = districtsOptionRead.find(
(district: { label: string }) =>
district.label === transformedName
);

console.log("matchingDistrict", matchingDistrict);

return {
label: transformedName,
Expand Down Expand Up @@ -258,9 +259,9 @@ const District: React.FC = () => {
sort: sortBy,
};

const response = await getCohortList(reqParams);

const activeBlocks = response?.results?.cohortDetails || [];
const response:any = await getCohortList(reqParams);
const activeBlocks = response?.results?.cohortDetails || [];
console.log("activeBlocks", activeBlocks);

const activeBlocksCount = activeBlocks.filter(
Expand All @@ -273,6 +274,8 @@ const District: React.FC = () => {
setLoading(false);
}
};


useEffect(() => {
if (districtValueForDelete) {
getBlockDataCohort();
Expand Down Expand Up @@ -332,7 +335,7 @@ const District: React.FC = () => {
const resp = await updateCohort(cohotIdForDelete, cohortDetails);
console.log(resp);
if (resp?.responseCode === 200) {
const cohort = paginatedData()?.find(
const cohort = filteredCohortOptionData()?.find(
(item: any) => item.cohortId == cohotIdForDelete
);
if (cohort) {
Expand Down Expand Up @@ -395,7 +398,7 @@ const District: React.FC = () => {
try {
const cohortCreateResponse = await createCohort(queryParameters);
if (cohortCreateResponse) {
paginatedData()
filteredCohortOptionData()
showToastMessage(t("COMMON.DISTRICT_ADDED_SUCCESS"), "success");
} else if (cohortCreateResponse.responseCode === 409) {
showToastMessage(t("COMMON.DISTRICT_DUPLICATION_FAILURE"), "error");
Expand Down Expand Up @@ -446,7 +449,7 @@ const District: React.FC = () => {
queryParameters
);
if (cohortCreateResponse) {
paginatedData()
filteredCohortOptionData()
showToastMessage(t("COMMON.DISTRICT_UPDATED_SUCCESS"), "success");
} else if (cohortCreateResponse.responseCode === 409) {
showToastMessage(t("COMMON.DISTRICT_DUPLICATION_FAILURE"), "error");
Expand Down Expand Up @@ -478,12 +481,28 @@ const District: React.FC = () => {
) => {
setPageOffset(value - 1);
};
const paginatedData = () => {

function transformLabels(label: string) {
if (!label || typeof label !== 'string') return '';
return label
.toLowerCase()
.replace(/_/g, " ")
.replace(/\b\w/g, (char) => char.toUpperCase());
}


const filteredCohortOptionData = () => {
const startIndex = pageOffset * pageLimit;
const endIndex = startIndex + pageLimit;
return districtData.slice(startIndex, endIndex);

const transformedData = districtData.map(item => ({
...item,
label: transformLabels(item.label)
}));

return transformedData.slice(startIndex, endIndex);
};
const PagesSelector = () => (
const PagesSelector = () => (
<>
<Box sx={{ display: { xs: "block" } }}>
<Pagination
Expand Down Expand Up @@ -599,7 +618,7 @@ const District: React.FC = () => {
</FormControl>
</Box>

{paginatedData().length > 0 ? (
{filteredCohortOptionData().length > 0 ? (
<KaTableComponent
columns={[
{
Expand Down Expand Up @@ -641,7 +660,7 @@ const District: React.FC = () => {
width: "130",
},
]}
data={paginatedData()}
data={filteredCohortOptionData()}
limit={pageLimit}
offset={pageOffset}
paginationEnable={paginationCount >= Numbers.FIVE}
Expand Down
10 changes: 5 additions & 5 deletions src/services/MasterDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,18 @@ export const getBlocksForDistricts = async ({
optionName,
sort,
}: {
limit: number;
offset: number;
controllingfieldfk: string | undefined;
limit?: number;
offset?: number;
controllingfieldfk?: string | undefined;
fieldName: string;
optionName?: string;
sort?: [string, string];
}): Promise<any> => {
const apiUrl: string = `${process.env.NEXT_PUBLIC_BASE_URL}/fields/options/read`;

const requestBody: {
limit: number;
offset: number;
limit?: number;
offset?: number;
controllingfieldfk?: string;
fieldName: string;
optionName?: string;
Expand Down

0 comments on commit 65ddd80

Please sign in to comment.