diff --git a/public/locale/en.json b/public/locale/en.json index dffc0093acc..719d762361e 100644 --- a/public/locale/en.json +++ b/public/locale/en.json @@ -1427,6 +1427,7 @@ "location_history": "Location History", "location_management": "Location Management", "location_name": "Location Name", + "location_removed_successfully": "Location removed successfully", "location_status": "Location Status", "location_status__active": "Active", "location_status__inactive": "Inactive", @@ -1901,8 +1902,8 @@ "preset_name_placeholder": "Specify an identifiable name for the new preset", "preset_updated": "Preset updated", "prev_sessions": "Prev Sessions", - "preview_bed_names": "Preview Bed Names", "preview": "Preview", + "preview_bed_names": "Preview Bed Names", "preview_form": "Preview form", "previous": "Previous", "primary_ph_no": "Primary Ph No.", diff --git a/src/pages/Facility/settings/locations/LocationList.tsx b/src/pages/Facility/settings/locations/LocationList.tsx index ccff4dd76ee..72ea418df2a 100644 --- a/src/pages/Facility/settings/locations/LocationList.tsx +++ b/src/pages/Facility/settings/locations/LocationList.tsx @@ -292,6 +292,7 @@ export default function LocationList({ facilityId }: Props) { getChildren={getChildren} handleEditLocation={handleEditLocation} setExpandedRows={setExpandedRows} + facilityId={facilityId} /> ) : ( diff --git a/src/pages/Facility/settings/locations/LocationView.tsx b/src/pages/Facility/settings/locations/LocationView.tsx index 52685711fa0..b43fe61f036 100644 --- a/src/pages/Facility/settings/locations/LocationView.tsx +++ b/src/pages/Facility/settings/locations/LocationView.tsx @@ -216,6 +216,7 @@ export default function LocationView({ id, facilityId }: Props) { key={childLocation.id} location={childLocation} onEdit={handleEditLocation} + facilityId={facilityId} /> )) ) : ( diff --git a/src/pages/Facility/settings/locations/components/LocationCard.tsx b/src/pages/Facility/settings/locations/components/LocationCard.tsx index fcb784f0178..cf256f7d157 100644 --- a/src/pages/Facility/settings/locations/components/LocationCard.tsx +++ b/src/pages/Facility/settings/locations/components/LocationCard.tsx @@ -1,23 +1,59 @@ +import { useMutation, useQueryClient } from "@tanstack/react-query"; import { ChevronRight, Folder, FolderOpen, PenLine } from "lucide-react"; import { Link } from "raviger"; import { useTranslation } from "react-i18next"; +import { toast } from "sonner"; import { cn } from "@/lib/utils"; +import CareIcon from "@/CAREUI/icons/CareIcon"; + +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from "@/components/ui/alert-dialog"; import { Badge } from "@/components/ui/badge"; -import { Button } from "@/components/ui/button"; +import { Button, buttonVariants } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; +import mutate from "@/Utils/request/mutate"; import { LocationList, LocationTypeIcons } from "@/types/location/location"; +import locationApi from "@/types/location/locationApi"; interface Props { location: LocationList; onEdit?: (location: LocationList) => void; className?: string; + facilityId: string; } -export function LocationCard({ location, onEdit, className }: Props) { +export function LocationCard({ + location, + onEdit, + className, + facilityId, +}: Props) { const { t } = useTranslation(); + const queryClient = useQueryClient(); + + const { mutate: removeLocation } = useMutation({ + mutationFn: mutate(locationApi.delete, { + pathParams: { facility_id: facilityId, id: location.id }, + }), + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ["locations", facilityId], + }); + toast.success(t("location_removed_successfully")); + }, + }); const Icon = LocationTypeIcons[location.form as keyof typeof LocationTypeIcons] || Folder; @@ -80,16 +116,61 @@ export function LocationCard({ location, onEdit, className }: Props) {
-
- +
+ {!location.has_children && !location.current_encounter && ( +
+ + + + + + + + {t("remove")} {location.name} + + + {t("are_you_sure_want_to_delete", { + name: location.name, + })} + + + + {t("cancel")} + + removeLocation({ + pathParams: { + facility_id: facilityId, + id: location.id, + }, + }) + } + className={cn( + buttonVariants({ variant: "destructive" }), + )} + > + {t("remove")} + + + + +
+ )} +
+ +
diff --git a/src/pages/Facility/settings/locations/components/LocationListView.tsx b/src/pages/Facility/settings/locations/components/LocationListView.tsx index 30a30b22699..f302831563e 100644 --- a/src/pages/Facility/settings/locations/components/LocationListView.tsx +++ b/src/pages/Facility/settings/locations/components/LocationListView.tsx @@ -1,10 +1,25 @@ +import { useMutation, useQueryClient } from "@tanstack/react-query"; import { PenLine } from "lucide-react"; import { Link } from "raviger"; import { useTranslation } from "react-i18next"; +import { toast } from "sonner"; + +import { cn } from "@/lib/utils"; import CareIcon from "@/CAREUI/icons/CareIcon"; -import { Button } from "@/components/ui/button"; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from "@/components/ui/alert-dialog"; +import { Button, buttonVariants } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Table, @@ -19,7 +34,9 @@ import { TableSkeleton } from "@/components/Common/SkeletonLoading"; import useBreakpoints from "@/hooks/useBreakpoints"; +import mutate from "@/Utils/request/mutate"; import { LocationList as LocationListType } from "@/types/location/location"; +import locationApi from "@/types/location/locationApi"; interface LocationRowProps { location: LocationListType; @@ -32,6 +49,7 @@ interface LocationRowProps { React.SetStateAction> >; displayExpandAll?: boolean; + facilityId: string; } function LocationRow({ @@ -43,8 +61,10 @@ function LocationRow({ onEdit, setExpandedRows, displayExpandAll = true, + facilityId, }: LocationRowProps) { const { t } = useTranslation(); + const queryClient = useQueryClient(); const isMobile = useBreakpoints({ default: true, sm: false }); const children = getChildren(location.id); const isTopLevel = @@ -68,6 +88,17 @@ function LocationRow({ return newExpandedRows; }); }; + const { mutate: removeLocation } = useMutation({ + mutationFn: mutate(locationApi.delete, { + pathParams: { facility_id: facilityId, id: location.id }, + }), + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ["locations", facilityId], + }); + toast.success(t("location_removed_successfully")); + }, + }); const allExpanded = children.every((child) => expandedRows[child.id]); @@ -110,7 +141,7 @@ function LocationRow({ {isTopLevel && (
-
+
{children.length > 0 && displayExpandAll && (
+
+ {!location.has_children && !location.current_encounter ? ( + + + + + + + + {t("remove")} {location.name} + + + {t("are_you_sure_want_to_delete", { + name: location.name, + })} + + + + {t("cancel")} + + removeLocation({ + pathParams: { + facility_id: facilityId, + id: location.id, + }, + }) + } + className={cn( + buttonVariants({ variant: "destructive" }), + )} + > + {t("remove")} + + + + + ) : null} +