From b6400f4d4dea3c52e1493bce3fb5d179fd92ee1d Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Mon, 25 Dec 2023 16:07:01 +0530 Subject: [PATCH 1/5] fixes #6906; migrate Bed Management to `useQuery` --- src/Components/Facility/BedManagement.tsx | 127 ++++++---------------- src/Redux/api.tsx | 3 + 2 files changed, 37 insertions(+), 93 deletions(-) diff --git a/src/Components/Facility/BedManagement.tsx b/src/Components/Facility/BedManagement.tsx index 9a33f6141df..999786ba68b 100644 --- a/src/Components/Facility/BedManagement.tsx +++ b/src/Components/Facility/BedManagement.tsx @@ -1,14 +1,4 @@ -import { lazy, useCallback, useState } from "react"; - -import { useDispatch } from "react-redux"; -import { statusType, useAbortableEffect } from "../../Common/utils"; -import { - getAnyFacility, - getFacilityAssetLocation, - listFacilityBeds, - deleteFacilityBed, -} from "../../Redux/actions"; -import Pagination from "../Common/Pagination"; +import { lazy, useState } from "react"; import ButtonV2 from "../Common/components/ButtonV2"; import { BedModel } from "./models"; import { ReactElement } from "react"; @@ -18,6 +8,10 @@ import BedDeleteDialog from "./BedDeleteDialog"; import { NonReadOnlyUsers } from "../../Utils/AuthorizeFor"; import CareIcon from "../../CAREUI/icons/CareIcon"; import Page from "../Common/components/Page"; +import request from "../../Utils/request/request"; +import routes from "../../Redux/api"; +import useQuery from "../../Utils/request/useQuery"; +import useFilters from "../../Common/hooks/useFilters"; const Loading = lazy(() => import("../Common/Loading")); interface BedManagementProps { @@ -47,8 +41,6 @@ const BedRow = (props: BedRowProps) => { bedType, isOccupied, } = props; - - const dispatchAction: any = useDispatch(); const [bedData, setBedData] = useState<{ show: boolean; name: string; @@ -62,15 +54,11 @@ const BedRow = (props: BedRowProps) => { }; const handleDeleteConfirm = async () => { - const res = await dispatchAction(deleteFacilityBed(id)); - if (res?.status === 204) { - Notification.Success({ - msg: "Bed deleted successfully", - }); - } else { - Notification.Error({ - msg: "Error while deleting Bed: " + (res?.data?.detail || ""), - }); + const { res } = await request(routes.deleteFacilityBed, { + pathParams: { external_id: id }, + }); + if (res?.ok) { + Notification.Success({ msg: "Bed deleted successfully" }); } setBedData({ show: false, name: "" }); triggerRerender(); @@ -151,83 +139,41 @@ const BedRow = (props: BedRowProps) => { export const BedManagement = (props: BedManagementProps) => { const { facilityId, locationId } = props; - const dispatchAction: any = useDispatch(); - const [isLoading, setIsLoading] = useState(false); let bed: ReactElement | null = null; let BedList: ReactElement[] | ReactElement = []; - const [beds, setBeds] = useState([]); - const [offset, setOffset] = useState(0); - const [currentPage, setCurrentPage] = useState(1); - const [totalCount, setTotalCount] = useState(0); - const [rerender, setRerender] = useState(false); - const [facilityName, setFacilityName] = useState(""); - const [locationName, setLocationName] = useState(""); - const limit = 14; - - const triggerRerender = () => { - setRerender(!rerender); - }; - - const fetchData = useCallback( - async (status: statusType) => { - setIsLoading(true); - const facility = await dispatchAction(getAnyFacility(facilityId)); - - setFacilityName(facility?.data?.name || ""); + const { qParams, Pagination, resultsPerPage } = useFilters({}); - const location = await dispatchAction( - getFacilityAssetLocation(facilityId, locationId) - ); - - setLocationName(location?.data?.name || ""); - - const res = await dispatchAction( - listFacilityBeds({ - limit, - offset, - facility: facilityId, - location: locationId, - }) - ); - if (!status.aborted) { - if (res?.data) { - setBeds(res.data.results); - setTotalCount(res.data.count); - } - setIsLoading(false); - } + const { data: location } = useQuery(routes.getFacilityAssetLocation, { + pathParams: { + facility_external_id: facilityId, + external_id: locationId, }, - [dispatchAction, offset, rerender, facilityId, locationId] - ); - - useAbortableEffect( - (status: statusType) => { - fetchData(status); + }); + + const { loading, data, refetch } = useQuery(routes.listFacilityBeds, { + query: { + facility: facilityId, + location: locationId, + limit: resultsPerPage, + offset: (qParams.page ? qParams.page - 1 : 0) * resultsPerPage, }, - [fetchData] - ); - - const handlePagination = (page: number, limit: number) => { - const offset = (page - 1) * limit; - setCurrentPage(page); - setOffset(offset); - }; + }); - if (beds && beds.length) { - BedList = beds.map((bedItem: BedModel) => ( + if (data?.results.length) { + BedList = data.results.map((bedItem: BedModel) => ( )); - } else if (beds && beds.length === 0) { + } else if (data?.results.length === 0) { BedList = (

