mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-22 13:26:20 +02:00
♻️ Improve query invalidation (#659)
This commit is contained in:
parent
0ad5abb590
commit
5b6d8424af
6 changed files with 21 additions and 26 deletions
|
@ -25,23 +25,19 @@ interface CommentForm {
|
||||||
|
|
||||||
const Discussion: React.FunctionComponent = () => {
|
const Discussion: React.FunctionComponent = () => {
|
||||||
const { dayjs } = useDayjs();
|
const { dayjs } = useDayjs();
|
||||||
const queryClient = trpc.useContext();
|
|
||||||
const { t } = useTranslation("app");
|
const { t } = useTranslation("app");
|
||||||
const { poll, admin } = usePoll();
|
const { poll, admin } = usePoll();
|
||||||
|
|
||||||
const pollId = poll.id;
|
const pollId = poll.id;
|
||||||
|
|
||||||
const { data: comments } = trpc.polls.comments.list.useQuery(
|
const { data: comments } = trpc.polls.comments.list.useQuery({ pollId });
|
||||||
{ pollId },
|
|
||||||
{
|
|
||||||
refetchInterval: 10000, // refetch every 10 seconds
|
|
||||||
trpc: {},
|
|
||||||
},
|
|
||||||
);
|
|
||||||
const posthog = usePostHog();
|
const posthog = usePostHog();
|
||||||
|
|
||||||
|
const queryClient = trpc.useContext();
|
||||||
|
|
||||||
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");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -95,8 +95,10 @@ const ChangeNameModal = (props: {
|
||||||
onDone: () => void;
|
onDone: () => void;
|
||||||
}) => {
|
}) => {
|
||||||
const posthog = usePostHog();
|
const posthog = usePostHog();
|
||||||
|
const queryClient = trpc.useContext();
|
||||||
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,
|
||||||
|
|
|
@ -22,8 +22,10 @@ export const DeletePollForm: React.FunctionComponent<{
|
||||||
const confirmationText = watch("confirmation");
|
const confirmationText = watch("confirmation");
|
||||||
const canDelete = confirmationText === confirmText;
|
const canDelete = confirmationText === confirmText;
|
||||||
const posthog = usePostHog();
|
const posthog = usePostHog();
|
||||||
|
const queryClient = trpc.useContext();
|
||||||
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");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,8 +16,11 @@ export const normalizeVotes = (
|
||||||
|
|
||||||
export const useAddParticipantMutation = () => {
|
export const useAddParticipantMutation = () => {
|
||||||
const posthog = usePostHog();
|
const posthog = usePostHog();
|
||||||
|
const queryClient = trpc.useContext();
|
||||||
|
|
||||||
return trpc.polls.participants.add.useMutation({
|
return trpc.polls.participants.add.useMutation({
|
||||||
onSuccess: (_, { pollId, name, email }) => {
|
onSuccess: (_, { pollId, name, email }) => {
|
||||||
|
queryClient.polls.participants.list.invalidate({ pollId });
|
||||||
posthog?.capture("add participant", {
|
posthog?.capture("add participant", {
|
||||||
pollId,
|
pollId,
|
||||||
name,
|
name,
|
||||||
|
|
|
@ -20,12 +20,18 @@ const NotificationsToggle: React.FunctionComponent = () => {
|
||||||
const watchers = data.watchers ?? [];
|
const watchers = data.watchers ?? [];
|
||||||
|
|
||||||
const { user } = useUser();
|
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 posthog = usePostHog();
|
||||||
|
|
||||||
const watch = trpc.polls.watch.useMutation({
|
const watch = trpc.polls.watch.useMutation({
|
||||||
|
onMutate: () => {
|
||||||
|
setIsWatching(true);
|
||||||
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
|
// TODO (Luke Vella) [2023-04-08]: We should have a separate query for getting watchers
|
||||||
posthog?.capture("turned notifications on", {
|
posthog?.capture("turned notifications on", {
|
||||||
pollId: poll.id,
|
pollId: poll.id,
|
||||||
source: "notifications-toggle",
|
source: "notifications-toggle",
|
||||||
|
@ -34,6 +40,9 @@ const NotificationsToggle: React.FunctionComponent = () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const unwatch = trpc.polls.unwatch.useMutation({
|
const unwatch = trpc.polls.unwatch.useMutation({
|
||||||
|
onMutate: () => {
|
||||||
|
setIsWatching(false);
|
||||||
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
posthog?.capture("turned notifications off", {
|
posthog?.capture("turned notifications off", {
|
||||||
pollId: poll.id,
|
pollId: poll.id,
|
||||||
|
@ -42,7 +51,6 @@ const NotificationsToggle: React.FunctionComponent = () => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const isUpdating = watch.isLoading || unwatch.isLoading;
|
|
||||||
const { openLoginModal } = useLoginModal();
|
const { openLoginModal } = useLoginModal();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -59,7 +67,6 @@ const NotificationsToggle: React.FunctionComponent = () => {
|
||||||
>
|
>
|
||||||
<Button
|
<Button
|
||||||
data-testid="notifications-toggle"
|
data-testid="notifications-toggle"
|
||||||
loading={isUpdating}
|
|
||||||
disabled={poll.demo}
|
disabled={poll.demo}
|
||||||
icon={isWatching ? <BellIcon /> : <BellCrossedIcon />}
|
icon={isWatching ? <BellIcon /> : <BellCrossedIcon />}
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
|
|
|
@ -9,21 +9,6 @@ import { AppRouter } from "../../trpc/routers";
|
||||||
export * from "../../trpc/types";
|
export * from "../../trpc/types";
|
||||||
|
|
||||||
export const trpc = createTRPCNext<AppRouter>({
|
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() {
|
config() {
|
||||||
return {
|
return {
|
||||||
links: [
|
links: [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue