2025-01-18 11:20:47 +08:00
|
|
|
import {
|
|
|
|
PREFETCH_STALE_TIME,
|
|
|
|
useCreateReactMutation,
|
|
|
|
useCreateReactQuery
|
|
|
|
} from '@/api/createReactQuery';
|
2025-02-15 12:02:25 +08:00
|
|
|
import {
|
|
|
|
getUser,
|
|
|
|
getUser_server,
|
|
|
|
updateOrganizationUser,
|
|
|
|
getMyUserInfo,
|
|
|
|
getMyUserInfo_server
|
|
|
|
} from './requests';
|
2025-01-18 00:46:13 +08:00
|
|
|
import { useMemoizedFn } from 'ahooks';
|
|
|
|
import { QueryClient, useQueryClient } from '@tanstack/react-query';
|
2025-02-02 12:27:54 +08:00
|
|
|
import type { OrganizationUser } from '@/api/asset_interfaces';
|
2025-02-15 12:02:25 +08:00
|
|
|
import { queryKeys } from '@/api/query_keys';
|
|
|
|
|
|
|
|
export const useGetMyUserInfo = () => {
|
|
|
|
const queryFn = useMemoizedFn(async () => getMyUserInfo());
|
|
|
|
|
|
|
|
return useCreateReactQuery({
|
|
|
|
...queryKeys['/users/response:getUserMyself'],
|
|
|
|
queryFn
|
|
|
|
// staleTime: PREFETCH_STALE_TIME,
|
|
|
|
// enabled: false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const prefetchGetMyUserInfo = async (queryClientProp?: QueryClient) => {
|
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
|
|
|
await queryClient.prefetchQuery({
|
|
|
|
...queryKeys['/users/response:getUserMyself'],
|
|
|
|
queryFn: () => getMyUserInfo()
|
|
|
|
});
|
|
|
|
};
|
2025-01-18 00:46:13 +08:00
|
|
|
|
|
|
|
export const useGetUser = (params: Parameters<typeof getUser>[0]) => {
|
|
|
|
const queryFn = useMemoizedFn(() => {
|
|
|
|
return getUser(params);
|
|
|
|
});
|
|
|
|
|
|
|
|
return useCreateReactQuery({
|
|
|
|
queryKey: config.USER_QUERY_KEY_ID(params.userId),
|
|
|
|
queryFn,
|
2025-01-18 11:20:47 +08:00
|
|
|
staleTime: PREFETCH_STALE_TIME
|
2025-01-18 00:46:13 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useUpdateUser = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
const mutationFn = useMemoizedFn(async (params: Parameters<typeof updateOrganizationUser>[0]) => {
|
|
|
|
queryClient.setQueryData(
|
|
|
|
config.USER_QUERY_KEY_ID(params.userId),
|
|
|
|
(oldData: OrganizationUser) => {
|
|
|
|
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),
|
2025-01-18 00:46:13 +08:00
|
|
|
queryFn: () => getUser_server({ userId })
|
|
|
|
});
|
|
|
|
return queryClient;
|
|
|
|
};
|