mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-02 22:01:05 +02:00
refactor: update user mutation and query atoms to use USER_QUERY_KEY
- Replaced cacheKeys with USER_QUERY_KEY in user-mutation.atoms.ts for consistency. - Updated query function in user-query.atoms.ts to use a separate userQueryFn for clarity.
This commit is contained in:
parent
18c9a39345
commit
9b9279ec54
2 changed files with 9 additions and 7 deletions
|
|
@ -1,18 +1,18 @@
|
||||||
import { atomWithMutation, queryClientAtom } from "jotai-tanstack-query";
|
import { atomWithMutation, queryClientAtom } from "jotai-tanstack-query";
|
||||||
import type { UpdateUserRequest } from "@/contracts/types/user.types";
|
import type { UpdateUserRequest } from "@/contracts/types/user.types";
|
||||||
import { userApiService } from "@/lib/apis/user-api.service";
|
import { userApiService } from "@/lib/apis/user-api.service";
|
||||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
import { USER_QUERY_KEY } from "./user-query.atoms";
|
||||||
|
|
||||||
export const updateUserMutationAtom = atomWithMutation((get) => {
|
export const updateUserMutationAtom = atomWithMutation((get) => {
|
||||||
const queryClient = get(queryClientAtom);
|
const queryClient = get(queryClientAtom);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
mutationKey: cacheKeys.user.current(),
|
mutationKey: USER_QUERY_KEY,
|
||||||
mutationFn: async (request: UpdateUserRequest) => {
|
mutationFn: async (request: UpdateUserRequest) => {
|
||||||
return userApiService.updateMe(request);
|
return userApiService.updateMe(request);
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
queryClient.invalidateQueries({ queryKey: cacheKeys.user.current() });
|
queryClient.invalidateQueries({ queryKey: USER_QUERY_KEY });
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,16 @@
|
||||||
import { atomWithQuery } from "jotai-tanstack-query";
|
import { atomWithQuery } from "jotai-tanstack-query";
|
||||||
import { userApiService } from "@/lib/apis/user-api.service";
|
import { userApiService } from "@/lib/apis/user-api.service";
|
||||||
import { getBearerToken, isPublicRoute } from "@/lib/auth-utils";
|
import { getBearerToken, isPublicRoute } from "@/lib/auth-utils";
|
||||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
|
||||||
|
export const USER_QUERY_KEY = ["user", "me"] as const;
|
||||||
|
const userQueryFn = () => userApiService.getMe();
|
||||||
|
|
||||||
export const currentUserAtom = atomWithQuery(() => {
|
export const currentUserAtom = atomWithQuery(() => {
|
||||||
const pathname = typeof window !== "undefined" ? window.location.pathname : null;
|
const pathname = typeof window !== "undefined" ? window.location.pathname : null;
|
||||||
return {
|
return {
|
||||||
queryKey: cacheKeys.user.current(),
|
queryKey: USER_QUERY_KEY,
|
||||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
staleTime: 5 * 60 * 1000,
|
||||||
enabled: !!getBearerToken() && pathname !== null && !isPublicRoute(pathname),
|
enabled: !!getBearerToken() && pathname !== null && !isPublicRoute(pathname),
|
||||||
queryFn: async () => userApiService.getMe(),
|
queryFn: userQueryFn,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue