♻️ Improve query invalidation (#659)

This commit is contained in:
Luke Vella 2023-04-08 12:43:54 +01:00 committed by GitHub
parent 0ad5abb590
commit 5b6d8424af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 26 deletions

View file

@ -25,23 +25,19 @@ interface CommentForm {
const Discussion: React.FunctionComponent = () => {
const { dayjs } = useDayjs();
const queryClient = trpc.useContext();
const { t } = useTranslation("app");
const { poll, admin } = usePoll();
const pollId = poll.id;
const { data: comments } = trpc.polls.comments.list.useQuery(
{ pollId },
{
refetchInterval: 10000, // refetch every 10 seconds
trpc: {},
},
);
const { data: comments } = trpc.polls.comments.list.useQuery({ pollId });
const posthog = usePostHog();
const queryClient = trpc.useContext();
const addComment = trpc.polls.comments.add.useMutation({
onSuccess: () => {
queryClient.polls.comments.invalidate();
posthog?.capture("created comment");
},
});

View file

@ -95,8 +95,10 @@ const ChangeNameModal = (props: {
onDone: () => void;
}) => {
const posthog = usePostHog();
const queryClient = trpc.useContext();
const changeName = trpc.polls.participants.rename.useMutation({
onSuccess: (_, { participantId, newName }) => {
queryClient.polls.participants.invalidate();
posthog?.capture("changed name", {
participantId,
oldName: props.oldName,

View file

@ -22,8 +22,10 @@ export const DeletePollForm: React.FunctionComponent<{
const confirmationText = watch("confirmation");
const canDelete = confirmationText === confirmText;
const posthog = usePostHog();
const queryClient = trpc.useContext();
const deletePoll = trpc.polls.delete.useMutation({
onSuccess: () => {
queryClient.polls.invalidate();
posthog?.capture("deleted poll");
},
});

View file

@ -16,8 +16,11 @@ export const normalizeVotes = (
export const useAddParticipantMutation = () => {
const posthog = usePostHog();
const queryClient = trpc.useContext();
return trpc.polls.participants.add.useMutation({
onSuccess: (_, { pollId, name, email }) => {
queryClient.polls.participants.list.invalidate({ pollId });
posthog?.capture("add participant", {
pollId,
name,

View file

@ -20,12 +20,18 @@ const NotificationsToggle: React.FunctionComponent = () => {
const watchers = data.watchers ?? [];
const { user } = useUser();
const isWatching = watchers.some(({ userId }) => userId === user.id);
const [isWatching, setIsWatching] = React.useState(() =>
watchers.some(({ userId }) => userId === user.id),
);
const posthog = usePostHog();
const watch = trpc.polls.watch.useMutation({
onMutate: () => {
setIsWatching(true);
},
onSuccess: () => {
// TODO (Luke Vella) [2023-04-08]: We should have a separate query for getting watchers
posthog?.capture("turned notifications on", {
pollId: poll.id,
source: "notifications-toggle",
@ -34,6 +40,9 @@ const NotificationsToggle: React.FunctionComponent = () => {
});
const unwatch = trpc.polls.unwatch.useMutation({
onMutate: () => {
setIsWatching(false);
},
onSuccess: () => {
posthog?.capture("turned notifications off", {
pollId: poll.id,
@ -42,7 +51,6 @@ const NotificationsToggle: React.FunctionComponent = () => {
},
});
const isUpdating = watch.isLoading || unwatch.isLoading;
const { openLoginModal } = useLoginModal();
return (
@ -59,7 +67,6 @@ const NotificationsToggle: React.FunctionComponent = () => {
>
<Button
data-testid="notifications-toggle"
loading={isUpdating}
disabled={poll.demo}
icon={isWatching ? <BellIcon /> : <BellCrossedIcon />}
onClick={async () => {

View file

@ -9,21 +9,6 @@ import { AppRouter } from "../../trpc/routers";
export * from "../../trpc/types";
export const trpc = createTRPCNext<AppRouter>({
unstable_overrides: {
useMutation: {
async onSuccess(opts) {
/**
* @note that order here matters:
* The order here allows route changes in `onSuccess` without
* having a flash of content change whilst redirecting.
**/
await opts.originalFn();
if (!opts.meta?.doNotInvalidate) {
await opts.queryClient.invalidateQueries();
}
},
},
},
config() {
return {
links: [