Skip to content

Commit

Permalink
fixed filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
konavivekramakrishna committed Oct 6, 2023
1 parent 0ee26c6 commit 1ab65b7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 46 deletions.
3 changes: 3 additions & 0 deletions src/Components/ExternalResult/ListFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ export default function ListFilter(props: any) {
};
onChange(data);
dataList(selectedLsgs, wards);
console.log("data", data);
console.log("selectedLsgs", selectedLsgs);
console.log("wards", wards);
};

const sortByName = (items: any) => {
Expand Down
1 change: 0 additions & 1 deletion src/Components/ExternalResult/ResultItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export default function ResultItem(props: any) {
});

const handleDelete = async () => {
console.log("handleDelete");
if (showDeleteAlert) {
const { res, data } = await request(routes.deleteExternalResult, {
pathParams: { id: props.id },
Expand Down
46 changes: 29 additions & 17 deletions src/Components/ExternalResult/ResultList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import Page from "../Common/components/Page";
import routes from "../../Redux/api";
import useQuery from "../../Utils/request/useQuery";
import { IExternalResult } from "./models";
import request from "../../Utils/request/request";

const Loading = lazy(() => import("../Common/Loading"));

export default function ResultList() {
const [resultListData, setResultListData] = useState<
Partial<IExternalResult>[]
>([]);
const [isLoading, setIsLoading] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [totalCount, setTotalCount] = useState(0);
const {
qParams,
Expand Down Expand Up @@ -60,7 +61,17 @@ export default function ResultList() {

setPhoneNumberError("Enter a valid number");
};
const { res, data, loading } = useQuery(routes.externalResultList);

useQuery(routes.externalResultList, {
onResponse: ({ res, data }) => {
if (res && data) {
setResultListData(data.results);
setTotalCount(data.count);
setIsLoading(false);
}
},
});

let manageResults: any = null;
useEffect(() => {
setIsLoading(true);
Expand All @@ -80,25 +91,26 @@ export default function ResultList() {
qParams.sample_collection_date_after || undefined,
sample_collection_date_before:
qParams.sample_collection_date_before || undefined,
offset: (qParams.page ? qParams.page - 1 : 0) * resultsPerPage,
offset: String((qParams.page ? qParams.page - 1 : 0) * resultsPerPage),
srf_id: qParams.srf_id || undefined,
};

if (loading) {
setIsLoading(true);
} else if (res && data) {
setResultListData(data.results);
setTotalCount(data.count);
setIsLoading(false);
}
const fetchData = async () => {
const { res, data } = await request(routes.externalResultList, {
query: params,
});
if (res && data) {
setResultListData(data.results);
setTotalCount(data.count);
setIsLoading(false);
}
};
fetchData();

if (!params.mobile_number) {
setPhoneNum("+91");
}
}, [
res,
data,
loading,
qParams.name,
qParams.page,
qParams.mobile_number,
Expand Down Expand Up @@ -159,7 +171,7 @@ export default function ResultList() {
};

let resultList: any[] = [];
if (data && resultListData.length) {
if (resultListData.length) {
resultList = resultListData.map((result: any) => {
const resultUrl = `/external_results/${result.id}`;
return (
Expand Down Expand Up @@ -215,17 +227,17 @@ export default function ResultList() {
});
}

if (isLoading || !data) {
if (isLoading) {
manageResults = (
<tr className="bg-white">
<td colSpan={5}>
<Loading />
</td>
</tr>
);
} else if (data && resultListData.length) {
} else if (resultListData.length) {
manageResults = <>{resultList}</>;
} else if (data && resultListData.length === 0) {
} else if (resultListData.length === 0) {
manageResults = (
<tr className="bg-white">
<td colSpan={5}>
Expand Down
17 changes: 0 additions & 17 deletions src/Components/ExternalResult/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,10 @@ export interface ILocalBodies {
district: number;
}

export interface IExternalResultList {
count: number;
results: Partial<IExternalResult>[];
}

export interface IDeleteExternalResult {
detail: string;
}

export interface Ward {
id: number;
name: string;
number: number;
local_body: number;
}

export interface IWardByLocalBody {
count: number;
results: Ward[];
}

export interface IPartialUpdateExternalResult {
address: string;
ward: number;
Expand Down
4 changes: 4 additions & 0 deletions src/Redux/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ export const getWards = (params: object) => {
return fireRequest("getWards", [], params);
};

export const getAllLocalBodyByDistrict = (pathParam: object) => {
return fireRequest("getAllLocalBodyByDistrict", [], {}, pathParam);
};

// Local Body
export const getLocalBody = (pathParam: object) => {
return fireRequest("getLocalBody", [], {}, pathParam);
Expand Down
8 changes: 3 additions & 5 deletions src/Redux/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import {
IDeleteExternalResult,
IExternalResult,
IExternalResultCsv,
IExternalResultList,
ILocalBodies,
ILocalBodyByDistrict,
IPartialUpdateExternalResult,
IWardByLocalBody,
} from "../Components/ExternalResult/models";
import { LocationModel } from "../Components/Facility/models";
import { LocationModel, WardModel } from "../Components/Facility/models";
import { Prescription } from "../Components/Medicine/models";
import { UserModel } from "../Components/Users/models";
import { PaginatedResponse } from "../Utils/request/types";
Expand Down Expand Up @@ -509,7 +507,7 @@ const routes = {
externalResultList: {
path: "/api/v1/external_result/",
method: "GET",
TRes: Type<IExternalResultList>(),
TRes: Type<PaginatedResponse<IExternalResult>>(),
},
externalResult: {
path: "/api/v1/external_result/{id}/",
Expand Down Expand Up @@ -593,7 +591,7 @@ const routes = {
getWardByLocalBody: {
path: "/api/v1/ward/?local_body={id}",
method: "GET",
TRes: Type<IWardByLocalBody>(),
TRes: Type<PaginatedResponse<WardModel>>(),
},

// Sample Test
Expand Down
29 changes: 23 additions & 6 deletions src/Utils/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,30 @@ export default async function request<TData, TBody>(

try {
const res = await fetch(url, options);
const data: TData = await res.json();

result = {
res,
data: res.ok ? data : undefined,
error: res.ok ? undefined : (data as Record<string, unknown>),
};
if (!res.ok) {
const data: Record<string, unknown> = await res.json();
result = {
res,
data: undefined,
error: data,
};
} else if (
res.headers.get("content-type")?.includes("application/json")
) {
const data: TData = await res.json();
result = {
res,
data,
error: undefined,
};
} else {
result = {
res,
data: undefined,
error: undefined,
};
}

onResponse?.(result);
handleResponse(result, silent);
Expand Down

0 comments on commit 1ab65b7

Please sign in to comment.