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

Fix : Button Refreshes Entire Organizations #10978

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions src/components/Patient/LinkDepartmentsSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export default function LinkDepartmentsSheet({
const [open, setOpen] = useState(false);
const [selectedOrg, setSelectedOrg] = useState<string>("");
const queryClient = useQueryClient();
const [removingOrgId, setRemovingOrgId] = useState<string | null>(null);

const { mutate: addOrganization, isPending: isAdding } = useMutation({
mutationFn: (organizationId: string) => {
Expand Down Expand Up @@ -125,8 +126,9 @@ export default function LinkDepartmentsSheet({
},
});

const { mutate: removeOrganization, isPending: isRemoving } = useMutation({
const { mutate: removeOrganization } = useMutation({
mutationFn: (organizationId: string) => {
setRemovingOrgId(organizationId);
const { route, pathParams } = getMutationParams(
entityType,
entityId,
Expand All @@ -147,9 +149,11 @@ export default function LinkDepartmentsSheet({
);
queryClient.invalidateQueries({ queryKey });
toast.success("Organization removed successfully");
setRemovingOrgId(null);
onUpdate?.();
},
onError: (error) => {
setRemovingOrgId(null);
const errorData = error.cause as { errors: { msg: string }[] };
errorData.errors.forEach((er) => {
toast.error(er.msg);
Expand Down Expand Up @@ -219,9 +223,9 @@ export default function LinkDepartmentsSheet({
variant="ghost"
size="icon"
onClick={() => removeOrganization(org.id)}
disabled={isRemoving}
disabled={removingOrgId === org.id}
>
{isRemoving ? (
{removingOrgId === org.id ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<Trash2 className="h-4 w-4 text-destructive" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd say wrap this button in a component and keep the useMutation there in that component, and use useMutation's isPending to show the loading state instead of creating a new state here;

Expand Down
Loading