From 6ebc0fe4e011e4e7ded27894e985947003bbfca5 Mon Sep 17 00:00:00 2001 From: Don Xavier Date: Wed, 12 Feb 2025 16:08:50 +0530 Subject: [PATCH] Add Deletion Functionality to Locations --- .../settings/locations/LocationList.tsx | 57 ++++++++++++++++- .../settings/locations/LocationView.tsx | 62 ++++++++++++++++++- .../locations/components/LocationCard.tsx | 10 ++- src/types/location/locationApi.ts | 5 ++ 4 files changed, 130 insertions(+), 4 deletions(-) diff --git a/src/pages/Facility/settings/locations/LocationList.tsx b/src/pages/Facility/settings/locations/LocationList.tsx index 4cb04e934ee..6b53b98a49b 100644 --- a/src/pages/Facility/settings/locations/LocationList.tsx +++ b/src/pages/Facility/settings/locations/LocationList.tsx @@ -1,6 +1,7 @@ -import { useQuery } from "@tanstack/react-query"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; import { useTranslation } from "react-i18next"; +import { toast } from "sonner"; import CareIcon from "@/CAREUI/icons/CareIcon"; @@ -8,9 +9,11 @@ import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; +import ConfirmDialog from "@/components/Common/ConfirmDialog"; import Pagination from "@/components/Common/Pagination"; import { CardGridSkeleton } from "@/components/Common/SkeletonLoading"; +import mutate from "@/Utils/request/mutate"; import query from "@/Utils/request/query"; import { LocationList as LocationListType } from "@/types/location/location"; import locationApi from "@/types/location/locationApi"; @@ -26,10 +29,12 @@ export default function LocationList({ facilityId }: Props) { const { t } = useTranslation(); const [page, setPage] = useState(1); const [searchQuery, setSearchQuery] = useState(""); + const [openDeleteDialog, setOpenDeleteDialog] = useState(false); const [selectedLocation, setSelectedLocation] = useState(null); const [isSheetOpen, setIsSheetOpen] = useState(false); const limit = 12; + const queryClient = useQueryClient(); const { data, isLoading } = useQuery({ queryKey: ["locations", facilityId, page, limit, searchQuery], @@ -53,6 +58,41 @@ export default function LocationList({ facilityId }: Props) { setSelectedLocation(location); setIsSheetOpen(true); }; + const { mutate: removeLocation } = useMutation({ + mutationFn: ({ + facilityId, + locationId, + }: { + facilityId: string; + locationId: string; + }) => + mutate(locationApi.delete, { + pathParams: { facility_id: facilityId, id: locationId }, + })({ facilityId, locationId }), + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ["locations", facilityId, page, limit, searchQuery], + }); + toast.success("Location removed successfully"); + }, + onError: (error) => { + const errorData = error.cause as { errors: { msg: string }[] }; + errorData.errors.forEach((er) => { + toast.error(er.msg); + }); + }, + }); + + const handleDeleteLocation = (location: LocationListType) => { + setSelectedLocation(location); + setOpenDeleteDialog(true); + }; + const confirmDelete = () => { + if (!selectedLocation) return; + removeLocation({ facilityId, locationId: selectedLocation.id }); + setOpenDeleteDialog(false); + setSelectedLocation(null); + }; const handleSheetClose = () => { setIsSheetOpen(false); @@ -61,6 +101,20 @@ export default function LocationList({ facilityId }: Props) { return (
+ + {t("are_you_sure_want_to_delete", { name: selectedLocation?.name })} + + } + action="Delete" + variant="destructive" + show={openDeleteDialog} + onClose={() => setOpenDeleteDialog(false)} + onConfirm={confirmDelete} + cancelLabel={t("cancel")} + />

{t("locations")}

@@ -95,6 +149,7 @@ export default function LocationList({ facilityId }: Props) { key={location.id} location={location} onEdit={handleEditLocation} + onDelete={handleDeleteLocation} /> )) ) : ( diff --git a/src/pages/Facility/settings/locations/LocationView.tsx b/src/pages/Facility/settings/locations/LocationView.tsx index 9bab726b813..86a83526301 100644 --- a/src/pages/Facility/settings/locations/LocationView.tsx +++ b/src/pages/Facility/settings/locations/LocationView.tsx @@ -1,7 +1,8 @@ -import { useQuery, useQueryClient } from "@tanstack/react-query"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { Link } from "raviger"; import { useState } from "react"; import { useTranslation } from "react-i18next"; +import { toast } from "sonner"; import CareIcon from "@/CAREUI/icons/CareIcon"; @@ -17,11 +18,13 @@ import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; +import ConfirmDialog from "@/components/Common/ConfirmDialog"; import Page from "@/components/Common/Page"; import Pagination from "@/components/Common/Pagination"; import { CardGridSkeleton } from "@/components/Common/SkeletonLoading"; import LinkDepartmentsSheet from "@/components/Patient/LinkDepartmentsSheet"; +import mutate from "@/Utils/request/mutate"; import query from "@/Utils/request/query"; import { LocationList, getLocationFormLabel } from "@/types/location/location"; import locationApi from "@/types/location/locationApi"; @@ -40,6 +43,7 @@ export default function LocationView({ id, facilityId }: Props) { const [page, setPage] = useState(1); const [searchQuery, setSearchQuery] = useState(""); + const [openDeleteDialog, setOpenDeleteDialog] = useState(false); const [selectedLocation, setSelectedLocation] = useState( null, ); @@ -92,6 +96,47 @@ export default function LocationView({ id, facilityId }: Props) { setIsSheetOpen(false); setSelectedLocation(null); }; + const { mutate: removeLocation } = useMutation({ + mutationFn: ({ + facilityId, + locationId, + }: { + facilityId: string; + locationId: string; + }) => + mutate(locationApi.delete, { + pathParams: { facility_id: facilityId, id: locationId }, + })({ facilityId, locationId }), + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: [ + "locations", + facilityId, + id, + "children", + { page, limit, searchQuery }, + ], + }); + toast.success("Location removed successfully"); + }, + onError: (error) => { + const errorData = error.cause as { errors: { msg: string }[] }; + errorData.errors.forEach((er) => { + toast.error(er.msg); + }); + }, + }); + + const handleDeleteLocation = (location: LocationList) => { + setSelectedLocation(location); + setOpenDeleteDialog(true); + }; + const confirmDelete = () => { + if (!selectedLocation) return; + removeLocation({ facilityId, locationId: selectedLocation.id }); + setOpenDeleteDialog(false); + setSelectedLocation(null); + }; if (!location) return ( @@ -115,6 +160,20 @@ export default function LocationView({ id, facilityId }: Props) { return ( <> + + {t("are_you_sure_want_to_delete", { name: selectedLocation?.name })} + + } + action="Delete" + variant="destructive" + show={openDeleteDialog} + onClose={() => setOpenDeleteDialog(false)} + onConfirm={confirmDelete} + cancelLabel={t("cancel")} + /> @@ -216,6 +275,7 @@ export default function LocationView({ id, facilityId }: Props) { key={childLocation.id} location={childLocation} onEdit={handleEditLocation} + onDelete={handleDeleteLocation} /> )) ) : ( diff --git a/src/pages/Facility/settings/locations/components/LocationCard.tsx b/src/pages/Facility/settings/locations/components/LocationCard.tsx index e12d031d6f2..6d69c34d4bf 100644 --- a/src/pages/Facility/settings/locations/components/LocationCard.tsx +++ b/src/pages/Facility/settings/locations/components/LocationCard.tsx @@ -17,6 +17,8 @@ import { useTranslation } from "react-i18next"; import { cn } from "@/lib/utils"; +import CareIcon from "@/CAREUI/icons/CareIcon"; + import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; @@ -26,10 +28,11 @@ import { LocationList, getLocationFormLabel } from "@/types/location/location"; interface Props { location: LocationList; onEdit?: (location: LocationList) => void; + onDelete?: (location: LocationList) => void; className?: string; } -export function LocationCard({ location, onEdit, className }: Props) { +export function LocationCard({ location, onEdit, onDelete, className }: Props) { const { t } = useTranslation(); const getLocationTypeIcon = (form: string) => { @@ -127,7 +130,10 @@ export function LocationCard({ location, onEdit, className }: Props) {
-
+
+