♻️ Remove explicit invalidate calls (#1445)

This commit is contained in:
Luke Vella 2024-11-24 15:08:29 +00:00 committed by GitHub
parent 4b3376536e
commit a34aa8c465
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 16 additions and 46 deletions

View file

@ -33,12 +33,10 @@ export function DeleteAccountDialog({
}, },
}); });
const { t } = useTranslation(); const { t } = useTranslation();
const trpcUtils = trpc.useUtils();
const posthog = usePostHog(); const posthog = usePostHog();
const deleteAccount = trpc.user.delete.useMutation({ const deleteAccount = trpc.user.delete.useMutation({
onSuccess() { onSuccess() {
posthog?.capture("delete account"); posthog?.capture("delete account");
trpcUtils.invalidate();
signOut({ signOut({
callbackUrl: "/login", callbackUrl: "/login",
}); });

View file

@ -44,7 +44,6 @@ export const RegisterForm = () => {
}); });
const { handleSubmit, control, getValues, setError, formState } = form; const { handleSubmit, control, getValues, setError, formState } = form;
const queryClient = trpc.useUtils();
const requestRegistration = trpc.auth.requestRegistration.useMutation(); const requestRegistration = trpc.auth.requestRegistration.useMutation();
const authenticateRegistration = const authenticateRegistration =
trpc.auth.authenticateRegistration.useMutation(); trpc.auth.authenticateRegistration.useMutation();
@ -68,7 +67,6 @@ export const RegisterForm = () => {
throw new Error("Failed to authenticate user"); throw new Error("Failed to authenticate user");
} }
queryClient.invalidate();
posthog?.identify(res.user.id, { posthog?.identify(res.user.id, {
email: res.user.email, email: res.user.email,

View file

@ -16,7 +16,6 @@ type PageProps = { magicLink: string; email: string };
export const LoginPage = ({ magicLink, email }: PageProps) => { export const LoginPage = ({ magicLink, email }: PageProps) => {
const session = useSession(); const session = useSession();
const posthog = usePostHog(); const posthog = usePostHog();
const trpcUtils = trpc.useUtils();
const magicLinkFetch = useMutation({ const magicLinkFetch = useMutation({
mutationFn: async () => { mutationFn: async () => {
const res = await fetch(magicLink); const res = await fetch(magicLink);
@ -32,8 +31,6 @@ export const LoginPage = ({ magicLink, email }: PageProps) => {
email: updatedSession.user.email, email: updatedSession.user.email,
name: updatedSession.user.name, name: updatedSession.user.name,
}); });
await trpcUtils.invalidate();
} }
} }

View file

@ -23,7 +23,6 @@ export function DuplicateDialog({
pollTitle, pollTitle,
...props ...props
}: DialogProps & { pollId: string; pollTitle: string }) { }: DialogProps & { pollId: string; pollTitle: string }) {
const queryClient = trpc.useUtils();
const duplicate = trpc.polls.duplicate.useMutation(); const duplicate = trpc.polls.duplicate.useMutation();
const posthog = usePostHog(); const posthog = usePostHog();
const router = useRouter(); const router = useRouter();
@ -52,7 +51,6 @@ export function DuplicateDialog({
pollId, pollId,
newPollId: res.id, newPollId: res.id,
}); });
queryClient.invalidate();
router.push(`/poll/${res.id}`); router.push(`/poll/${res.id}`);
}, },
}, },

View file

@ -7,11 +7,9 @@ import { useForm } from "react-hook-form";
import { requiredString } from "../../utils/form-validation"; import { requiredString } from "../../utils/form-validation";
export const verifyCode = async (options: { email: string; token: string }) => { export const verifyCode = async (options: { email: string; token: string }) => {
const url = `${ const url = `${window.location.origin
window.location.origin }/api/auth/callback/email?email=${encodeURIComponent(options.email)}&token=${options.token
}/api/auth/callback/email?email=${encodeURIComponent(options.email)}&token=${ }`;
options.token
}`;
const res = await fetch(url); const res = await fetch(url);

View file

@ -64,7 +64,6 @@ export const CreatePoll: React.FunctionComponent = () => {
useUnmount(clear); useUnmount(clear);
const posthog = usePostHog(); const posthog = usePostHog();
const queryClient = trpc.useUtils();
const createPoll = trpc.polls.create.useMutation({ const createPoll = trpc.polls.create.useMutation({
networkMode: "always", networkMode: "always",
onSuccess: () => { onSuccess: () => {
@ -104,7 +103,6 @@ export const CreatePoll: React.FunctionComponent = () => {
last_poll_created_at: new Date().toISOString(), last_poll_created_at: new Date().toISOString(),
}, },
}); });
queryClient.invalidate();
router.push(`/poll/${res.id}`); router.push(`/poll/${res.id}`);
}, },
}, },

View file

@ -73,7 +73,6 @@ function NewCommentForm({
const posthog = usePostHog(); const posthog = usePostHog();
const queryClient = trpc.useUtils();
const session = useUser(); const session = useUser();
@ -88,7 +87,6 @@ function NewCommentForm({
const addComment = trpc.polls.comments.add.useMutation({ const addComment = trpc.polls.comments.add.useMutation({
onSuccess: () => { onSuccess: () => {
queryClient.polls.comments.invalidate();
posthog?.capture("created comment"); posthog?.capture("created comment");
}, },
onError: (error) => { onError: (error) => {
@ -166,9 +164,6 @@ function DiscussionInner() {
const { data: comments } = trpc.polls.comments.list.useQuery( const { data: comments } = trpc.polls.comments.list.useQuery(
{ pollId }, { pollId },
{
staleTime: 1000 * 5,
},
); );
const posthog = usePostHog(); const posthog = usePostHog();

View file

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

View file

@ -51,7 +51,6 @@ function PauseResumeToggle() {
status: "live", status: "live",
}; };
}); });
queryClient.polls.invalidate();
}, },
}); });
const pause = trpc.polls.pause.useMutation({ const pause = trpc.polls.pause.useMutation({
@ -63,7 +62,6 @@ function PauseResumeToggle() {
status: "paused", status: "paused",
}; };
}); });
queryClient.polls.invalidate();
}, },
}); });
@ -139,9 +137,6 @@ const ManagePoll: React.FunctionComponent<{
}; };
}); });
}, },
onSuccess: () => {
queryClient.polls.invalidate();
},
}); });
const [showDeletePollDialog, setShowDeletePollDialog] = React.useState(false); const [showDeletePollDialog, setShowDeletePollDialog] = React.useState(false);

