diff --git a/src/Common/hooks/useExport.tsx b/src/Common/hooks/useExport.tsx index e7a76036b07..1e7540e6525 100644 --- a/src/Common/hooks/useExport.tsx +++ b/src/Common/hooks/useExport.tsx @@ -1,9 +1,7 @@ import dayjs from "../../Utils/dayjs"; import { useState } from "react"; -import { useDispatch } from "react-redux"; export default function useExport() { - const dispatch: any = useDispatch(); const [isExporting, setIsExporting] = useState(false); const getTimestamp = () => { @@ -16,17 +14,17 @@ export default function useExport() { const exportCSV = async ( filenamePrefix: string, - action: any, + getData: () => Promise, parse = (data: string) => data, ) => { setIsExporting(true); const filename = `${filenamePrefix}_${getTimestamp()}.csv`; - const res = await dispatch(action); - if (res.status === 200) { + const data = await getData(); + if (data) { const a = document.createElement("a"); - const blob = new Blob([parse(res.data)], { + const blob = new Blob([parse(data)], { type: "text/csv", }); a.href = URL.createObjectURL(blob); @@ -39,15 +37,15 @@ export default function useExport() { const exportJSON = async ( filenamePrefix: string, - action: any, + getData: () => Promise<{ results: object[] } | null>, parse = (data: string) => data, ) => { setIsExporting(true); - const res = await dispatch(action); - if (res.status === 200) { + const data = await getData(); + if (data?.results.length) { const a = document.createElement("a"); - const blob = new Blob([parse(JSON.stringify(res.data.results))], { + const blob = new Blob([parse(JSON.stringify(data.results))], { type: "application/json", }); a.href = URL.createObjectURL(blob); @@ -59,7 +57,7 @@ export default function useExport() { }; const exportFile = ( - action: any, + action: () => Promise<{ results: object[] } | string | null>, filePrefix = "export", type = "csv", parse = (data: string) => data, @@ -68,13 +66,17 @@ export default function useExport() { switch (type) { case "csv": - exportCSV(filePrefix, action(), parse); + exportCSV(filePrefix, action as Parameters[1], parse); break; case "json": - exportJSON(filePrefix, action(), parse); + exportJSON( + filePrefix, + action as Parameters[1], + parse, + ); break; default: - exportCSV(filePrefix, action(), parse); + exportCSV(filePrefix, action as Parameters[1], parse); } }; diff --git a/src/Components/Assets/AssetsList.tsx b/src/Components/Assets/AssetsList.tsx index fa609e0e3b8..72da2dad360 100644 --- a/src/Components/Assets/AssetsList.tsx +++ b/src/Components/Assets/AssetsList.tsx @@ -1,6 +1,5 @@ import { Scanner } from "@yudiel/react-qr-scanner"; import * as Notification from "../../Utils/Notifications.js"; -import { listAssets } from "../../Redux/actions"; import { assetClassProps, AssetData } from "./AssetTypes"; import { useState, useEffect, lazy } from "react"; import { Link, navigate } from "raviger"; @@ -317,13 +316,12 @@ const AssetsList = () => { }, { label: "Export Assets (JSON)", - action: () => - authorizedForImportExport && - listAssets({ - ...qParams, - json: true, - limit: totalCount, - }), + action: async () => { + const { data } = await request(routes.listAssets, { + query: { ...qParams, json: true, limit: totalCount }, + }); + return data ?? null; + }, type: "json", filePrefix: `assets_${facility?.name ?? "all"}`, options: { @@ -334,13 +332,12 @@ const AssetsList = () => { }, { label: "Export Assets (CSV)", - action: () => - authorizedForImportExport && - listAssets({ - ...qParams, - csv: true, - limit: totalCount, - }), + action: async () => { + const { data } = await request(routes.listAssets, { + query: { ...qParams, csv: true, limit: totalCount }, + }); + return data ?? null; + }, type: "csv", filePrefix: `assets_${facility?.name ?? "all"}`, options: { diff --git a/src/Components/Common/Export.tsx b/src/Components/Common/Export.tsx index 72d5f447e57..fb1aa812633 100644 --- a/src/Components/Common/Export.tsx +++ b/src/Components/Common/Export.tsx @@ -6,6 +6,8 @@ import DropdownMenu, { import ButtonV2 from "../../Components/Common/components/ButtonV2"; import CareIcon from "../../CAREUI/icons/CareIcon"; import useExport from "../../Common/hooks/useExport"; +import { Route } from "../../Utils/request/types"; +import request from "../../Utils/request/request"; interface ExportItem { options?: DropdownItemProps; @@ -13,7 +15,8 @@ interface ExportItem { filePrefix?: string; label: string; parse?: (data: string) => string; - action?: any; + action?: Parameters["exportFile"]>[0]; + route?: Route; } interface ExportMenuProps { @@ -27,7 +30,8 @@ interface ExportButtonProps { tooltip?: string | undefined; tooltipClassName?: string; type?: "csv" | "json"; - action?: any; + action?: Parameters["exportFile"]>[0]; + route?: Route; parse?: (data: string) => string; filenamePrefix: string; } @@ -45,9 +49,18 @@ export const ExportMenu = ({ return ( - exportFile(item.action, item.filePrefix, item.type, item.parse) - } + onClick={() => { + let action = item.action; + if (item.route) { + action = async () => { + const { data } = await request(item.route!); + return data ?? null; + }; + } + if (action) { + exportFile(action, item.filePrefix, item.type, item.parse); + } + }} border ghost className="py-2.5" @@ -69,9 +82,18 @@ export const ExportMenu = ({ {exportItems.map((item) => ( - exportFile(item.action, item.filePrefix, item.type, item.parse) - } + onClick={() => { + let action = item.action; + if (item.route) { + action = async () => { + const { data } = await request(item.route!); + return data ?? null; + }; + } + if (action) { + exportFile(action, item.filePrefix, item.type, item.parse); + } + }} {...item.options} > {item.label} @@ -94,9 +116,18 @@ export const ExportButton = ({ <> - exportFile(props.action, props.filenamePrefix, type, parse) - } + onClick={() => { + let action = props.action; + if (props.route) { + action = async () => { + const { data } = await request(props.route!); + return data ?? null; + }; + } + if (action) { + exportFile(action, props.filenamePrefix, type, parse); + } + }} className="tooltip mx-2 p-4 text-lg text-secondary-800 disabled:bg-transparent disabled:text-secondary-500" variant="secondary" ghost diff --git a/src/Components/ExternalResult/ResultList.tsx b/src/Components/ExternalResult/ResultList.tsx index abd77e0e4ac..74c4655104d 100644 --- a/src/Components/ExternalResult/ResultList.tsx +++ b/src/Components/ExternalResult/ResultList.tsx @@ -1,7 +1,6 @@ import ButtonV2 from "../Common/components/ButtonV2"; import { navigate } from "raviger"; import { lazy, useState } from "react"; -import { externalResultList } from "../../Redux/actions"; import ListFilter from "./ListFilter"; import FacilitiesSelectDialogue from "./FacilitiesSelectDialogue"; import { FacilityModel } from "../Facility/models"; @@ -19,6 +18,7 @@ import { parsePhoneNumber } from "../../Utils/utils"; import useAuthUser from "../../Common/hooks/useAuthUser"; import { NonReadOnlyUsers } from "../../Utils/AuthorizeFor"; import ExternalResultImportModal from "./ExternalResultImportModal"; +import request from "../../Utils/request/request"; const Loading = lazy(() => import("../Common/Loading")); @@ -254,11 +254,12 @@ export default function ResultList() { : []), { label: "Export Results", - action: () => - externalResultList( - { ...qParams, csv: true }, - "externalResultList", - ), + action: async () => { + const { data } = await request(routes.externalResultList, { + query: { ...qParams, csv: true }, + }); + return data ?? null; + }, filePrefix: "external_results", options: { icon: , diff --git a/src/Components/Facility/ConsultationDetails/index.tsx b/src/Components/Facility/ConsultationDetails/index.tsx index 911e62d5627..cac822d76c4 100644 --- a/src/Components/Facility/ConsultationDetails/index.tsx +++ b/src/Components/Facility/ConsultationDetails/index.tsx @@ -4,7 +4,6 @@ import { getConsultation, getPatient, listAssetBeds, - listShiftRequests, } from "../../../Redux/actions"; import { statusType, useAbortableEffect } from "../../../Common/utils"; import { lazy, useCallback, useState } from "react"; @@ -179,12 +178,11 @@ export const ConsultationDetails = (props: any) => { setAbhaNumberData(abhaNumberData); // Get shifting data - const shiftingRes = await dispatch( - listShiftRequests({ patient: id }, "shift-list-call"), - ); - if (shiftingRes?.data?.results) { - const data = shiftingRes.data.results; - setActiveShiftingData(data); + const shiftRequestsQuery = await request(routes.listShiftRequests, { + query: { patient: id }, + }); + if (shiftRequestsQuery.data?.results) { + setActiveShiftingData(shiftRequestsQuery.data.results); } } else { navigate("/not-found"); diff --git a/src/Components/Facility/HospitalList.tsx b/src/Components/Facility/HospitalList.tsx index 893d54cd698..ca67fd0eb2c 100644 --- a/src/Components/Facility/HospitalList.tsx +++ b/src/Components/Facility/HospitalList.tsx @@ -1,9 +1,3 @@ -import { - downloadFacility, - downloadFacilityCapacity, - downloadFacilityDoctors, - downloadFacilityTriage, -} from "../../Redux/actions"; import { lazy, useEffect } from "react"; import { AdvancedFilterButton } from "../../CAREUI/interactive/FiltersSlideover"; import CountBlock from "../../CAREUI/display/Count"; @@ -160,22 +154,22 @@ export const HospitalList = () => { exportItems={[ { label: "Facilities", - action: downloadFacility, + route: routes.downloadFacility, filePrefix: "facilities", }, { label: "Capacities", - action: downloadFacilityCapacity, + route: routes.downloadFacilityCapacity, filePrefix: "capacities", }, { label: "Doctors", - action: downloadFacilityDoctors, + route: routes.downloadFacilityDoctors, filePrefix: "doctors", }, { label: "Triages", - action: downloadFacilityTriage, + route: routes.downloadFacilityTriage, filePrefix: "triages", }, ]} diff --git a/src/Components/Patient/ManagePatients.tsx b/src/Components/Patient/ManagePatients.tsx index 7a5b0d28a38..0ad1b81705b 100644 --- a/src/Components/Patient/ManagePatients.tsx +++ b/src/Components/Patient/ManagePatients.tsx @@ -13,7 +13,6 @@ import { import { FacilityModel, PatientCategory } from "../Facility/models"; import { Link, navigate } from "raviger"; import { ReactNode, lazy, useEffect, useState } from "react"; -import { getAllPatient } from "../../Redux/actions"; import { parseOptionId } from "../../Common/utils"; import { AdvancedFilterButton } from "../../CAREUI/interactive/FiltersSlideover"; @@ -53,6 +52,7 @@ import { import { ICD11DiagnosisModel } from "../Diagnosis/types.js"; import { getDiagnosesByIds } from "../Diagnosis/utils.js"; import Tabs from "../Common/components/Tabs.js"; +import request from "../../Utils/request/request.js"; const Loading = lazy(() => import("../Common/Loading")); @@ -276,13 +276,6 @@ export const PatientManager = () => { !durations.every((x) => x === 0); let managePatients: any = null; - - const exportPatients = (isFiltered: boolean) => { - const filters = { ...params, csv: true, facility: qParams.facility }; - if (!isFiltered) delete filters.is_active; - return () => getAllPatient(filters, "downloadPatients"); - }; - const preventDuplicatePatientsDuetoPolicyId = (data: any) => { // Generate a array which contains imforamation of duplicate patient IDs and there respective linenumbers const lines = data.split("\n"); // Split the data into individual lines @@ -923,7 +916,18 @@ export const PatientManager = () => { exportItems={[ { label: "Export Live patients", - action: exportPatients(true), + action: async () => { + const query = { + ...params, + csv: true, + facility: qParams.facility, + }; + delete qParams.is_active; + const { data } = await request(routes.patientList, { + query, + }); + return data ?? null; + }, parse: preventDuplicatePatientsDuetoPolicyId, }, ]} diff --git a/src/Components/Patient/SampleViewAdmin.tsx b/src/Components/Patient/SampleViewAdmin.tsx index 43430dc8546..ced7e1a96ba 100644 --- a/src/Components/Patient/SampleViewAdmin.tsx +++ b/src/Components/Patient/SampleViewAdmin.tsx @@ -7,7 +7,6 @@ import { SAMPLE_FLOW_RULES, SAMPLE_TYPE_CHOICES, } from "../../Common/constants"; -import { downloadSampleTests } from "../../Redux/actions"; import * as Notification from "../../Utils/Notifications"; import { SampleTestModel } from "./models"; import UpdateStatusDialog from "./UpdateStatusDialog"; @@ -314,7 +313,12 @@ export default function SampleViewAdmin() { breadcrumbs={false} componentRight={ downloadSampleTests({ ...qParams })} + action={async () => { + const { data } = await request(routes.getTestSampleList, { + query: { ...qParams, csv: true }, + }); + return data ?? null; + }} parse={parseExportData} filenamePrefix="samples" /> diff --git a/src/Components/Resource/ListView.tsx b/src/Components/Resource/ListView.tsx index 128e17c2c46..11362f54891 100644 --- a/src/Components/Resource/ListView.tsx +++ b/src/Components/Resource/ListView.tsx @@ -1,6 +1,5 @@ import { lazy } from "react"; import { navigate } from "raviger"; -import { downloadResourceRequests } from "../../Redux/actions"; import ListFilter from "./ListFilter"; import { formatFilter } from "./Commons"; import BadgesList from "./BadgesList"; @@ -16,6 +15,7 @@ import useQuery from "../../Utils/request/useQuery"; import routes from "../../Redux/api"; import Page from "../Common/components/Page"; import SearchInput from "../Form/SearchInput"; +import request from "../../Utils/request/request"; const Loading = lazy(() => import("../Common/Loading")); @@ -161,7 +161,12 @@ export default function ListView() { hideBack componentRight={ downloadResourceRequests({ ...appliedFilters, csv: 1 })} + action={async () => { + const { data } = await request(routes.downloadResourceRequests, { + query: { ...appliedFilters, csv: true }, + }); + return data ?? null; + }} filenamePrefix="resource_requests" /> } diff --git a/src/Components/Resource/ResourceBoard.tsx b/src/Components/Resource/ResourceBoard.tsx index 4b32c72eda4..aa0b031ba0e 100644 --- a/src/Components/Resource/ResourceBoard.tsx +++ b/src/Components/Resource/ResourceBoard.tsx @@ -1,5 +1,4 @@ import { useState, useEffect } from "react"; -import { downloadResourceRequests } from "../../Redux/actions"; import { navigate } from "raviger"; import { classNames, formatName } from "../../Utils/utils"; import { useDrag, useDrop } from "react-dnd"; @@ -241,12 +240,18 @@ export default function ResourceBoard({

{renderBoardTitle(board)}{" "} - downloadResourceRequests({ - ...formatFilter({ ...filterProp, status: board }), - csv: 1, - }) - } + action={async () => { + const { data } = await request( + routes.downloadResourceRequests, + { + query: { + ...formatFilter({ ...filterProp, status: board }), + csv: true, + }, + }, + ); + return data ?? null; + }} filenamePrefix={`resource_requests_${board}`} />

diff --git a/src/Components/Resource/ResourceBoardView.tsx b/src/Components/Resource/ResourceBoardView.tsx index bff0282f91a..345cd11c381 100644 --- a/src/Components/Resource/ResourceBoardView.tsx +++ b/src/Components/Resource/ResourceBoardView.tsx @@ -3,7 +3,6 @@ import { navigate } from "raviger"; import ListFilter from "./ListFilter"; import ResourceBoard from "./ResourceBoard"; import { RESOURCE_CHOICES } from "../../Common/constants"; -import { downloadResourceRequests } from "../../Redux/actions"; import withScrolling from "react-dnd-scrolling"; import BadgesList from "./BadgesList"; import { formatFilter } from "./Commons"; @@ -15,6 +14,8 @@ import { AdvancedFilterButton } from "../../CAREUI/interactive/FiltersSlideover" import CareIcon from "../../CAREUI/icons/CareIcon"; import SearchInput from "../Form/SearchInput"; import Tabs from "../Common/components/Tabs"; +import request from "../../Utils/request/request"; +import routes from "../../Redux/api"; const Loading = lazy(() => import("../Common/Loading")); const PageTitle = lazy(() => import("../Common/PageTitle")); @@ -50,9 +51,15 @@ export default function BoardView() { className="mx-3 md:mx-5" componentRight={ - downloadResourceRequests({ ...appliedFilters, csv: 1 }) - } + action={async () => { + const { data } = await request( + routes.downloadResourceRequests, + { + query: { ...appliedFilters, csv: true }, + }, + ); + return data ?? null; + }} filenamePrefix="resource_requests" /> } diff --git a/src/Components/Shifting/BoardView.tsx b/src/Components/Shifting/BoardView.tsx index f4fb45093aa..f303df72d99 100644 --- a/src/Components/Shifting/BoardView.tsx +++ b/src/Components/Shifting/BoardView.tsx @@ -8,7 +8,6 @@ import { ExportButton } from "../Common/Export"; import ListFilter from "./ListFilter"; import SearchInput from "../Form/SearchInput"; import ShiftingBoard from "./ShiftingBoard"; -import { downloadShiftRequests } from "../../Redux/actions"; import { formatFilter } from "./Commons"; import { navigate } from "raviger"; @@ -21,6 +20,8 @@ import { AdvancedFilterButton } from "../../CAREUI/interactive/FiltersSlideover" import CareIcon from "../../CAREUI/icons/CareIcon"; import Tabs from "../Common/components/Tabs"; import careConfig from "@careConfig"; +import request from "../../Utils/request/request"; +import routes from "../../Redux/api"; const Loading = lazy(() => import("../Common/Loading")); const PageTitle = lazy(() => import("../Common/PageTitle")); @@ -135,9 +136,12 @@ export default function BoardView() { hideBack componentRight={ - downloadShiftRequests({ ...formatFilter(qParams), csv: 1 }) - } + action={async () => { + const { data } = await request(routes.downloadShiftRequests, { + query: { ...formatFilter(qParams), csv: true }, + }); + return data ?? null; + }} filenamePrefix="shift_requests" /> } diff --git a/src/Components/Shifting/ListView.tsx b/src/Components/Shifting/ListView.tsx index b05795f934d..b40b42dc979 100644 --- a/src/Components/Shifting/ListView.tsx +++ b/src/Components/Shifting/ListView.tsx @@ -1,6 +1,4 @@ -import { downloadShiftRequests } from "../../Redux/actions"; import { lazy, useState } from "react"; - import BadgesList from "./BadgesList"; import ButtonV2 from "../Common/components/ButtonV2"; import ConfirmDialog from "../Common/ConfirmDialog"; @@ -236,9 +234,12 @@ export default function ListView() { hideBack componentRight={ - downloadShiftRequests({ ...formatFilter(qParams), csv: 1 }) - } + action={async () => { + const { data } = await request(routes.downloadShiftRequests, { + query: { ...formatFilter(qParams), csv: true }, + }); + return data ?? null; + }} filenamePrefix="shift_requests" /> } diff --git a/src/Components/Shifting/ShiftingBoard.tsx b/src/Components/Shifting/ShiftingBoard.tsx index d7d182ec3e0..6f2eb3b77a3 100644 --- a/src/Components/Shifting/ShiftingBoard.tsx +++ b/src/Components/Shifting/ShiftingBoard.tsx @@ -7,7 +7,6 @@ import { useState, } from "react"; import { classNames, formatDateTime, formatName } from "../../Utils/utils"; -import { downloadShiftRequests } from "../../Redux/actions"; import { useDrag, useDrop } from "react-dnd"; import ButtonV2 from "../Common/components/ButtonV2"; import ConfirmDialog from "../Common/ConfirmDialog"; @@ -346,12 +345,15 @@ export default function ShiftingBoard({

{title || board}{" "} - downloadShiftRequests({ - ...formatFilter({ ...filterProp, status: board }), - csv: 1, - }) - } + action={async () => { + const { data } = await request(routes.downloadShiftRequests, { + query: { + ...formatFilter({ ...filterProp, status: board }), + csv: true, + }, + }); + return data ?? null; + }} filenamePrefix={`shift_requests_${board}`} />

diff --git a/src/Redux/actions.tsx b/src/Redux/actions.tsx index 00e96e48eef..47d3530d065 100644 --- a/src/Redux/actions.tsx +++ b/src/Redux/actions.tsx @@ -24,27 +24,6 @@ export const deleteAssetBed = (asset_id: string) => }, ); -// Download Actions -export const downloadFacility = () => { - return fireRequest("downloadFacility"); -}; - -export const downloadFacilityCapacity = () => { - return fireRequest("downloadFacilityCapacity"); -}; - -export const downloadFacilityDoctors = () => { - return fireRequest("downloadFacilityDoctors"); -}; - -export const downloadFacilityTriage = () => { - return fireRequest("downloadFacilityTriage"); -}; - -//Patient -export const getAllPatient = (params: object, altKey: string) => { - return fireRequest("patientList", [], params, null, altKey); -}; export const getPatient = (pathParam: object) => { return fireRequest("getPatient", [], {}, pathParam); }; @@ -54,11 +33,6 @@ export const getDistrictByName = (params: object) => { return fireRequest("getDistrictByName", [], params, null); }; -// Sample Test -export const downloadSampleTests = (params: object) => { - return fireRequest("getTestSampleList", [], { ...params, csv: 1 }); -}; - // Consultation export const getConsultation = (id: string) => { return fireRequest("getConsultation", [], {}, { id: id }); @@ -68,26 +42,5 @@ export const dischargePatient = (params: object, pathParams: object) => { return fireRequest("dischargePatient", [], params, pathParams); }; -//Shift -export const listShiftRequests = (params: object, key: string) => { - return fireRequest("listShiftRequests", [], params, null, key); -}; - -export const downloadShiftRequests = (params: object) => { - return fireRequest("downloadShiftRequests", [], params); -}; - -// External Results -export const externalResultList = (params: object, altKey: string) => { - return fireRequest("externalResultList", [], params, null, altKey); -}; - -// Resource -export const downloadResourceRequests = (params: object) => { - return fireRequest("downloadResourceRequests", [], params); -}; - -export const listAssets = (params: object) => - fireRequest("listAssets", [], params); export const operateAsset = (id: string, params: object) => fireRequest("operateAsset", [], params, { external_id: id }); diff --git a/src/Redux/api.tsx b/src/Redux/api.tsx index 0bedb12dca5..a0c3271386d 100644 --- a/src/Redux/api.tsx +++ b/src/Redux/api.tsx @@ -577,24 +577,29 @@ const routes = { downloadFacility: { path: "/api/v1/facility/?csv", method: "GET", + TRes: Type(), }, downloadFacilityCapacity: { path: "/api/v1/facility/?csv&capacity", method: "GET", + TRes: Type(), }, downloadFacilityDoctors: { path: "/api/v1/facility/?csv&doctors", method: "GET", + TRes: Type(), }, downloadFacilityTriage: { path: "/api/v1/facility/?csv&triage", method: "GET", + TRes: Type(), }, downloadPatients: { path: "/api/v1/patient/?csv", method: "GET", + TRes: Type(), }, getConsultationList: { path: "/api/v1/consultation/", @@ -1120,6 +1125,7 @@ const routes = { downloadShiftRequests: { path: "/api/v1/shift/", method: "GET", + TRes: Type(), }, getShiftComments: { path: "/api/v1/shift/{id}/comment/", @@ -1291,6 +1297,7 @@ const routes = { downloadResourceRequests: { path: "/api/v1/resource/", method: "GET", + TRes: Type(), }, getResourceComments: { path: "/api/v1/resource/{id}/comment/",