diff --git a/src/Components/ExternalResult/ListFilter.tsx b/src/Components/ExternalResult/ListFilter.tsx index d2651afcb3f..ca0fcabfad1 100644 --- a/src/Components/ExternalResult/ListFilter.tsx +++ b/src/Components/ExternalResult/ListFilter.tsx @@ -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) => { diff --git a/src/Components/ExternalResult/ResultItem.tsx b/src/Components/ExternalResult/ResultItem.tsx index a98026b5b08..ec24273c114 100644 --- a/src/Components/ExternalResult/ResultItem.tsx +++ b/src/Components/ExternalResult/ResultItem.tsx @@ -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 }, diff --git a/src/Components/ExternalResult/ResultList.tsx b/src/Components/ExternalResult/ResultList.tsx index c108ace8a9b..1e8b5ace74e 100644 --- a/src/Components/ExternalResult/ResultList.tsx +++ b/src/Components/ExternalResult/ResultList.tsx @@ -17,6 +17,7 @@ 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")); @@ -24,7 +25,7 @@ export default function ResultList() { const [resultListData, setResultListData] = useState< Partial[] >([]); - const [isLoading, setIsLoading] = useState(false); + const [isLoading, setIsLoading] = useState(true); const [totalCount, setTotalCount] = useState(0); const { qParams, @@ -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); @@ -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, @@ -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 ( @@ -215,7 +227,7 @@ export default function ResultList() { }); } - if (isLoading || !data) { + if (isLoading) { manageResults = ( @@ -223,9 +235,9 @@ export default function ResultList() { ); - } else if (data && resultListData.length) { + } else if (resultListData.length) { manageResults = <>{resultList}; - } else if (data && resultListData.length === 0) { + } else if (resultListData.length === 0) { manageResults = ( diff --git a/src/Components/ExternalResult/models.ts b/src/Components/ExternalResult/models.ts index 163e3400f34..8ccaba04d05 100644 --- a/src/Components/ExternalResult/models.ts +++ b/src/Components/ExternalResult/models.ts @@ -50,27 +50,10 @@ export interface ILocalBodies { district: number; } -export interface IExternalResultList { - count: number; - results: Partial[]; -} - 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; diff --git a/src/Redux/actions.tsx b/src/Redux/actions.tsx index 3257ab1fa3e..06a12796737 100644 --- a/src/Redux/actions.tsx +++ b/src/Redux/actions.tsx @@ -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); diff --git a/src/Redux/api.tsx b/src/Redux/api.tsx index e4ceaa74a6a..60ca3c2906b 100644 --- a/src/Redux/api.tsx +++ b/src/Redux/api.tsx @@ -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"; @@ -509,7 +507,7 @@ const routes = { externalResultList: { path: "/api/v1/external_result/", method: "GET", - TRes: Type(), + TRes: Type>(), }, externalResult: { path: "/api/v1/external_result/{id}/", @@ -593,7 +591,7 @@ const routes = { getWardByLocalBody: { path: "/api/v1/ward/?local_body={id}", method: "GET", - TRes: Type(), + TRes: Type>(), }, // Sample Test diff --git a/src/Utils/request/request.ts b/src/Utils/request/request.ts index 2d735734aae..a2adad1bd45 100644 --- a/src/Utils/request/request.ts +++ b/src/Utils/request/request.ts @@ -38,13 +38,30 @@ export default async function request( 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), - }; + if (!res.ok) { + const data: Record = 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);