View file

@ -19,11 +19,9 @@ export const DeletePollDialog: React.FunctionComponent<{
urlId: string; urlId: string;
}> = ({ open, onOpenChange, urlId }) => { }> = ({ open, onOpenChange, urlId }) => {
const posthog = usePostHog(); const posthog = usePostHog();
const queryClient = trpc.useUtils();
const router = useRouter(); const router = useRouter();
const deletePoll = trpc.polls.delete.useMutation({ const deletePoll = trpc.polls.delete.useMutation({
onSuccess: () => { onSuccess: () => {
queryClient.polls.invalidate();
posthog?.capture("deleted poll"); posthog?.capture("deleted poll");
onOpenChange(false); onOpenChange(false);
router.replace("/polls"); router.replace("/polls");

View file

@ -212,12 +212,7 @@ export const FinalizePollForm = ({
export function FinalizePollDialog(props: DialogProps) { export function FinalizePollDialog(props: DialogProps) {
const poll = usePoll(); const poll = usePoll();
const queryClient = trpc.useUtils(); const scheduleEvent = trpc.polls.book.useMutation();
const scheduleEvent = trpc.polls.book.useMutation({
onSuccess: () => {
queryClient.invalidate();
},
});
return ( return (
<Dialog {...props}> <Dialog {...props}>
<DialogContent size="2xl"> <DialogContent size="2xl">

View file

@ -31,7 +31,6 @@ export const useAddParticipantMutation = () => {
]; ];
}, },
); );
queryClient.polls.participants.list.invalidate({ pollId });
posthog?.capture("add participant", { posthog?.capture("add participant", {
pollId, pollId,
name, name,
@ -83,7 +82,6 @@ export const useDeleteParticipantMutation = () => {
); );
}, },
onSuccess: (_, { participantId }) => { onSuccess: (_, { participantId }) => {
queryClient.polls.participants.list.invalidate({ pollId: poll.id });
posthog?.capture("remove participant", { posthog?.capture("remove participant", {
pollId: poll.id, pollId: poll.id,
participantId, participantId,
@ -93,11 +91,9 @@ export const useDeleteParticipantMutation = () => {
}; };
export const useUpdatePollMutation = () => { export const useUpdatePollMutation = () => {
const queryClient = trpc.useUtils();
const posthog = usePostHog(); const posthog = usePostHog();
return trpc.polls.update.useMutation({ return trpc.polls.update.useMutation({
onSuccess: (_data, { urlId }) => { onSuccess: (_data, { urlId }) => {
queryClient.polls.invalidate();
posthog?.capture("updated poll", { posthog?.capture("updated poll", {
id: urlId, id: urlId,
}); });

View file

@ -50,7 +50,6 @@ const NotificationsToggle: React.FunctionComponent = () => {
return [...oldWatchers, { userId: user.id }]; return [...oldWatchers, { userId: user.id }];
}, },
); );
queryClient.polls.invalidate();
}, },
}); });
@ -69,7 +68,6 @@ const NotificationsToggle: React.FunctionComponent = () => {
return oldWatchers.filter(({ userId }) => userId !== user.id); return oldWatchers.filter(({ userId }) => userId !== user.id);
}, },
); );
queryClient.polls.invalidate();
}, },
}); });
@ -126,11 +124,11 @@ const NotificationsToggle: React.FunctionComponent = () => {
values={{ values={{
value: isWatching value: isWatching
? t("notificationsOn", { ? t("notificationsOn", {
defaultValue: "On", defaultValue: "On",
}) })
: t("notificationsOff", { : t("notificationsOff", {
defaultValue: "Off", defaultValue: "Off",
}), }),
}} }}
/> />
)} )}

View file

@ -7,4 +7,12 @@ export const trpc = createTRPCNext<AppRouter>({
config() { config() {
return trpcConfig; return trpcConfig;
}, },
unstable_overrides: {
useMutation: {
async onSuccess(opts) {
await opts.originalFn();
await opts.queryClient.invalidateQueries();
},
},
},
}); });