2025-03-06 03:14:24 +08:00
|
|
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
2025-02-18 08:30:05 +08:00
|
|
|
import {
|
|
|
|
getUser,
|
|
|
|
getUser_server,
|
|
|
|
updateOrganizationUser,
|
|
|
|
getMyUserInfo,
|
2025-03-11 12:36:25 +08:00
|
|
|
getMyUserInfo_server,
|
|
|
|
getUserFavorites,
|
|
|
|
getUserFavorites_server,
|
|
|
|
createUserFavorite,
|
|
|
|
deleteUserFavorite,
|
|
|
|
updateUserFavorites,
|
|
|
|
getUserList,
|
|
|
|
getUserList_server
|
2025-02-18 08:30:05 +08:00
|
|
|
} from './requests';
|
2025-03-08 07:02:56 +08:00
|
|
|
import { useMemoizedFn } from '@/hooks';
|
2025-01-18 00:46:13 +08:00
|
|
|
import { QueryClient, useQueryClient } from '@tanstack/react-query';
|
2025-02-15 12:02:25 +08:00
|
|
|
import { queryKeys } from '@/api/query_keys';
|
2025-03-12 01:15:37 +08:00
|
|
|
import type { UserRequestUserListPayload } from '@/api/request_interfaces/user/interfaces';
|
2025-02-15 12:02:25 +08:00
|
|
|
|
|
|
|
export const useGetMyUserInfo = () => {
|
2025-03-06 03:14:24 +08:00
|
|
|
return useQuery({
|
2025-02-18 13:22:32 +08:00
|
|
|
...queryKeys.userGetUserMyself,
|
2025-03-12 01:15:37 +08:00
|
|
|
queryFn: getMyUserInfo,
|
2025-02-15 12:20:22 +08:00
|
|
|
enabled: false //This is a server only query
|
2025-02-15 12:02:25 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2025-02-18 08:30:05 +08:00
|
|
|
export const prefetchGetMyUserInfo = async (
|
|
|
|
params: Parameters<typeof getMyUserInfo_server>[0],
|
|
|
|
queryClientProp?: QueryClient
|
|
|
|
) => {
|
2025-02-15 12:02:25 +08:00
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
2025-02-18 08:30:05 +08:00
|
|
|
const initialData = await getMyUserInfo_server(params);
|
2025-02-15 12:02:25 +08:00
|
|
|
await queryClient.prefetchQuery({
|
2025-02-18 13:22:32 +08:00
|
|
|
...queryKeys.userGetUserMyself,
|
2025-02-19 05:15:01 +08:00
|
|
|
queryFn: () => initialData!,
|
2025-02-18 08:30:05 +08:00
|
|
|
initialData
|
2025-02-15 12:02:25 +08:00
|
|
|
});
|
2025-02-18 08:30:05 +08:00
|
|
|
return { queryClient, initialData };
|
2025-02-15 12:02:25 +08:00
|
|
|
};
|
2025-01-18 00:46:13 +08:00
|
|
|
|
|
|
|
export const useGetUser = (params: Parameters<typeof getUser>[0]) => {
|
2025-02-15 12:20:22 +08:00
|
|
|
const queryFn = useMemoizedFn(() => getUser(params));
|
2025-01-18 00:46:13 +08:00
|
|
|
|
2025-03-06 03:14:24 +08:00
|
|
|
return useQuery({
|
2025-02-18 13:22:32 +08:00
|
|
|
...queryKeys.userGetUser(params.userId),
|
2025-03-06 03:14:24 +08:00
|
|
|
queryFn
|
2025-01-18 00:46:13 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useUpdateUser = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
const mutationFn = useMemoizedFn(async (params: Parameters<typeof updateOrganizationUser>[0]) => {
|
2025-02-18 13:22:32 +08:00
|
|
|
const options = queryKeys.userGetUser(params.userId);
|
2025-02-15 12:20:22 +08:00
|
|
|
queryClient.setQueryData(options.queryKey, (oldData) => {
|
|
|
|
return {
|
|
|
|
...oldData!,
|
|
|
|
...params
|
|
|
|
};
|
|
|
|
});
|
2025-01-18 00:46:13 +08:00
|
|
|
const res = await updateOrganizationUser(params);
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
|
2025-03-06 03:14:24 +08:00
|
|
|
return useMutation({
|
2025-01-18 00:46:13 +08:00
|
|
|
mutationFn: mutationFn
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const prefetchGetUser = async (userId: string, queryClientProp?: QueryClient) => {
|
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
|
|
|
await queryClient.prefetchQuery({
|
2025-02-18 13:22:32 +08:00
|
|
|
...queryKeys.userGetUser(userId),
|
2025-01-18 00:46:13 +08:00
|
|
|
queryFn: () => getUser_server({ userId })
|
|
|
|
});
|
|
|
|
return queryClient;
|
|
|
|
};
|
2025-03-11 12:36:25 +08:00
|
|
|
|
|
|
|
export const useGetUserFavorites = () => {
|
|
|
|
const queryFn = useMemoizedFn(async () => getUserFavorites());
|
|
|
|
return useQuery({
|
|
|
|
...queryKeys.favoritesGetList,
|
|
|
|
queryFn
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const prefetchGetUserFavorites = async (queryClientProp?: QueryClient) => {
|
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
|
|
|
await queryClient.prefetchQuery({
|
|
|
|
...queryKeys.favoritesGetList,
|
|
|
|
queryFn: () => getUserFavorites_server()
|
|
|
|
});
|
|
|
|
return queryClient;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useAddUserFavorite = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: createUserFavorite,
|
|
|
|
onMutate: (params) => {
|
|
|
|
queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => {
|
|
|
|
return [params, ...(prev || [])];
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSettled: () => {
|
|
|
|
queryClient.invalidateQueries(queryKeys.favoritesGetList);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useDeleteUserFavorite = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: deleteUserFavorite,
|
|
|
|
onMutate: (params) => {
|
|
|
|
queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => {
|
|
|
|
return prev?.filter((fav) => fav.id !== params.id);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSettled: () => {
|
|
|
|
queryClient.invalidateQueries(queryKeys.favoritesGetList);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useUpdateUserFavorites = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: updateUserFavorites,
|
|
|
|
onMutate: (params) => {
|
|
|
|
queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => {
|
|
|
|
return prev?.filter((fav, index) => {
|
|
|
|
const id = fav.id || fav.collection_id;
|
|
|
|
const favorite = (prev || []).find((f) => f.id === id || f.collection_id === id)!;
|
|
|
|
return { ...favorite, index };
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useGetUserList = (params: UserRequestUserListPayload) => {
|
|
|
|
const queryFn = useMemoizedFn(() => getUserList(params));
|
|
|
|
|
|
|
|
return useQuery({
|
|
|
|
...queryKeys.userGetUserList(params),
|
|
|
|
queryFn
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const prefetchGetUserList = async (
|
|
|
|
params: UserRequestUserListPayload,
|
|
|
|
queryClientProp?: QueryClient
|
|
|
|
) => {
|
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
|
|
|
await queryClient.prefetchQuery({
|
|
|
|
...queryKeys.userGetUserList(params),
|
|
|
|
queryFn: () => getUserList_server(params)
|
|
|
|
});
|
|
|
|
return queryClient;
|
|
|
|
};
|