diff --git a/src/components/Patient/LinkDepartmentsSheet.tsx b/src/components/Patient/LinkDepartmentsSheet.tsx index d3f98027b06..4150702fdd9 100644 --- a/src/components/Patient/LinkDepartmentsSheet.tsx +++ b/src/components/Patient/LinkDepartmentsSheet.tsx @@ -84,25 +84,28 @@ function getInvalidateQueries( return ["location", entityId, "organizations"]; } -export default function LinkDepartmentsSheet({ +function DeleteOrganizationButton({ + organizationId, entityType, entityId, - currentOrganizations, facilityId, - trigger, - onUpdate, -}: Props) { - const [open, setOpen] = useState(false); - const [selectedOrg, setSelectedOrg] = useState(""); + onSuccess, +}: { + organizationId: string; + entityType: "encounter" | "location"; + entityId: string; + facilityId: string; + onSuccess?: () => void; +}) { const queryClient = useQueryClient(); - const { mutate: addOrganization, isPending: isAdding } = useMutation({ + const { mutate: removeOrganization, isPending } = useMutation({ mutationFn: (organizationId: string) => { const { route, pathParams } = getMutationParams( entityType, entityId, facilityId, - true, + false, ); return mutate(route, { pathParams, @@ -110,12 +113,15 @@ export default function LinkDepartmentsSheet({ })({ organization: organizationId }); }, onSuccess: () => { - const invalidateQueries = getInvalidateQueries(entityType, entityId); - queryClient.invalidateQueries({ queryKey: invalidateQueries }); - toast.success("Organization added successfully"); - setSelectedOrg(""); - setOpen(false); - onUpdate?.(); + const { queryKey } = getMutationParams( + entityType, + entityId, + facilityId, + false, + ); + queryClient.invalidateQueries({ queryKey }); + toast.success("Organization removed successfully"); + onSuccess?.(); }, onError: (error) => { const errorData = error.cause as { errors: { msg: string }[] }; @@ -125,13 +131,41 @@ export default function LinkDepartmentsSheet({ }, }); - const { mutate: removeOrganization, isPending: isRemoving } = useMutation({ + return ( + + ); +} + +export default function LinkDepartmentsSheet({ + entityType, + entityId, + currentOrganizations, + facilityId, + trigger, + onUpdate, +}: Props) { + const [open, setOpen] = useState(false); + const [selectedOrg, setSelectedOrg] = useState(""); + const queryClient = useQueryClient(); + + const { mutate: addOrganization, isPending: isAdding } = useMutation({ mutationFn: (organizationId: string) => { const { route, pathParams } = getMutationParams( entityType, entityId, facilityId, - false, + true, ); return mutate(route, { pathParams, @@ -139,14 +173,11 @@ export default function LinkDepartmentsSheet({ })({ organization: organizationId }); }, onSuccess: () => { - const { queryKey } = getMutationParams( - entityType, - entityId, - facilityId, - false, - ); - queryClient.invalidateQueries({ queryKey }); - toast.success("Organization removed successfully"); + const invalidateQueries = getInvalidateQueries(entityType, entityId); + queryClient.invalidateQueries({ queryKey: invalidateQueries }); + toast.success("Organization added successfully"); + setSelectedOrg(""); + setOpen(false); onUpdate?.(); }, onError: (error) => { @@ -215,18 +246,13 @@ export default function LinkDepartmentsSheet({ )} - + ))} {currentOrganizations.length === 0 && (