No beds available in this location @@ -235,25 +181,20 @@ export const BedManagement = (props: BedManagementProps) => { ); } - if (beds) { + if (data?.results.length) { bed = ( <>

{BedList}
- {totalCount > limit && ( + {data.count && (
- +
)} ); } - if (isLoading || !beds) { + if (loading) { return ; } @@ -261,9 +202,9 @@ export const BedManagement = (props: BedManagementProps) => { >(), }, createFacilityBed: { path: "/api/v1/bed/", @@ -409,6 +411,7 @@ const routes = { deleteFacilityBed: { path: "/api/v1/bed/{external_id}/", method: "DELETE", + TRes: Type>(), }, // Consultation beds From 44abc084bf4fc3bd1544be09a1e4f3ecd778b8b3 Mon Sep 17 00:00:00 2001 From: Mohammed Nihal <57055998+nihal467@users.noreply.github.com> Date: Mon, 25 Dec 2023 16:15:52 +0530 Subject: [PATCH 2/5] uncommented related test --- cypress/e2e/facility_spec/locations.cy.ts | 32 +++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/cypress/e2e/facility_spec/locations.cy.ts b/cypress/e2e/facility_spec/locations.cy.ts index 26364048ca2..aa39fa0e63c 100644 --- a/cypress/e2e/facility_spec/locations.cy.ts +++ b/cypress/e2e/facility_spec/locations.cy.ts @@ -3,14 +3,14 @@ import { AssetPage } from "../../pageobject/Asset/AssetCreation"; import { UserCreationPage } from "../../pageobject/Users/UserCreation"; import FacilityPage from "../../pageobject/Facility/FacilityCreation"; import FacilityLocation from "../../pageobject/Facility/FacilityLocation"; -// import { AssetPagination } from "../../pageobject/Asset/AssetPagination"; +import { AssetPagination } from "../../pageobject/Asset/AssetPagination"; describe("Location Management Section", () => { const assetPage = new AssetPage(); const userCreationPage = new UserCreationPage(); const facilityPage = new FacilityPage(); const facilityLocation = new FacilityLocation(); - // const assetPagination = new AssetPagination(); + const assetPagination = new AssetPagination(); const EXPECTED_LOCATION_ERROR_MESSAGES = [ "Name is required", "Location Type is required", @@ -35,7 +35,7 @@ describe("Location Management Section", () => { const bedModifiedDescrption = "test modified description"; const bedModifiedType = "Isolation"; const numberOfBeds = 10; - // const numberOfModifiedBeds = 25; + const numberOfModifiedBeds = 25; before(() => { cy.loginByApi("devdistrictadmin", "Coronasafe@123"); @@ -106,19 +106,19 @@ describe("Location Management Section", () => { facilityLocation.deleteBedRequest(); }); - // it("Add Multiple Bed to a facility location and verify pagination", () => { - // // bed creation - // facilityLocation.clickManageBedButton(); - // facilityLocation.clickAddBedButton(); - // facilityLocation.enterBedName(bedModifiedName); - // facilityLocation.enterBedDescription(bedModifiedDescrption); - // facilityLocation.selectBedType(bedModifiedType); - // facilityLocation.setMultipleBeds(numberOfModifiedBeds); - // assetPage.clickassetupdatebutton(); - // // pagination - // assetPagination.navigateToNextPage(); - // assetPagination.navigateToPreviousPage(); - // }); need to be unblocked upon issue #6906 is solved + it("Add Multiple Bed to a facility location and verify pagination", () => { + // bed creation + facilityLocation.clickManageBedButton(); + facilityLocation.clickAddBedButton(); + facilityLocation.enterBedName(bedModifiedName); + facilityLocation.enterBedDescription(bedModifiedDescrption); + facilityLocation.selectBedType(bedModifiedType); + facilityLocation.setMultipleBeds(numberOfModifiedBeds); + assetPage.clickassetupdatebutton(); + // pagination + assetPagination.navigateToNextPage(); + assetPagination.navigateToPreviousPage(); + }); it("Add Single Bed to a facility location and modify it", () => { // mandatory field verification in bed creation From 90210d3d40b8f971f8579eb0f77a95f53ee70a98 Mon Sep 17 00:00:00 2001 From: Mohammed Nihal <57055998+nihal467@users.noreply.github.com> Date: Thu, 28 Dec 2023 12:03:07 +0530 Subject: [PATCH 3/5] handle the auto-fill delay --- cypress/pageobject/Facility/FacilityCreation.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cypress/pageobject/Facility/FacilityCreation.ts b/cypress/pageobject/Facility/FacilityCreation.ts index e3639388620..1e1b9caae33 100644 --- a/cypress/pageobject/Facility/FacilityCreation.ts +++ b/cypress/pageobject/Facility/FacilityCreation.ts @@ -418,6 +418,7 @@ class FacilityPage { selectStateOnPincode(stateName) { this.getStateElement() .scrollIntoView() + .wait(2000) .should("be.visible") .then(($element) => { const text = $element.text(); @@ -431,6 +432,7 @@ class FacilityPage { selectDistrictOnPincode(districtName) { this.getDistrictElement() .scrollIntoView() + .wait(2000) .should("be.visible") .then(($element) => { const text = $element.text(); From d4ce655e6e1a16453344e7045b9e98272475c33b Mon Sep 17 00:00:00 2001 From: Mohammed Nihal <57055998+nihal467@users.noreply.github.com> Date: Thu, 28 Dec 2023 12:44:09 +0530 Subject: [PATCH 4/5] handle the google map delay --- cypress/pageobject/Facility/FacilityCreation.ts | 1 + vite.config.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cypress/pageobject/Facility/FacilityCreation.ts b/cypress/pageobject/Facility/FacilityCreation.ts index 1e1b9caae33..2d6aa9ff375 100644 --- a/cypress/pageobject/Facility/FacilityCreation.ts +++ b/cypress/pageobject/Facility/FacilityCreation.ts @@ -299,6 +299,7 @@ class FacilityPage { cy.intercept("https://maps.googleapis.com/maps/api/mapsjs/*").as("mapApi"); cy.wait("@mapApi").its("response.statusCode").should("eq", 200); cy.get("input#pac-input").type(location).type("{enter}"); + cy.wait(2000); cy.get("div#map-close").click(); } diff --git a/vite.config.ts b/vite.config.ts index ecd6a2676d3..561ea4a5c81 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -85,7 +85,7 @@ export default defineConfig({ port: 4000, proxy: { "/api": { - target: process.env.CARE_API ?? "https://careapi.ohc.network", + target: process.env.CARE_API ?? "http://192.168.0.203:9000/", changeOrigin: true, }, }, @@ -94,7 +94,7 @@ export default defineConfig({ port: 4000, proxy: { "/api": { - target: process.env.CARE_API ?? "https://careapi.ohc.network", + target: process.env.CARE_API ?? "http://192.168.0.203:9000/", changeOrigin: true, }, }, From 671292ba60949dd7e45229bd9cc85e736f7c0fda Mon Sep 17 00:00:00 2001 From: Mohammed Nihal <57055998+nihal467@users.noreply.github.com> Date: Thu, 28 Dec 2023 12:45:29 +0530 Subject: [PATCH 5/5] revert vite --- vite.config.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index 561ea4a5c81..ecd6a2676d3 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -85,7 +85,7 @@ export default defineConfig({ port: 4000, proxy: { "/api": { - target: process.env.CARE_API ?? "http://192.168.0.203:9000/", + target: process.env.CARE_API ?? "https://careapi.ohc.network", changeOrigin: true, }, }, @@ -94,7 +94,7 @@ export default defineConfig({ port: 4000, proxy: { "/api": { - target: process.env.CARE_API ?? "http://192.168.0.203:9000/", + target: process.env.CARE_API ?? "https://careapi.ohc.network", changeOrigin: true, }, },