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

✨ Tags: Mark stall data after create/edit mutation #1098

Merged
merged 2 commits into from
Jul 11, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { AxiosError, AxiosResponse } from "axios";
import { object, string, mixed } from "yup";

import {
ActionGroup,
Button,
Expand Down
1 change: 0 additions & 1 deletion client/src/app/pages/controls/tags/tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useState } from "react";
import { AxiosError, AxiosResponse } from "axios";
import { useTranslation } from "react-i18next";
import { useSelectionState } from "@migtools/lib-ui";

import {
Button,
ButtonVariant,
Expand Down
14 changes: 7 additions & 7 deletions client/src/app/queries/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const useFetchTags = () => {

export const useFetchTagCategories = () => {
const { data, isLoading, error, refetch } = useQuery({
queryKey: [TagCategoriesQueryKey],
queryKey: [TagCategoriesQueryKey, TagsQueryKey],
queryFn: getTagCategories,
onError: (error: AxiosError) => console.log("error, ", error),
});
Expand All @@ -53,11 +53,11 @@ export const useCreateTagMutation = (
mutationFn: createTag,
onSuccess: (res) => {
onSuccess(res);
queryClient.invalidateQueries([TagsQueryKey]);
queryClient.invalidateQueries([TagCategoriesQueryKey, TagsQueryKey]);
Copy link
Member

Choose a reason for hiding this comment

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

I debugged through the react-query code -- this only invalidates the query that matches both keys.

Effectively, this change invalidates the useFetchTagCategories query now instead of the useFetchTags query.

Copy link
Member

Choose a reason for hiding this comment

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

AFAICT, the only way to invalidate 2 queries is to either:

  1. call invalidate twice with exactly matching queryKey, or
  2. setup multiple values in the initial queryKey and use one of those across all queries you want to invalidate at the same time.

For example:

export const useFetchTags = () => {
  const { data, isLoading, error, refetch } = useQuery({
    queryKey: ["allTags", TagsQueryKey],
    queryFn: getTags,
    onError: (error: AxiosError) => console.log("error, ", error),
  });
  return {
    tags: data || [],
    isFetching: isLoading,
    fetchError: error,
    refetch,
  };
};

export const useFetchTagCategories = () => {
  const { data, isLoading, error, refetch } = useQuery({
    queryKey: ["allTags", TagCategoriesQueryKey],
    queryFn: getTagCategories,
    onError: (error: AxiosError) => console.log("error, ", error),
  });
  return {
    tagCategories: data || [],
    isFetching: isLoading,
    fetchError: error,
    refetch,
  };
};

export const useCreateTagMutation = (
  onSuccess: (res: any) => void,
  onError: (err: AxiosError) => void
) => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: createTag,
    onSuccess: (res) => {
      onSuccess(res);
      queryClient.invalidateQueries(["allTags"]);
    },
    onError: (err: AxiosError) => {
      onError(err);
      queryClient.invalidateQueries(["allTags"]);
    },
  });
};

The extra "allTags" shouldn't do anything to the query itself other than make it easier to partial match on the invalidateQueries() call

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, option 2 here is what I meant above.

},
onError: (err: AxiosError) => {
onError(err);
queryClient.invalidateQueries([TagsQueryKey]);
queryClient.invalidateQueries([TagCategoriesQueryKey, TagsQueryKey]);
},
});
};
Expand Down Expand Up @@ -91,11 +91,11 @@ export const useUpdateTagMutation = (
mutationFn: updateTag,
onSuccess: (res) => {
onSuccess(res);
queryClient.invalidateQueries([TagsQueryKey]);
queryClient.invalidateQueries([TagCategoriesQueryKey, TagsQueryKey]);
},
onError: (err: AxiosError) => {
onError(err);
queryClient.invalidateQueries([TagsQueryKey]);
queryClient.invalidateQueries([TagCategoriesQueryKey, TagsQueryKey]);
},
});
};
Expand Down Expand Up @@ -128,11 +128,11 @@ export const useDeleteTagMutation = (
mutationFn: deleteTag,
onSuccess: (res) => {
onSuccess(res);
queryClient.invalidateQueries([TagsQueryKey]);
queryClient.invalidateQueries([TagCategoriesQueryKey, TagsQueryKey]);
},
onError: (err: AxiosError) => {
onError(err);
queryClient.invalidateQueries([TagsQueryKey]);
queryClient.invalidateQueries([TagCategoriesQueryKey, TagsQueryKey]);
},
});
};
Expand Down