eletrotupi / tcc/ commit / 7f3f9fe

frontend: invalid insights cache when adding a mood

Added a bit of timeout there, since the insights are processed in a
queue
Pedro Lucas Porcellis porcellis@eletrotupi.com 1 month ago 7f3f9feffb8681ec40c59529d36f91db5315dc19
Parents: bf66e80
1 file(s) changed
  • frontend/hooks/useMoodEntries.queries.ts +14 -0
frontend/hooks/useMoodEntries.queries.ts
1 1
import {
2 2
  useQuery,
3 3
  useMutation,
4 4
  useQueryClient,
5 5
  useInfiniteQuery,
6 6
} from "@tanstack/react-query";
7 7

8 8
import { apiClient, MoodEntry, CreateMoodEntryPayload } from "@/lib/api";
9 +
import { insightKeys } from "./useInsights.queries";
9 10

10 11
export const moodKeys = {
11 12
  all: () => ["mood-entries"] as const,
12 13
  lists: () => [...moodKeys.all(), "list"] as const,
13 14
  list: (filters?: MoodEntryFilters) => [...moodKeys.lists(), filters] as const,
▸ 79 unchanged lines
93 94
    },
94 95

95 96
    // On success, replace optimistic record with real one from server
96 97
    onSuccess: () => {
97 98
      queryClient.invalidateQueries({ queryKey: moodKeys.lists() });
99 +

100 +
      // Invalidate insights, so we can force refresh
101 +
      setTimeout(() => {
102 +
        queryClient.invalidateQueries({
103 +
          queryKey: insightKeys.byType("DAILY_ENERGY"),
104 +
        });
105 +
        queryClient.invalidateQueries({
106 +
          queryKey: insightKeys.byType("MOOD_TREND"),
107 +
        });
108 +
        queryClient.invalidateQueries({
109 +
          queryKey: insightKeys.byType("ENERGY_SLEEP_CORRELATION"),
110 +
        });
111 +
      }, 2000);
98 112
    },
99 113
  });
100 114
};
101 115

102 116
// Delete a mood entry with optimistic removal
▸ 23 unchanged lines
126 140
    onSuccess: () => {
127 141
      queryClient.invalidateQueries({ queryKey: moodKeys.lists() });
128 142
    },
129 143
  });
130 144
};