From 29a0120e355a4785094301eaf23fea5dcc0752c7 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Thu, 3 Oct 2024 11:39:56 +0200 Subject: [PATCH] vinvoor: invalidate queries when changing settings --- vinvoor/src/hooks/useSeasons.ts | 4 ++-- vinvoor/src/hooks/useSettings.ts | 13 ++++++++++--- vinvoor/src/settings/Settings.tsx | 5 ++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/vinvoor/src/hooks/useSeasons.ts b/vinvoor/src/hooks/useSeasons.ts index dca85db..f5221a7 100644 --- a/vinvoor/src/hooks/useSeasons.ts +++ b/vinvoor/src/hooks/useSeasons.ts @@ -14,7 +14,7 @@ export const useSeasons = () => { }; export const useSetSeason = () => { - const { mutate, ...rest } = usePatchSettings(); + const { mutate, ...other } = usePatchSettings(); const setSeason = ( id: number, @@ -26,5 +26,5 @@ export const useSetSeason = () => { >, ) => mutate({ season: id }, options); - return { setSeason, ...rest }; + return { setSeason, ...other }; }; diff --git a/vinvoor/src/hooks/useSettings.ts b/vinvoor/src/hooks/useSettings.ts index dcf1e10..3432b31 100644 --- a/vinvoor/src/hooks/useSettings.ts +++ b/vinvoor/src/hooks/useSettings.ts @@ -1,4 +1,4 @@ -import { useMutation, useQuery } from "@tanstack/react-query"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { getApi, patchApi } from "../util/fetch"; import { converSettingsJSON, Settings, SettingsJSON } from "../types/settings"; @@ -11,8 +11,15 @@ export const useSettings = () => retry: 1, }); -export const usePatchSettings = () => - useMutation({ +export const usePatchSettings = () => { + const queryClient = useQueryClient(); + + return useMutation({ mutationFn: (args: Record) => patchApi(ENDPOINT, args), + onSuccess: () => + queryClient.invalidateQueries({ + predicate: query => query.queryKey[0] !== "settings", + }), }); +}; diff --git a/vinvoor/src/settings/Settings.tsx b/vinvoor/src/settings/Settings.tsx index 3d30ad2..9c34630 100644 --- a/vinvoor/src/settings/Settings.tsx +++ b/vinvoor/src/settings/Settings.tsx @@ -89,7 +89,10 @@ export const Settings = () => { - +