Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Deletion Functionality to Locations #10571

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.",
Expand Down
1 change: 1 addition & 0 deletions src/pages/Facility/settings/locations/LocationView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export default function LocationView({ id, facilityId }: Props) {
key={childLocation.id}
location={childLocation}
onEdit={handleEditLocation}
facilityId={facilityId}
/>
))
) : (
Expand Down
105 changes: 93 additions & 12 deletions src/pages/Facility/settings/locations/components/LocationCard.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -80,16 +116,61 @@ export function LocationCard({ location, onEdit, className }: Props) {
</div>

<div className="mt-auto border-t border-gray-100 bg-gray-50 p-4">
<div className="flex justify-end">
<Button variant="outline" asChild>
<Link
href={`/location/${location.id}`}
className="flex items-center gap-2"
>
{t("view_details")}
<ChevronRight className="h-4 w-4" />
</Link>
</Button>
<div className="flex justify-between">
{!location.has_children && !location.current_encounter && (
<div>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
className={cn(buttonVariants({ variant: "white" }))}
>
<CareIcon icon="l-trash" />
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
{t("remove")} {location.name}
</AlertDialogTitle>
<AlertDialogDescription>
{t("are_you_sure_want_to_delete", {
name: location.name,
})}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{t("cancel")}</AlertDialogCancel>
<AlertDialogAction
onClick={() =>
removeLocation({
pathParams: {
facility_id: facilityId,
id: location.id,
},
})
}
className={cn(
buttonVariants({ variant: "destructive" }),
)}
>
{t("remove")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
)}
<div className="ml-auto">
<Button variant="outline" asChild>
<Link
href={`/location/${location.id}`}
className="flex items-center gap-2"
>
{t("view_details")}
<ChevronRight className="h-4 w-4" />
</Link>
</Button>
</div>
</div>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/types/location/locationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export default {
TRes: Type<LocationDetail>(),
TBody: Type<LocationWrite>(),
},
delete: {
path: "/api/v1/facility/{facility_id}/location/{id}/",
method: HttpMethod.DELETE,
TRes: Type<void>(),
},
getOrganizations: {
path: "/api/v1/facility/{facility_id}/location/{id}/organizations",
method: HttpMethod.GET,
Expand Down
Loading