buster/web/src/api/buster_rest/users/queryRequests.ts

66 lines
2.0 KiB
TypeScript
Raw Normal View History

import {
PREFETCH_STALE_TIME,
useCreateReactMutation,
useCreateReactQuery
} from '@/api/createReactQuery';
2025-02-15 12:20:22 +08:00
import { getUser, getUser_server, updateOrganizationUser, getMyUserInfo } from './requests';
import { useMemoizedFn } from 'ahooks';
import { QueryClient, useQueryClient } from '@tanstack/react-query';
2025-02-15 12:02:25 +08:00
import { queryKeys } from '@/api/query_keys';
export const useGetMyUserInfo = () => {
const queryFn = useMemoizedFn(async () => getMyUserInfo());
return useCreateReactQuery({
2025-02-15 12:20:22 +08:00
queryKey: queryKeys['/users/response:getUserMyself'].queryKey,
queryFn,
staleTime: PREFETCH_STALE_TIME,
enabled: false //This is a server only query
2025-02-15 12:02:25 +08:00
});
};
export const prefetchGetMyUserInfo = async (queryClientProp?: QueryClient) => {
const queryClient = queryClientProp || new QueryClient();
await queryClient.prefetchQuery({
...queryKeys['/users/response:getUserMyself'],
queryFn: () => getMyUserInfo()
});
};
export const useGetUser = (params: Parameters<typeof getUser>[0]) => {
2025-02-15 12:20:22 +08:00
const queryFn = useMemoizedFn(() => getUser(params));
return useCreateReactQuery({
2025-02-15 12:20:22 +08:00
queryKey: queryKeys['/users/response:getUser'](params.userId).queryKey,
queryFn,
staleTime: PREFETCH_STALE_TIME
});
};
export const useUpdateUser = () => {
const queryClient = useQueryClient();
const mutationFn = useMemoizedFn(async (params: Parameters<typeof updateOrganizationUser>[0]) => {
2025-02-15 12:20:22 +08:00
const options = queryKeys['/users/response:getUser'](params.userId);
queryClient.setQueryData(options.queryKey, (oldData) => {
return {
...oldData!,
...params
};
});
const res = await updateOrganizationUser(params);
return res;
});
return useCreateReactMutation({
mutationFn: mutationFn
});
};
export const prefetchGetUser = async (userId: string, queryClientProp?: QueryClient) => {
const queryClient = queryClientProp || new QueryClient();
await queryClient.prefetchQuery({
2025-02-15 12:02:25 +08:00
...queryKeys['/users/response:getUser'](userId),
queryFn: () => getUser_server({ userId })
});
return queryClient;
};